annotate .svn/pristine/55/5599405356ee2efcb93c9803fa4189ef3dece11e.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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