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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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. require_sudo_mode :create, :update, :destroy
  26. def index
  27. respond_to do |format|
  28. format.html {
  29. @roles = Role.sorted.to_a
  30. render :layout => false if request.xhr?
  31. }
  32. format.api {
  33. @roles = Role.givable.to_a
  34. }
  35. end
  36. end
  37. def show
  38. respond_to do |format|
  39. format.api
  40. end
  41. end
  42. def new
  43. # Prefills the form with 'Non member' role permissions by default
  44. @role = Role.new
  45. @role.safe_attributes = params[:role] || {:permissions => Role.non_member.permissions}
  46. if params[:copy].present? && @copy_from = Role.find_by_id(params[:copy])
  47. @role.copy_from(@copy_from)
  48. end
  49. @roles = Role.sorted.to_a
  50. end
  51. def create
  52. @role = Role.new
  53. @role.safe_attributes = params[:role]
  54. if request.post? && @role.save
  55. # workflow copy
  56. if !params[:copy_workflow_from].blank? && (copy_from = Role.find_by_id(params[:copy_workflow_from]))
  57. @role.copy_workflow_rules(copy_from)
  58. end
  59. flash[:notice] = l(:notice_successful_create)
  60. redirect_to roles_path
  61. else
  62. @roles = Role.sorted.to_a
  63. render :action => 'new'
  64. end
  65. end
  66. def edit
  67. end
  68. def update
  69. @role.safe_attributes = params[:role]
  70. if @role.save
  71. respond_to do |format|
  72. format.html {
  73. flash[:notice] = l(:notice_successful_update)
  74. redirect_to roles_path(:page => params[:page])
  75. }
  76. format.js { head 200 }
  77. end
  78. else
  79. respond_to do |format|
  80. format.html { render :action => 'edit' }
  81. format.js { head 422 }
  82. end
  83. end
  84. end
  85. def destroy
  86. begin
  87. @role.destroy
  88. rescue
  89. flash[:error] = l(:error_can_not_remove_role)
  90. end
  91. redirect_to roles_path
  92. end
  93. def permissions
  94. @roles = Role.sorted.to_a
  95. @permissions = Redmine::AccessControl.permissions.select { |p| !p.public? }
  96. if request.post?
  97. @roles.each do |role|
  98. role.permissions = params[:permissions][role.id.to_s]
  99. role.save
  100. end
  101. flash[:notice] = l(:notice_successful_update)
  102. redirect_to roles_path
  103. end
  104. end
  105. private
  106. def find_role
  107. @role = Role.find(params[:id])
  108. rescue ActiveRecord::RecordNotFound
  109. render_404
  110. end
  111. end