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.

access_control.rb 3.9KB

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