You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

roles_controller.rb 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class RolesController < ApplicationController
  19. layout 'admin'
  20. self.main_menu = false
  21. before_action :require_admin, :except => [:index, :show]
  22. before_action :require_admin_or_api_request, :only => [:index, :show]
  23. before_action :find_role, :only => [:show, :edit, :update, :destroy]
  24. accept_api_auth :index, :show
  25. include RolesHelper
  26. require_sudo_mode :create, :update, :destroy
  27. def index
  28. respond_to do |format|
  29. format.html do
  30. @roles = Role.sorted.to_a
  31. render :layout => false if request.xhr?
  32. end
  33. format.api do
  34. @roles = Role.givable.to_a
  35. end
  36. end
  37. end
  38. def show
  39. respond_to do |format|
  40. format.api
  41. end
  42. end
  43. def new
  44. # Prefills the form with 'Non member' role permissions by default
  45. @role = Role.new
  46. @role.safe_attributes = params[:role] || {:permissions => Role.non_member.permissions}
  47. if params[:copy].present? && @copy_from = Role.find_by_id(params[:copy])
  48. @role.copy_from(@copy_from)
  49. end
  50. @roles = Role.sorted.to_a
  51. end
  52. def create
  53. @role = Role.new
  54. @role.safe_attributes = params[:role]
  55. if request.post? && @role.save
  56. # workflow copy
  57. if !params[:copy_workflow_from].blank? && (copy_from = Role.find_by_id(params[:copy_workflow_from]))
  58. @role.copy_workflow_rules(copy_from)
  59. end
  60. flash[:notice] = l(:notice_successful_create)
  61. redirect_to roles_path
  62. else
  63. @roles = Role.sorted.to_a
  64. render :action => 'new'
  65. end
  66. end
  67. def edit
  68. end
  69. def update
  70. @role.safe_attributes = params[:role]
  71. if @role.save
  72. respond_to do |format|
  73. format.html do
  74. flash[:notice] = l(:notice_successful_update)
  75. redirect_to roles_path(:page => params[:page])
  76. end
  77. format.js {head 200}
  78. end
  79. else
  80. respond_to do |format|
  81. format.html {render :action => 'edit'}
  82. format.js {head 422}
  83. end
  84. end
  85. end
  86. def destroy
  87. begin
  88. @role.destroy
  89. rescue
  90. flash[:error] = l(:error_can_not_remove_role)
  91. end
  92. redirect_to roles_path
  93. end
  94. def permissions
  95. scope = Role.sorted
  96. if params[:ids].present?
  97. scope = scope.where(:id => params[:ids])
  98. end
  99. @roles = scope.to_a
  100. @permissions = Redmine::AccessControl.permissions.reject(&:public?)
  101. respond_to do |format|
  102. format.html
  103. format.csv do
  104. send_data(permissions_to_csv(@roles, @permissions), :type => 'text/csv; header=present', :filename => 'permissions.csv')
  105. end
  106. end
  107. end
  108. def update_permissions
  109. @roles = Role.where(:id => params[:permissions].keys)
  110. @roles.each do |role|
  111. role.permissions = params[:permissions][role.id.to_s]
  112. role.save
  113. end
  114. flash[:notice] = l(:notice_successful_update)
  115. redirect_to roles_path
  116. end
  117. private
  118. def find_role
  119. @role = Role.find(params[:id])
  120. rescue ActiveRecord::RecordNotFound
  121. render_404
  122. end
  123. end