1 # Copyright (C) 2016, 2017 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/>.
19 class Admin::ParticipantsController < ApplicationController
25 # TODO verify http methods
28 redirect_to admin_participants_path
33 render :action => 'list'
37 if params[:sys] == "true"
38 @participants = Participant.all
40 @participants = Participant.all.where.not(name: "ecs")
43 @list_participants_anonymous_count = @participants.count - @participants.without_anonymous.count
44 @participants = case params[:anonymous]
47 @list_participants_count = @participants.count
48 @participants.distinct
51 @list_participants_count = @participants.count - @list_participants_anonymous_count
52 @participants.without_anonymous.distinct
55 @list_participants_count = @participants.count - @list_participants_anonymous_count
56 @participants.without_anonymous.distinct
58 @action_buttons = case params[:actionbuttons]
69 @participant = Participant.find(params[:id])
73 @participant = Participant.new
74 @organizations = Organization.all.order(:id)
75 if ECS_CONFIG["participants"]["allow_anonymous"]
76 @communities=Community.all.distinct
78 @communities=Community.all.where.not(name: "public").distinct
80 @participant.identities.build
84 @participant = Participant.new(participant_params)
85 @participant.ptype = Participant::TYPE[:main]
87 flash[:notice] = "Participant \"#{CGI.escapeHTML @participant.name}\" was successfully created."
88 redirect_to admin_participants_path
90 @organizations = Organization.all.order(:id)
91 render :action => 'new'
96 @participant = Participant.find(params[:id])
97 @organizations = Organization.all.order(:id)
98 if ECS_CONFIG["participants"]["allow_anonymous"]
99 @communities=Community.all.distinct
101 @communities=Community.all.where.not(name: "public").distinct
106 @participant = Participant.find(params[:id])
107 @participant.destroy_receiver_messages
108 @participant.destroy_sender_messages
109 @participant.destroy_events
110 flash[:notice] = "Successfully cleared all sent and received messages and events of participant \"#{CGI.escapeHTML @participant.name}\"."
111 redirect_to_admin_participants_path
115 params[:participant][:community_ids] ||= []
116 @organizations = Organization.all.order(:id)
117 @participant = Participant.find(params[:id])
118 lmsgs= leaved_messages(@participant, params[:participant][:community_ids])
119 if @participant.update(participant_params)
120 generate_destroyed_events_by_leaving_a_community(@participant,lmsgs) unless lmsgs.blank?
121 flash[:notice] = 'Participant was successfully updated.'
122 redirect_to admin_participant_path(:id => @participant)
124 render :action => 'edit'
129 p = Participant.find(params[:id])
131 flash[:notice] = "Participant \"#{CGI.escapeHTML p.name}\" was successfully destroyed."
132 redirect_to_admin_participants_path
135 def index_communities
136 @participant = Participant.find(params[:id])
137 @communities=Participant.find(params[:id]).memberships.collect {|i| i.community }.uniq.sort{|x,y| x.id <=> y.id }
140 # lists all those communities which the participant has not yet joined
141 def index_noncommunities
143 @communities=(Community.all - @communities).sort{|x,y| x.id <=> y.id }
146 def destroy_community
147 destroy_membership(params[:c_id], params[:id])
148 redirect_to admin_participant_communities_path(params[:id])
152 # join to a community
154 create_membership(params[:c_id], params[:id])
155 redirect_to admin_participant_communities_path(params[:id])
160 def redirect_to_admin_participants_path
162 if params[:anonymous]
163 queryparams[:anonymous]=true
165 if params[:actionbuttons]
166 queryparams[:actionbuttons]=true
168 redirect_to admin_participants_path(queryparams)
171 # Generate destroyed events for all messages unconnected in respect to the
172 # leaving communities.
173 def generate_destroyed_events_by_leaving_a_community(participant, messages )
174 messages.each do |msg|
175 Event.make(:event_type_name => EvType.find_by_name("destroyed").name, :participant => participant, :message => msg)
176 logger.info "destroyed event for message.id=#{msg.id}, participant:#{participant.name} (pid:#{participant.id})"
180 def leaved_messages(participant, community_ids)
181 leaved_community_ids= participant.communities.map{|c| c.id} - community_ids.map{|p| p.to_i}
183 leaved_community_ids.each do |cid|
184 leaved_messages << Membership.find_by_participant_id_and_community_id(participant.id, cid).messages
185 leaved_messages << Community.find(cid).messages
187 leaved_messages.flatten.compact.uniq
190 def participant_params
191 params.require(:participant).permit(:name, :description, :dns, :email, :organization_id, :community_selfrouting, :events_, community_ids: [],
192 identities_attributes: [:id, :name, :description, :_destroy])
195 # "authenticity_token"=>"NQtz97vmdpbhtkRAwEaouDia55K+XXnApAN9+flu2sw=",
198 # "name"=>"Universität Stuttgart ILIAS",
199 # "description"=>"Zentrale E-Learning Plattform.",
200 # "dns"=>"ilias3.uni-stuttgart.de",
201 # "email"=>"christian.bogen@tik.uni-stuttgart.de",
202 # "organization_id"=>"3",
203 # "community_selfrouting"=>"0",
205 # "identities_attributes"=>
209 # "name"=>"27_vorkauf@rus.uni-stuttgart.de",
210 # "description"=>"X.509 credentials",
216 # "name"=>"A2_vorkauf@rus.uni-stuttgart.de",
217 # "description"=>"X.509 credentials",
223 # "name"=>"hubba bubba",
224 # "description"=>"Und noch mehr"
227 # "community_ids"=>["1"]