選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

access_control.rb 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. module Redmine
  19. module AccessControl
  20. class << self
  21. def map
  22. mapper = Mapper.new
  23. yield mapper
  24. @permissions ||= []
  25. @permissions += mapper.mapped_permissions
  26. end
  27. def permissions
  28. @permissions
  29. end
  30. # Returns the permission of given name or nil if it wasn't found
  31. # Argument should be a symbol
  32. def permission(name)
  33. permissions.detect {|p| p.name == name}
  34. end
  35. # Returns the actions that are allowed by the permission of given name
  36. def allowed_actions(permission_name)
  37. perm = permission(permission_name)
  38. perm ? perm.actions : []
  39. end
  40. def public_permissions
  41. @public_permissions ||= @permissions.select {|p| p.public?}
  42. end
  43. def members_only_permissions
  44. @members_only_permissions ||= @permissions.select {|p| p.require_member?}
  45. end
  46. def loggedin_only_permissions
  47. @loggedin_only_permissions ||= @permissions.select {|p| p.require_loggedin?}
  48. end
  49. def read_action?(action)
  50. if action.is_a?(Symbol)
  51. perm = permission(action)
  52. !perm.nil? && perm.read?
  53. elsif action.is_a?(Hash)
  54. s = "#{action[:controller]}/#{action[:action]}"
  55. permissions.detect {|p| p.actions.include?(s) && p.read?}.present?
  56. else
  57. raise ArgumentError.new("Symbol or a Hash expected, #{action.class.name} given: #{action}")
  58. end
  59. end
  60. def available_project_modules
  61. @available_project_modules ||= @permissions.collect(&:project_module).uniq.compact
  62. end
  63. def modules_permissions(modules)
  64. @permissions.select {|p| p.project_module.nil? || modules.include?(p.project_module.to_s)}
  65. end
  66. end
  67. class Mapper
  68. def initialize
  69. @project_module = nil
  70. end
  71. def permission(name, hash, options={})
  72. @permissions ||= []
  73. options.merge!(:project_module => @project_module)
  74. @permissions << Permission.new(name, hash, options)
  75. end
  76. def project_module(name, options={})
  77. @project_module = name
  78. yield self
  79. @project_module = nil
  80. end
  81. def mapped_permissions
  82. @permissions
  83. end
  84. end
  85. class Permission
  86. attr_reader :name, :actions, :project_module
  87. def initialize(name, hash, options)
  88. @name = name
  89. @actions = []
  90. @public = options[:public] || false
  91. @require = options[:require]
  92. @read = options[:read] || false
  93. @project_module = options[:project_module]
  94. hash.each do |controller, actions|
  95. if actions.is_a? Array
  96. @actions << actions.collect {|action| "#{controller}/#{action}"}
  97. else
  98. @actions << "#{controller}/#{actions}"
  99. end
  100. end
  101. @actions.flatten!
  102. end
  103. def public?
  104. @public
  105. end
  106. def require_member?
  107. @require && @require == :member
  108. end
  109. def require_loggedin?
  110. @require && (@require == :member || @require == :loggedin)
  111. end
  112. def read?
  113. @read
  114. end
  115. end
  116. end
  117. end