annotate app/models/.svn/text-base/role.rb.svn-base @ 507:0c939c159af4 redmine-1.2

Update to Redmine 1.2.1 on 1.2-stable branch (Redmine SVN rev 6270)
author Chris Cannam
date Thu, 14 Jul 2011 10:32:19 +0100
parents cbce1fd3b1b7
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@0 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@0 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@0 19 # Built-in roles
Chris@0 20 BUILTIN_NON_MEMBER = 1
Chris@0 21 BUILTIN_ANONYMOUS = 2
Chris@441 22
Chris@441 23 ISSUES_VISIBILITY_OPTIONS = [
Chris@441 24 ['all', :label_issues_visibility_all],
Chris@441 25 ['default', :label_issues_visibility_public],
Chris@441 26 ['own', :label_issues_visibility_own]
Chris@441 27 ]
Chris@0 28
Chris@0 29 named_scope :givable, { :conditions => "builtin = 0", :order => 'position' }
Chris@0 30 named_scope :builtin, lambda { |*args|
Chris@0 31 compare = 'not' if args.first == true
Chris@0 32 { :conditions => "#{compare} builtin = 0" }
Chris@0 33 }
Chris@0 34
Chris@0 35 before_destroy :check_deletable
Chris@0 36 has_many :workflows, :dependent => :delete_all do
Chris@0 37 def copy(source_role)
Chris@0 38 Workflow.copy(nil, source_role, nil, proxy_owner)
Chris@0 39 end
Chris@0 40 end
Chris@0 41
Chris@0 42 has_many :member_roles, :dependent => :destroy
Chris@0 43 has_many :members, :through => :member_roles
Chris@0 44 acts_as_list
Chris@0 45
Chris@0 46 serialize :permissions, Array
Chris@0 47 attr_protected :builtin
Chris@0 48
Chris@0 49 validates_presence_of :name
Chris@0 50 validates_uniqueness_of :name
Chris@0 51 validates_length_of :name, :maximum => 30
Chris@441 52 validates_inclusion_of :issues_visibility,
Chris@441 53 :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
Chris@441 54 :if => lambda {|role| role.respond_to?(:issues_visibility)}
Chris@441 55
Chris@0 56 def permissions
Chris@0 57 read_attribute(:permissions) || []
Chris@0 58 end
Chris@0 59
Chris@0 60 def permissions=(perms)
Chris@0 61 perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
Chris@0 62 write_attribute(:permissions, perms)
Chris@0 63 end
Chris@0 64
Chris@0 65 def add_permission!(*perms)
Chris@0 66 self.permissions = [] unless permissions.is_a?(Array)
Chris@0 67
Chris@0 68 permissions_will_change!
Chris@0 69 perms.each do |p|
Chris@0 70 p = p.to_sym
Chris@0 71 permissions << p unless permissions.include?(p)
Chris@0 72 end
Chris@0 73 save!
Chris@0 74 end
Chris@0 75
Chris@0 76 def remove_permission!(*perms)
Chris@0 77 return unless permissions.is_a?(Array)
Chris@0 78 permissions_will_change!
Chris@0 79 perms.each { |p| permissions.delete(p.to_sym) }
Chris@0 80 save!
Chris@0 81 end
Chris@0 82
Chris@0 83 # Returns true if the role has the given permission
Chris@0 84 def has_permission?(perm)
Chris@0 85 !permissions.nil? && permissions.include?(perm.to_sym)
Chris@0 86 end
Chris@0 87
Chris@0 88 def <=>(role)
Chris@0 89 role ? position <=> role.position : -1
Chris@0 90 end
Chris@0 91
Chris@0 92 def to_s
Chris@0 93 name
Chris@0 94 end
Chris@0 95
Chris@441 96 def name
Chris@441 97 case builtin
Chris@441 98 when 1; l(:label_role_non_member, :default => read_attribute(:name))
Chris@441 99 when 2; l(:label_role_anonymous, :default => read_attribute(:name))
Chris@441 100 else; read_attribute(:name)
Chris@441 101 end
Chris@441 102 end
Chris@441 103
Chris@0 104 # Return true if the role is a builtin role
Chris@0 105 def builtin?
Chris@0 106 self.builtin != 0
Chris@0 107 end
Chris@0 108
Chris@0 109 # Return true if the role is a project member role
Chris@0 110 def member?
Chris@0 111 !self.builtin?
Chris@0 112 end
Chris@0 113
Chris@0 114 # Return true if role is allowed to do the specified action
Chris@0 115 # action can be:
Chris@0 116 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
Chris@0 117 # * a permission Symbol (eg. :edit_project)
Chris@0 118 def allowed_to?(action)
Chris@0 119 if action.is_a? Hash
Chris@0 120 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
Chris@0 121 else
Chris@0 122 allowed_permissions.include? action
Chris@0 123 end
Chris@0 124 end
Chris@0 125
Chris@0 126 # Return all the permissions that can be given to the role
Chris@0 127 def setable_permissions
Chris@0 128 setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
Chris@0 129 setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
Chris@0 130 setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
Chris@0 131 setable_permissions
Chris@0 132 end
Chris@0 133
Chris@0 134 # Find all the roles that can be given to a project member
Chris@0 135 def self.find_all_givable
Chris@0 136 find(:all, :conditions => {:builtin => 0}, :order => 'position')
Chris@0 137 end
Chris@0 138
Chris@0 139 # Return the builtin 'non member' role. If the role doesn't exist,
Chris@0 140 # it will be created on the fly.
Chris@0 141 def self.non_member
Chris@0 142 non_member_role = find(:first, :conditions => {:builtin => BUILTIN_NON_MEMBER})
Chris@0 143 if non_member_role.nil?
Chris@0 144 non_member_role = create(:name => 'Non member', :position => 0) do |role|
Chris@0 145 role.builtin = BUILTIN_NON_MEMBER
Chris@0 146 end
Chris@0 147 raise 'Unable to create the non-member role.' if non_member_role.new_record?
Chris@0 148 end
Chris@0 149 non_member_role
Chris@0 150 end
Chris@0 151
Chris@0 152 # Return the builtin 'anonymous' role. If the role doesn't exist,
Chris@0 153 # it will be created on the fly.
Chris@0 154 def self.anonymous
Chris@0 155 anonymous_role = find(:first, :conditions => {:builtin => BUILTIN_ANONYMOUS})
Chris@0 156 if anonymous_role.nil?
Chris@0 157 anonymous_role = create(:name => 'Anonymous', :position => 0) do |role|
Chris@0 158 role.builtin = BUILTIN_ANONYMOUS
Chris@0 159 end
Chris@0 160 raise 'Unable to create the anonymous role.' if anonymous_role.new_record?
Chris@0 161 end
Chris@0 162 anonymous_role
Chris@0 163 end
Chris@0 164
Chris@0 165
Chris@0 166 private
Chris@0 167 def allowed_permissions
Chris@0 168 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
Chris@0 169 end
Chris@0 170
Chris@0 171 def allowed_actions
Chris@0 172 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
Chris@0 173 end
Chris@0 174
Chris@0 175 def check_deletable
Chris@0 176 raise "Can't delete role" if members.any?
Chris@0 177 raise "Can't delete builtin role" if builtin?
Chris@0 178 end
Chris@0 179 end