1 # Copyright (C) 2014 Heiko Bernloehr (FreeIT.de).
3 # This file is part of ECS.
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.
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.
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]
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" }
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 }
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) }
66 json_data= ActiveSupport::JSON.decode request.raw_post
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) }
80 subparticipant= Subparticipant.find(params[:id])
81 subparticipant.participant.destroy
82 render :text => "", :layout => false, :status => 200, :content_type => :json
87 def show_render(subparticipant)
88 participant= subparticipant.participant
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,
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."
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