Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: module Redmine Chris@909: module AccessControl Chris@909: Chris@909: class << self Chris@909: def map Chris@909: mapper = Mapper.new Chris@909: yield mapper Chris@909: @permissions ||= [] Chris@909: @permissions += mapper.mapped_permissions Chris@909: end Chris@909: Chris@909: def permissions Chris@909: @permissions Chris@909: end Chris@909: Chris@909: # Returns the permission of given name or nil if it wasn't found Chris@909: # Argument should be a symbol Chris@909: def permission(name) Chris@909: permissions.detect {|p| p.name == name} Chris@909: end Chris@909: Chris@909: # Returns the actions that are allowed by the permission of given name Chris@909: def allowed_actions(permission_name) Chris@909: perm = permission(permission_name) Chris@909: perm ? perm.actions : [] Chris@909: end Chris@909: Chris@909: def public_permissions Chris@909: @public_permissions ||= @permissions.select {|p| p.public?} Chris@909: end Chris@909: Chris@909: def members_only_permissions Chris@909: @members_only_permissions ||= @permissions.select {|p| p.require_member?} Chris@909: end Chris@909: Chris@909: def loggedin_only_permissions Chris@909: @loggedin_only_permissions ||= @permissions.select {|p| p.require_loggedin?} Chris@909: end Chris@909: Chris@909: def available_project_modules Chris@909: @available_project_modules ||= @permissions.collect(&:project_module).uniq.compact Chris@909: end Chris@909: Chris@909: def modules_permissions(modules) Chris@909: @permissions.select {|p| p.project_module.nil? || modules.include?(p.project_module.to_s)} Chris@909: end Chris@909: end Chris@909: Chris@909: class Mapper Chris@909: def initialize Chris@909: @project_module = nil Chris@909: end Chris@909: Chris@909: def permission(name, hash, options={}) Chris@909: @permissions ||= [] Chris@909: options.merge!(:project_module => @project_module) Chris@909: @permissions << Permission.new(name, hash, options) Chris@909: end Chris@909: Chris@909: def project_module(name, options={}) Chris@909: @project_module = name Chris@909: yield self Chris@909: @project_module = nil Chris@909: end Chris@909: Chris@909: def mapped_permissions Chris@909: @permissions Chris@909: end Chris@909: end Chris@909: Chris@909: class Permission Chris@909: attr_reader :name, :actions, :project_module Chris@909: Chris@909: def initialize(name, hash, options) Chris@909: @name = name Chris@909: @actions = [] Chris@909: @public = options[:public] || false Chris@909: @require = options[:require] Chris@909: @project_module = options[:project_module] Chris@909: hash.each do |controller, actions| Chris@909: if actions.is_a? Array Chris@909: @actions << actions.collect {|action| "#{controller}/#{action}"} Chris@909: else Chris@909: @actions << "#{controller}/#{actions}" Chris@909: end Chris@909: end Chris@909: @actions.flatten! Chris@909: end Chris@909: Chris@909: def public? Chris@909: @public Chris@909: end Chris@909: Chris@909: def require_member? Chris@909: @require && @require == :member Chris@909: end Chris@909: Chris@909: def require_loggedin? Chris@909: @require && (@require == :member || @require == :loggedin) Chris@909: end Chris@909: end Chris@909: end Chris@909: end