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