| 
									
										
										
										
											2019-03-16 09:37:35 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							| 
									
										
										
										
											2019-03-15 01:32:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-31 08:43:49 +00:00
										 |  |  | # Redmine - project management software | 
					
						
							| 
									
										
										
										
											2024-02-26 22:55:54 +00:00
										 |  |  | # Copyright (C) 2006-  Jean-Philippe Lang | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +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 08:43:49 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +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 08:43:49 +00:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +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 RolesController < 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 08:43:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 07:27:31 +00:00
										 |  |  |   before_action :require_admin, :except => [:index, :show] | 
					
						
							|  |  |  |   before_action :require_admin_or_api_request, :only => [:index, :show] | 
					
						
							|  |  |  |   before_action :find_role, :only => [:show, :edit, :update, :destroy] | 
					
						
							| 
									
										
										
										
											2012-11-30 07:48:47 +00:00
										 |  |  |   accept_api_auth :index, :show | 
					
						
							| 
									
										
										
										
											2007-01-28 00:00:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-12 05:29:50 +00:00
										 |  |  |   include RolesHelper | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-19 18:41:10 +00:00
										 |  |  |   require_sudo_mode :create, :update, :destroy | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |   def index | 
					
						
							| 
									
										
										
										
											2012-02-06 10:06:32 +00:00
										 |  |  |     respond_to do |format| | 
					
						
							| 
									
										
										
										
											2020-11-08 13:02:50 +00:00
										 |  |  |       format.html do | 
					
						
							| 
									
										
										
										
											2016-03-20 09:57:24 +00:00
										 |  |  |         @roles = Role.sorted.to_a | 
					
						
							|  |  |  |         render :layout => false if request.xhr? | 
					
						
							| 
									
										
										
										
											2020-11-08 13:02:50 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |       format.api do | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |         @roles = Role.givable.to_a | 
					
						
							| 
									
										
										
										
											2020-11-08 13:02:50 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2012-02-06 10:06:32 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-12 17:22:52 +00:00
										 |  |  |   def show | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.api | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |   def new | 
					
						
							| 
									
										
										
										
											2012-09-03 17:04:28 +00:00
										 |  |  |     # Prefills the form with 'Non member' role permissions by default | 
					
						
							| 
									
										
										
										
											2016-07-16 09:30:09 +00:00
										 |  |  |     @role = Role.new | 
					
						
							|  |  |  |     @role.safe_attributes = params[:role] || {:permissions => Role.non_member.permissions} | 
					
						
							| 
									
										
										
										
											2012-09-03 17:04:28 +00:00
										 |  |  |     if params[:copy].present? && @copy_from = Role.find_by_id(params[:copy]) | 
					
						
							|  |  |  |       @role.copy_from(@copy_from) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |     @roles = Role.sorted.to_a | 
					
						
							| 
									
										
										
										
											2011-12-09 23:29:58 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def create | 
					
						
							| 
									
										
										
										
											2016-07-16 09:30:09 +00:00
										 |  |  |     @role = Role.new | 
					
						
							|  |  |  |     @role.safe_attributes = params[:role] | 
					
						
							| 
									
										
										
										
											2007-08-29 16:52:35 +00:00
										 |  |  |     if request.post? && @role.save | 
					
						
							| 
									
										
										
										
											2008-03-15 08:27:38 +00:00
										 |  |  |       # workflow copy | 
					
						
							| 
									
										
										
										
											2023-03-25 01:32:23 +00:00
										 |  |  |       if params[:copy_workflow_from].present? && (copy_from = Role.find_by_id(params[:copy_workflow_from])) | 
					
						
							| 
									
										
										
										
											2017-06-03 08:04:13 +00:00
										 |  |  |         @role.copy_workflow_rules(copy_from) | 
					
						
							| 
									
										
										
										
											2008-03-15 08:27:38 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2007-08-29 16:52:35 +00:00
										 |  |  |       flash[:notice] = l(:notice_successful_create) | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |       redirect_to roles_path | 
					
						
							| 
									
										
										
										
											2011-05-01 23:15:03 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2014-10-22 17:37:16 +00:00
										 |  |  |       @roles = Role.sorted.to_a | 
					
						
							| 
									
										
										
										
											2011-12-09 23:29:58 +00:00
										 |  |  |       render :action => 'new' | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def edit | 
					
						
							| 
									
										
										
										
											2011-12-09 23:29:58 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def update | 
					
						
							| 
									
										
										
										
											2016-07-16 09:30:09 +00:00
										 |  |  |     @role.safe_attributes = params[:role] | 
					
						
							|  |  |  |     if @role.save | 
					
						
							| 
									
										
										
										
											2016-04-17 07:40:39 +00:00
										 |  |  |       respond_to do |format| | 
					
						
							| 
									
										
										
										
											2020-11-08 13:02:50 +00:00
										 |  |  |         format.html do | 
					
						
							| 
									
										
										
										
											2016-04-17 07:40:39 +00:00
										 |  |  |           flash[:notice] = l(:notice_successful_update) | 
					
						
							|  |  |  |           redirect_to roles_path(:page => params[:page]) | 
					
						
							| 
									
										
										
										
											2020-11-08 13:02:50 +00:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2020-11-19 13:30:39 +00:00
										 |  |  |         format.js {head 200} | 
					
						
							| 
									
										
										
										
											2016-04-17 07:40:39 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2011-05-01 23:15:03 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2016-04-17 07:40:39 +00:00
										 |  |  |       respond_to do |format| | 
					
						
							| 
									
										
										
										
											2020-11-19 13:30:39 +00:00
										 |  |  |         format.html {render :action => 'edit'} | 
					
						
							|  |  |  |         format.js   {head 422} | 
					
						
							| 
									
										
										
										
											2016-04-17 07:40:39 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def destroy | 
					
						
							| 
									
										
										
										
											2017-04-05 15:59:43 +00:00
										 |  |  |     begin | 
					
						
							|  |  |  |       @role.destroy | 
					
						
							|  |  |  |     rescue | 
					
						
							|  |  |  |       flash[:error] =  l(:error_can_not_remove_role) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2012-12-11 19:39:47 +00:00
										 |  |  |     redirect_to roles_path | 
					
						
							| 
									
										
										
										
											2007-03-12 17:59:02 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-08-31 08:43:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-09 23:29:58 +00:00
										 |  |  |   def permissions | 
					
						
							| 
									
										
										
										
											2019-10-25 15:53:27 +00:00
										 |  |  |     scope = Role.sorted | 
					
						
							|  |  |  |     if params[:ids].present? | 
					
						
							|  |  |  |       scope = scope.where(:id => params[:ids]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     @roles = scope.to_a | 
					
						
							| 
									
										
										
										
											2021-03-12 05:29:50 +00:00
										 |  |  |     @permissions = Redmine::AccessControl.permissions.reject(&:public?) | 
					
						
							|  |  |  |     respond_to do |format| | 
					
						
							|  |  |  |       format.html | 
					
						
							|  |  |  |       format.csv do | 
					
						
							|  |  |  |         send_data(permissions_to_csv(@roles, @permissions), :type => 'text/csv; header=present', :filename => 'permissions.csv') | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-10-25 16:31:57 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def update_permissions | 
					
						
							|  |  |  |     @roles = Role.where(:id => params[:permissions].keys) | 
					
						
							|  |  |  |     @roles.each do |role| | 
					
						
							|  |  |  |       role.permissions = params[:permissions][role.id.to_s] | 
					
						
							|  |  |  |       role.save | 
					
						
							| 
									
										
										
										
											2007-04-07 20:27:19 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-10-25 16:31:57 +00:00
										 |  |  |     flash[:notice] = l(:notice_successful_update) | 
					
						
							|  |  |  |     redirect_to roles_path | 
					
						
							| 
									
										
										
										
											2007-04-07 20:27:19 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2011-12-09 23:29:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def find_role | 
					
						
							|  |  |  |     @role = Role.find(params[:id]) | 
					
						
							|  |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     render_404 | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2006-06-28 18:11:03 +00:00
										 |  |  | end |