| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2020-03-03 00:24:10 +00:00
										 |  |  | # Copyright (C) 2006-2020  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  | # modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  | # as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  | # of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | # This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | # GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | # You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  | # along with this program; if not, write to the Free Software | 
					
						
							|  |  |  | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class GroupsController < ApplicationController | 
					
						
							| 
									
										
										
										
											2009-12-17 18:21:02 +00:00
										 |  |  |   layout 'admin' | 
					
						
							| 
									
										
										
										
											2016-11-19 10:30:02 +00:00
										 |  |  |   self.main_menu = false | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :require_admin | 
					
						
							|  |  |  |   before_action :find_group, :except => [:index, :new, :create] | 
					
						
							| 
									
										
										
										
											2012-06-03 10:40:32 +00:00
										 |  |  |   accept_api_auth :index, :show, :create, :update, :destroy, :add_users, :remove_user | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 18:41:10 +00:00
										 |  |  |   require_sudo_mode :add_users, :remove_user, :create, :update, :destroy, :edit_membership, :destroy_membership | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   helper :custom_fields | 
					
						
							| 
									
										
										
										
											2014-10-23 21:46:40 +00:00
										 |  |  |   helper :principal_memberships | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   def index | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2014-05-24 10:41:13 +00:00
										 |  |  |       format.html { | 
					
						
							| 
									
										
										
										
											2016-08-30 20:13:01 +00:00
										 |  |  |         scope = Group.sorted | 
					
						
							|  |  |  |         scope = scope.like(params[:name]) if params[:name].present? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         @group_count = scope.count | 
					
						
							|  |  |  |         @group_pages = Paginator.new @group_count, per_page_option, params['page'] | 
					
						
							|  |  |  |         @groups = scope.limit(@group_pages.per_page).offset(@group_pages.offset).to_a | 
					
						
							| 
									
										
										
										
											2014-05-24 10:41:13 +00:00
										 |  |  |         @user_count_by_group_id = user_count_by_group_id | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2014-09-28 14:51:08 +00:00
										 |  |  |       format.api { | 
					
						
							|  |  |  |         scope = Group.sorted | 
					
						
							|  |  |  |         scope = scope.givable unless params[:builtin] == '1' | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |         @groups = scope.to_a | 
					
						
							| 
									
										
										
										
											2014-09-28 14:51:08 +00:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def show | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-06-01 19:07:22 +00:00
										 |  |  |       format.html | 
					
						
							| 
									
										
										
										
											2012-06-03 10:40:32 +00:00
										 |  |  |       format.api | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def new | 
					
						
							|  |  |  |     @group = Group.new | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def create | 
					
						
							| 
									
										
										
										
											2012-06-01 19:06:16 +00:00
										 |  |  |     @group = Group.new | 
					
						
							|  |  |  |     @group.safe_attributes = params[:group] | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       if @group.save | 
					
						
							| 
									
										
										
										
											2011-07-21 15:55:50 +00:00
										 |  |  |         format.html { | 
					
						
							|  |  |  |           flash[:notice] = l(:notice_successful_create) | 
					
						
							|  |  |  |           redirect_to(params[:continue] ? new_group_path : groups_path) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2012-06-03 10:40:32 +00:00
										 |  |  |         format.api  { render :action => 'show', :status => :created, :location => group_url(@group) } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         format.html { render :action => "new" } | 
					
						
							| 
									
										
										
										
											2012-06-03 10:40:32 +00:00
										 |  |  |         format.api  { render_validation_errors(@group) } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-01 19:07:22 +00:00
										 |  |  |   def edit | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   def update | 
					
						
							| 
									
										
										
										
											2012-06-01 19:06:16 +00:00
										 |  |  |     @group.safe_attributes = params[:group] | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-06-01 19:06:16 +00:00
										 |  |  |       if @group.save | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |         flash[:notice] = l(:notice_successful_update) | 
					
						
							| 
									
										
										
										
											2016-08-30 20:20:09 +00:00
										 |  |  |         format.html { redirect_to_referer_or(groups_path) } | 
					
						
							| 
									
										
										
										
											2012-07-14 08:13:55 +00:00
										 |  |  |         format.api  { render_api_ok } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         format.html { render :action => "edit" } | 
					
						
							| 
									
										
										
										
											2012-06-03 10:40:32 +00:00
										 |  |  |         format.api  { render_validation_errors(@group) } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy | 
					
						
							|  |  |  |     @group.destroy | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2016-08-30 20:20:09 +00:00
										 |  |  |       format.html { redirect_to_referer_or(groups_path) } | 
					
						
							| 
									
										
										
										
											2012-07-14 08:13:55 +00:00
										 |  |  |       format.api  { render_api_ok } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-16 08:53:39 +00:00
										 |  |  |   def new_users | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   def add_users | 
					
						
							| 
									
										
										
										
											2014-12-21 20:15:24 +00:00
										 |  |  |     @users = User.not_in_group(@group).where(:id => (params[:user_id] || params[:user_ids])).to_a | 
					
						
							|  |  |  |     @group.users << @users | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-12-11 17:51:30 +00:00
										 |  |  |       format.html { redirect_to edit_group_path(@group, :tab => 'users') } | 
					
						
							| 
									
										
										
										
											2012-07-19 16:57:59 +00:00
										 |  |  |       format.js | 
					
						
							| 
									
										
										
										
											2014-12-21 20:15:24 +00:00
										 |  |  |       format.api { | 
					
						
							|  |  |  |         if @users.any? | 
					
						
							|  |  |  |           render_api_ok | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           render_api_errors "#{l(:label_user)} #{l('activerecord.errors.messages.invalid')}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   def remove_user | 
					
						
							| 
									
										
										
										
											2011-11-27 10:47:36 +00:00
										 |  |  |     @group.users.delete(User.find(params[:user_id])) if request.delete? | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2012-12-11 17:51:30 +00:00
										 |  |  |       format.html { redirect_to edit_group_path(@group, :tab => 'users') } | 
					
						
							| 
									
										
										
										
											2012-07-19 16:57:59 +00:00
										 |  |  |       format.js | 
					
						
							| 
									
										
										
										
											2012-07-14 08:13:55 +00:00
										 |  |  |       format.api { render_api_ok } | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   def autocomplete_for_user | 
					
						
							| 
									
										
										
										
											2013-01-18 17:57:16 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.js | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 12:19:39 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-01 19:41:06 +00:00
										 |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_group | 
					
						
							|  |  |  |     @group = Group.find(params[:id]) | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-05-24 10:41:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def user_count_by_group_id | 
					
						
							|  |  |  |     h = User.joins(:groups).group('group_id').count | 
					
						
							|  |  |  |     h.keys.each do |key| | 
					
						
							|  |  |  |       h[key.to_i] = h.delete(key) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     h | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2009-09-12 08:36:46 +00:00
										 |  |  | end |