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