Adjust controllers.
[ecs.git] / app / controllers / subparticipants_controller.rb
blob3d585ae9a2d412c140a2326a8652f3929179a8c6
1 # Copyright (C) 2014 Heiko Bernloehr (FreeIT.de).
2
3 # This file is part of ECS.
4
5 # ECS is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of
8 # the License, or (at your option) any later version.
9
10 # ECS is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Affero General Public License for more details.
14
15 # You should have received a copy of the GNU Affero General Public
16 # License along with ECS. If not, see <http://www.gnu.org/licenses/>.
18 class SubparticipantsController < ApplicationController
20   before_filter :authentication
21   before_filter :block_anonymous_participants
22   before_filter :block_subparticipants
23   before_filter :check_json_contenttype, :only => :create
24   before_filter :check_parent, :only => [:show, :destroy, :update]
26   def initialize
27     super
28   end
30   def index
31     childs= @participant.childs
32     childs.each do |child| 
33       @body << "subparticipants/" << child.id.to_s << "\n"
34     end unless childs.empty?
35     respond_to do |format|
36       format.text { render :text => @body, :content_type => "text/uri-list" }
37     end
38   end
40   def show
41     subparticipant= Subparticipant.find(params[:id])
42     body= show_render(subparticipant)
43     respond_to do |format|
44       format.json { render :json  => JSON.pretty_generate(body) + "\r\n" }
45       format.xml  { render :xml   => body }
46     end
47   end
49   def create
50     begin
51       json_data= ActiveSupport::JSON.decode request.raw_post
52     rescue ActiveSupport::OkJson::Error, StandardError
53       raise Ecs::InvalidMessageException, "You have provided invalid JSON data (SubparticipantsController#create)."
54     end unless request.raw_post.empty?
55     logger.debug "request raw post: #{(request.raw_post.empty?)?'<empty>':request.raw_post}"  
56     subparticipant= Subparticipant.generate(@participant, json_data)
57     body= show_render(subparticipant)
58     respond_to do |format|
59       format.json { render :json  => JSON.pretty_generate(body) + "\r\n", :location => location(subparticipant) }
60       format.xml  { render :xml   => body, :location => location(subparticipant) }
61     end
62   end
64   def update
65     begin
66       json_data= ActiveSupport::JSON.decode request.raw_post
67     rescue StandardError
68       raise Ecs::InvalidMessageException, "You have provided invalid JSON data (SubparticipantsController#update)."
69     end unless request.raw_post.empty?
70     subparticipant= Subparticipant.find(params[:id])
71     subparticipant.update__(@participant, json_data, subparticipant)
72     body= show_render(subparticipant)
73     respond_to do |format|
74       format.json { render :json  => JSON.pretty_generate(body) + "\r\n", :location => location(subparticipant) }
75       format.xml  { render :xml   => body, :location => location(subparticipant) }
76     end
77   end
79   def destroy
80     subparticipant= Subparticipant.find(params[:id])
81     subparticipant.participant.destroy
82     render :text => "", :layout => false, :status => 200, :content_type => :json
83   end
85 private
87   def show_render(subparticipant)
88     participant= subparticipant.participant
89     data = nil
90     data= \
91       { 
92         :name => participant.name,
93         :description => participant.description,
94         :auth_ids => participant.identities.map{|ident| {:auth_id=>ident.name, :desc=>ident.description}},
95         :dns => participant.dns,
96         :email => participant.email,
97         :community_selfrouting => participant.community_selfrouting,
98         :events => participant.events_,
99         :memberships => Membership.memberships(participant,true),
100         :realm => subparticipant.realm,
101       }
102     data
103   end
105   def check_parent
106     subparticipant= Subparticipant.find(params[:id])
107     unless @participant.childs.include?(subparticipant)
108       raise Ecs::AuthorizationException, "You are not allowed to access this subparticipant because you are not its parent/creator."
109     end
110   end
112   def location(subparticipant)
113     location = request.protocol + request.host
114     location += ":" + request.port.to_s unless [80, 443].include?(request.port)
115     location += request.headers["SCRIPT_NAME"] if request.headers["SCRIPT_NAME"]
116     location += request.path.gsub(/\/*$/,'') + "/" + subparticipant.id.to_s
117     location
118   end