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