annotate app/models/role.rb @ 1327:287f201c2802 redmine-2.2-integration

Add italic
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Wed, 19 Jun 2013 20:56:22 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 class Role < ActiveRecord::Base
Chris@1115 19 # Custom coder for the permissions attribute that should be an
Chris@1115 20 # array of symbols. Rails 3 uses Psych which can be *unbelievably*
Chris@1115 21 # slow on some platforms (eg. mingw32).
Chris@1115 22 class PermissionsAttributeCoder
Chris@1115 23 def self.load(str)
Chris@1115 24 str.to_s.scan(/:([a-z0-9_]+)/).flatten.map(&:to_sym)
Chris@1115 25 end
Chris@1115 26
Chris@1115 27 def self.dump(value)
Chris@1115 28 YAML.dump(value)
Chris@1115 29 end
Chris@1115 30 end
Chris@1115 31
Chris@0 32 # Built-in roles
Chris@0 33 BUILTIN_NON_MEMBER = 1
Chris@0 34 BUILTIN_ANONYMOUS = 2
Chris@909 35
Chris@441 36 ISSUES_VISIBILITY_OPTIONS = [
Chris@441 37 ['all', :label_issues_visibility_all],
Chris@441 38 ['default', :label_issues_visibility_public],
Chris@441 39 ['own', :label_issues_visibility_own]
Chris@441 40 ]
Chris@0 41
Chris@1115 42 scope :sorted, order("#{table_name}.builtin ASC, #{table_name}.position ASC")
Chris@1115 43 scope :givable, order("#{table_name}.position ASC").where(:builtin => 0)
Chris@1115 44 scope :builtin, lambda { |*args|
Chris@1115 45 compare = (args.first == true ? 'not' : '')
Chris@1115 46 where("#{compare} builtin = 0")
Chris@0 47 }
Chris@909 48
Chris@0 49 before_destroy :check_deletable
Chris@1115 50 has_many :workflow_rules, :dependent => :delete_all do
Chris@0 51 def copy(source_role)
Chris@1115 52 WorkflowRule.copy(nil, source_role, nil, proxy_association.owner)
Chris@0 53 end
Chris@0 54 end
Chris@909 55
Chris@0 56 has_many :member_roles, :dependent => :destroy
Chris@0 57 has_many :members, :through => :member_roles
Chris@0 58 acts_as_list
Chris@909 59
Chris@1115 60 serialize :permissions, ::Role::PermissionsAttributeCoder
Chris@0 61 attr_protected :builtin
Chris@0 62
Chris@0 63 validates_presence_of :name
Chris@0 64 validates_uniqueness_of :name
Chris@0 65 validates_length_of :name, :maximum => 30
Chris@441 66 validates_inclusion_of :issues_visibility,
Chris@441 67 :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
Chris@441 68 :if => lambda {|role| role.respond_to?(:issues_visibility)}
Chris@909 69
Chris@1115 70 # Copies attributes from another role, arg can be an id or a Role
Chris@1115 71 def copy_from(arg, options={})
Chris@1115 72 return unless arg.present?
Chris@1115 73 role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s)
Chris@1115 74 self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions")
Chris@1115 75 self.permissions = role.permissions.dup
Chris@1115 76 self
Chris@0 77 end
Chris@909 78
Chris@0 79 def permissions=(perms)
Chris@0 80 perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
Chris@0 81 write_attribute(:permissions, perms)
Chris@0 82 end
Chris@0 83
Chris@0 84 def add_permission!(*perms)
Chris@0 85 self.permissions = [] unless permissions.is_a?(Array)
Chris@0 86
Chris@0 87 permissions_will_change!
Chris@0 88 perms.each do |p|
Chris@0 89 p = p.to_sym
Chris@0 90 permissions << p unless permissions.include?(p)
Chris@0 91 end
Chris@0 92 save!
Chris@0 93 end
Chris@0 94
Chris@0 95 def remove_permission!(*perms)
Chris@0 96 return unless permissions.is_a?(Array)
Chris@0 97 permissions_will_change!
Chris@0 98 perms.each { |p| permissions.delete(p.to_sym) }
Chris@0 99 save!
Chris@0 100 end
Chris@909 101
Chris@0 102 # Returns true if the role has the given permission
Chris@0 103 def has_permission?(perm)
Chris@0 104 !permissions.nil? && permissions.include?(perm.to_sym)
Chris@0 105 end
Chris@909 106
Chris@0 107 def <=>(role)
Chris@1115 108 if role
Chris@1115 109 if builtin == role.builtin
Chris@1115 110 position <=> role.position
Chris@1115 111 else
Chris@1115 112 builtin <=> role.builtin
Chris@1115 113 end
Chris@1115 114 else
Chris@1115 115 -1
Chris@1115 116 end
Chris@0 117 end
Chris@909 118
Chris@0 119 def to_s
Chris@0 120 name
Chris@0 121 end
Chris@909 122
Chris@441 123 def name
Chris@441 124 case builtin
Chris@441 125 when 1; l(:label_role_non_member, :default => read_attribute(:name))
Chris@441 126 when 2; l(:label_role_anonymous, :default => read_attribute(:name))
Chris@441 127 else; read_attribute(:name)
Chris@441 128 end
Chris@441 129 end
Chris@909 130
Chris@0 131 # Return true if the role is a builtin role
Chris@0 132 def builtin?
Chris@0 133 self.builtin != 0
Chris@0 134 end
Chris@909 135
Chris@1115 136 # Return true if the role is the anonymous role
Chris@1115 137 def anonymous?
Chris@1115 138 builtin == 2
Chris@1115 139 end
Chris@1115 140
Chris@0 141 # Return true if the role is a project member role
Chris@0 142 def member?
Chris@0 143 !self.builtin?
Chris@0 144 end
Chris@909 145
Chris@0 146 # Return true if role is allowed to do the specified action
Chris@0 147 # action can be:
Chris@0 148 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
Chris@0 149 # * a permission Symbol (eg. :edit_project)
Chris@0 150 def allowed_to?(action)
Chris@0 151 if action.is_a? Hash
Chris@0 152 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
Chris@0 153 else
Chris@0 154 allowed_permissions.include? action
Chris@0 155 end
Chris@0 156 end
Chris@909 157
Chris@0 158 # Return all the permissions that can be given to the role
Chris@0 159 def setable_permissions
Chris@0 160 setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
Chris@0 161 setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
Chris@0 162 setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
Chris@0 163 setable_permissions
Chris@0 164 end
Chris@0 165
Chris@0 166 # Find all the roles that can be given to a project member
Chris@0 167 def self.find_all_givable
Chris@1115 168 Role.givable.all
Chris@0 169 end
Chris@0 170
Chris@0 171 # Return the builtin 'non member' role. If the role doesn't exist,
Chris@0 172 # it will be created on the fly.
Chris@0 173 def self.non_member
Chris@909 174 find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member')
Chris@0 175 end
Chris@0 176
Chris@0 177 # Return the builtin 'anonymous' role. If the role doesn't exist,
Chris@0 178 # it will be created on the fly.
Chris@0 179 def self.anonymous
Chris@909 180 find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous')
Chris@0 181 end
Chris@0 182
Chris@0 183 private
Chris@909 184
Chris@0 185 def allowed_permissions
Chris@0 186 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
Chris@0 187 end
Chris@0 188
Chris@0 189 def allowed_actions
Chris@0 190 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
Chris@0 191 end
Chris@909 192
Chris@0 193 def check_deletable
Chris@0 194 raise "Can't delete role" if members.any?
Chris@0 195 raise "Can't delete builtin role" if builtin?
Chris@0 196 end
Chris@909 197
Chris@909 198 def self.find_or_create_system_role(builtin, name)
Chris@1115 199 role = where(:builtin => builtin).first
Chris@909 200 if role.nil?
Chris@909 201 role = create(:name => name, :position => 0) do |r|
Chris@909 202 r.builtin = builtin
Chris@909 203 end
Chris@909 204 raise "Unable to create the #{name} role." if role.new_record?
Chris@909 205 end
Chris@909 206 role
Chris@909 207 end
Chris@0 208 end