annotate .svn/pristine/f1/f11f6b57f7e2388c7777d4dc34576404ddd5230b.svn-base @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents
children
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 module Redmine
Chris@1295 19 module AccessControl
Chris@1295 20
Chris@1295 21 class << self
Chris@1295 22 def map
Chris@1295 23 mapper = Mapper.new
Chris@1295 24 yield mapper
Chris@1295 25 @permissions ||= []
Chris@1295 26 @permissions += mapper.mapped_permissions
Chris@1295 27 end
Chris@1295 28
Chris@1295 29 def permissions
Chris@1295 30 @permissions
Chris@1295 31 end
Chris@1295 32
Chris@1295 33 # Returns the permission of given name or nil if it wasn't found
Chris@1295 34 # Argument should be a symbol
Chris@1295 35 def permission(name)
Chris@1295 36 permissions.detect {|p| p.name == name}
Chris@1295 37 end
Chris@1295 38
Chris@1295 39 # Returns the actions that are allowed by the permission of given name
Chris@1295 40 def allowed_actions(permission_name)
Chris@1295 41 perm = permission(permission_name)
Chris@1295 42 perm ? perm.actions : []
Chris@1295 43 end
Chris@1295 44
Chris@1295 45 def public_permissions
Chris@1295 46 @public_permissions ||= @permissions.select {|p| p.public?}
Chris@1295 47 end
Chris@1295 48
Chris@1295 49 def members_only_permissions
Chris@1295 50 @members_only_permissions ||= @permissions.select {|p| p.require_member?}
Chris@1295 51 end
Chris@1295 52
Chris@1295 53 def loggedin_only_permissions
Chris@1295 54 @loggedin_only_permissions ||= @permissions.select {|p| p.require_loggedin?}
Chris@1295 55 end
Chris@1295 56
Chris@1295 57 def read_action?(action)
Chris@1295 58 if action.is_a?(Symbol)
Chris@1295 59 perm = permission(action)
Chris@1295 60 !perm.nil? && perm.read?
Chris@1295 61 else
Chris@1295 62 s = "#{action[:controller]}/#{action[:action]}"
Chris@1295 63 permissions.detect {|p| p.actions.include?(s) && !p.read?}.nil?
Chris@1295 64 end
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 def available_project_modules
Chris@1295 68 @available_project_modules ||= @permissions.collect(&:project_module).uniq.compact
Chris@1295 69 end
Chris@1295 70
Chris@1295 71 def modules_permissions(modules)
Chris@1295 72 @permissions.select {|p| p.project_module.nil? || modules.include?(p.project_module.to_s)}
Chris@1295 73 end
Chris@1295 74 end
Chris@1295 75
Chris@1295 76 class Mapper
Chris@1295 77 def initialize
Chris@1295 78 @project_module = nil
Chris@1295 79 end
Chris@1295 80
Chris@1295 81 def permission(name, hash, options={})
Chris@1295 82 @permissions ||= []
Chris@1295 83 options.merge!(:project_module => @project_module)
Chris@1295 84 @permissions << Permission.new(name, hash, options)
Chris@1295 85 end
Chris@1295 86
Chris@1295 87 def project_module(name, options={})
Chris@1295 88 @project_module = name
Chris@1295 89 yield self
Chris@1295 90 @project_module = nil
Chris@1295 91 end
Chris@1295 92
Chris@1295 93 def mapped_permissions
Chris@1295 94 @permissions
Chris@1295 95 end
Chris@1295 96 end
Chris@1295 97
Chris@1295 98 class Permission
Chris@1295 99 attr_reader :name, :actions, :project_module
Chris@1295 100
Chris@1295 101 def initialize(name, hash, options)
Chris@1295 102 @name = name
Chris@1295 103 @actions = []
Chris@1295 104 @public = options[:public] || false
Chris@1295 105 @require = options[:require]
Chris@1295 106 @read = options[:read] || false
Chris@1295 107 @project_module = options[:project_module]
Chris@1295 108 hash.each do |controller, actions|
Chris@1295 109 if actions.is_a? Array
Chris@1295 110 @actions << actions.collect {|action| "#{controller}/#{action}"}
Chris@1295 111 else
Chris@1295 112 @actions << "#{controller}/#{actions}"
Chris@1295 113 end
Chris@1295 114 end
Chris@1295 115 @actions.flatten!
Chris@1295 116 end
Chris@1295 117
Chris@1295 118 def public?
Chris@1295 119 @public
Chris@1295 120 end
Chris@1295 121
Chris@1295 122 def require_member?
Chris@1295 123 @require && @require == :member
Chris@1295 124 end
Chris@1295 125
Chris@1295 126 def require_loggedin?
Chris@1295 127 @require && (@require == :member || @require == :loggedin)
Chris@1295 128 end
Chris@1295 129
Chris@1295 130 def read?
Chris@1295 131 @read
Chris@1295 132 end
Chris@1295 133 end
Chris@1295 134 end
Chris@1295 135 end