annotate .svn/pristine/d3/d36525152d9efe5f33a9df496757a5193831f75b.svn-base @ 1494:e248c7af89ec redmine-2.4

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