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

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