Hide public communty.
[ecs.git] / app / controllers / admin / participants_controller.rb
blobbeabd283570e3fe451d277d1f397acc37e6c39c3
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
21   require 'pp'
23   include Admin::Helper
25   # TODO verify http methods
27   def default
28     redirect_to admin_participants_path
29   end
31   def index
32     list
33     render :action => 'list'
34   end
36   def list
37     if params[:sys] == "true"
38       @participants = Participant.all
39     else
40       @participants = Participant.all.where.not(name: "ecs")
41     end
43     @list_participants_anonymous_count = @participants.count - @participants.without_anonymous.count
44     @participants = case params[:anonymous]
45       when "true"
46         @list_anonymous=true
47         @list_participants_count = @participants.count
48         @participants.distinct
49       when "false"
50         @list_anonymous=false
51         @list_participants_count = @participants.count - @list_participants_anonymous_count
52         @participants.without_anonymous.distinct
53       else
54         @list_anonymous=false
55         @list_participants_count = @participants.count - @list_participants_anonymous_count
56         @participants.without_anonymous.distinct
57     end
58     @action_buttons = case params[:actionbuttons]
59                       when "true"
60                         true
61                       when "false"
62                         false
63                       else
64                         false
65                       end
66   end
68   def show
69     @participant = Participant.find(params[:id])
70   end
72   def new
73     @participant = Participant.new
74     @organizations = Organization.all.order(:id)
75     if ECS_CONFIG["participants"]["allow_anonymous"]
76       @communities=Community.all.distinct
77     else
78       @communities=Community.all.where.not(name: "public").distinct
79     end
80     @participant.identities.build
81   end
83   def create
84     @participant = Participant.new(participant_params)
85     @participant.ptype = Participant::TYPE[:main]
86     if @participant.save
87       flash[:notice] = "Participant \"#{CGI.escapeHTML @participant.name}\" was successfully created."
88       redirect_to admin_participants_path
89     else
90       @organizations = Organization.all.order(:id)
91       render :action => 'new'
92     end
93   end
95   def edit
96     @participant = Participant.find(params[:id])
97     @organizations = Organization.all.order(:id)
98     if ECS_CONFIG["participants"]["allow_anonymous"]
99       @communities=Community.all.distinct
100     else
101       @communities=Community.all.where.not(name: "public").distinct
102     end
103   end
105   def reset
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
112   end
114   def update
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)
123     else
124       render :action => 'edit'
125     end
126   end
128   def destroy
129     p = Participant.find(params[:id])
130     p.destroy
131     flash[:notice] = "Participant \"#{CGI.escapeHTML p.name}\" was successfully destroyed."
132     redirect_to_admin_participants_path
133   end
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 }
138   end
140   # lists all those communities which the participant has not yet joined
141   def index_noncommunities
142     index_communities
143     @communities=(Community.all - @communities).sort{|x,y| x.id <=> y.id }
144   end
146   def destroy_community
147     destroy_membership(params[:c_id], params[:id])
148     redirect_to admin_participant_communities_path(params[:id])
149   end
152   # join to a community
153   def create_community
154     create_membership(params[:c_id], params[:id])
155     redirect_to admin_participant_communities_path(params[:id])
156   end
158 private
160   def redirect_to_admin_participants_path
161     queryparams={}
162     if params[:anonymous]
163       queryparams[:anonymous]=true
164     end
165     if params[:actionbuttons]
166       queryparams[:actionbuttons]=true
167     end
168     redirect_to admin_participants_path(queryparams)
169   end
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})"
177     end
178   end
180   def leaved_messages(participant, community_ids)
181     leaved_community_ids= participant.communities.map{|c| c.id} - community_ids.map{|p| p.to_i}
182     leaved_messages= []
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
186     end
187     leaved_messages.flatten.compact.uniq
188   end
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])
193     #  Parameter-Example:
194     #    { "utf8"=>"✓",
195     #      "authenticity_token"=>"NQtz97vmdpbhtkRAwEaouDia55K+XXnApAN9+flu2sw=",
196     #      "participant"=>
197     #        {
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",
204     #          "events_"=>"1",
205     #          "identities_attributes"=>
206     #            {
207     #              "0"=>
208     #                {
209     #                  "name"=>"27_vorkauf@rus.uni-stuttgart.de",
210     #                  "description"=>"X.509 credentials",
211     #                  "_destroy"=>"0",
212     #                  "id"=>"4"
213     #                },
214     #              "1"=>
215     #                {
216     #                  "name"=>"A2_vorkauf@rus.uni-stuttgart.de",
217     #                  "description"=>"X.509 credentials",
218     #                  "_destroy"=>"0",
219     #                  "id"=>"21"
220     #                },
221     #              "2"=>
222     #                {
223     #                  "name"=>"hubba bubba",
224     #                  "description"=>"Und noch mehr"
225     #                }
226     #            },
227     #            "community_ids"=>["1"]
228     #        },
229     #      "commit"=>"Save",
230     #      "id"=>"12"
231     #    }
232   end