annotate app/models/role.rb @ 1524:82fac3dcf466 redmine-2.5-integration

Fix failure to interpret Javascript when autocompleting members for project
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Thu, 11 Sep 2014 10:24:38 +0100
parents e248c7af89ec
children
rev   line source
Chris@441 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@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@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@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@909 56
Chris@0 57 has_many :member_roles, :dependent => :destroy
Chris@0 58 has_many :members, :through => :member_roles
Chris@0 59 acts_as_list
Chris@909 60
Chris@1115 61 serialize :permissions, ::Role::PermissionsAttributeCoder
Chris@0 62 attr_protected :builtin
Chris@0 63
Chris@0 64 validates_presence_of :name
Chris@0 65 validates_uniqueness_of :name
Chris@0 66 validates_length_of :name, :maximum => 30
Chris@441 67 validates_inclusion_of :issues_visibility,
Chris@441 68 :in => ISSUES_VISIBILITY_OPTIONS.collect(&:first),
Chris@441 69 :if => lambda {|role| role.respond_to?(:issues_visibility)}
Chris@909 70
Chris@1115 71 # Copies attributes from another role, arg can be an id or a Role
Chris@1115 72 def copy_from(arg, options={})
Chris@1115 73 return unless arg.present?
Chris@1115 74 role = arg.is_a?(Role) ? arg : Role.find_by_id(arg.to_s)
Chris@1115 75 self.attributes = role.attributes.dup.except("id", "name", "position", "builtin", "permissions")
Chris@1115 76 self.permissions = role.permissions.dup
Chris@1115 77 self
Chris@0 78 end
Chris@909 79
Chris@0 80 def permissions=(perms)
Chris@0 81 perms = perms.collect {|p| p.to_sym unless p.blank? }.compact.uniq if perms
Chris@0 82 write_attribute(:permissions, perms)
Chris@0 83 end
Chris@0 84
Chris@0 85 def add_permission!(*perms)
Chris@0 86 self.permissions = [] unless permissions.is_a?(Array)
Chris@0 87
Chris@0 88 permissions_will_change!
Chris@0 89 perms.each do |p|
Chris@0 90 p = p.to_sym
Chris@0 91 permissions << p unless permissions.include?(p)
Chris@0 92 end
Chris@0 93 save!
Chris@0 94 end
Chris@0 95
Chris@0 96 def remove_permission!(*perms)
Chris@0 97 return unless permissions.is_a?(Array)
Chris@0 98 permissions_will_change!
Chris@0 99 perms.each { |p| permissions.delete(p.to_sym) }
Chris@0 100 save!
Chris@0 101 end
Chris@909 102
Chris@0 103 # Returns true if the role has the given permission
Chris@0 104 def has_permission?(perm)
Chris@0 105 !permissions.nil? && permissions.include?(perm.to_sym)
Chris@0 106 end
Chris@909 107
Chris@0 108 def <=>(role)
Chris@1115 109 if role
Chris@1115 110 if builtin == role.builtin
Chris@1115 111 position <=> role.position
Chris@1115 112 else
Chris@1115 113 builtin <=> role.builtin
Chris@1115 114 end
Chris@1115 115 else
Chris@1115 116 -1
Chris@1115 117 end
Chris@0 118 end
Chris@909 119
Chris@0 120 def to_s
Chris@0 121 name
Chris@0 122 end
Chris@909 123
Chris@441 124 def name
Chris@441 125 case builtin
Chris@441 126 when 1; l(:label_role_non_member, :default => read_attribute(:name))
Chris@441 127 when 2; l(:label_role_anonymous, :default => read_attribute(:name))
Chris@441 128 else; read_attribute(:name)
Chris@441 129 end
Chris@441 130 end
Chris@909 131
Chris@0 132 # Return true if the role is a builtin role
Chris@0 133 def builtin?
Chris@0 134 self.builtin != 0
Chris@0 135 end
Chris@909 136
Chris@1115 137 # Return true if the role is the anonymous role
Chris@1115 138 def anonymous?
Chris@1115 139 builtin == 2
Chris@1115 140 end
Chris@1464 141
Chris@0 142 # Return true if the role is a project member role
Chris@0 143 def member?
Chris@0 144 !self.builtin?
Chris@0 145 end
Chris@909 146
Chris@0 147 # Return true if role is allowed to do the specified action
Chris@0 148 # action can be:
Chris@0 149 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
Chris@0 150 # * a permission Symbol (eg. :edit_project)
Chris@0 151 def allowed_to?(action)
Chris@0 152 if action.is_a? Hash
Chris@0 153 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
Chris@0 154 else
Chris@0 155 allowed_permissions.include? action
Chris@0 156 end
Chris@0 157 end
Chris@909 158
Chris@0 159 # Return all the permissions that can be given to the role
Chris@0 160 def setable_permissions
Chris@0 161 setable_permissions = Redmine::AccessControl.permissions - Redmine::AccessControl.public_permissions
Chris@0 162 setable_permissions -= Redmine::AccessControl.members_only_permissions if self.builtin == BUILTIN_NON_MEMBER
Chris@0 163 setable_permissions -= Redmine::AccessControl.loggedin_only_permissions if self.builtin == BUILTIN_ANONYMOUS
Chris@0 164 setable_permissions
Chris@0 165 end
Chris@0 166
Chris@0 167 # Find all the roles that can be given to a project member
Chris@0 168 def self.find_all_givable
Chris@1115 169 Role.givable.all
Chris@0 170 end
Chris@0 171
Chris@0 172 # Return the builtin 'non member' role. If the role doesn't exist,
Chris@0 173 # it will be created on the fly.
Chris@0 174 def self.non_member
Chris@909 175 find_or_create_system_role(BUILTIN_NON_MEMBER, 'Non member')
Chris@0 176 end
Chris@0 177
Chris@0 178 # Return the builtin 'anonymous' role. If the role doesn't exist,
Chris@0 179 # it will be created on the fly.
Chris@0 180 def self.anonymous
Chris@909 181 find_or_create_system_role(BUILTIN_ANONYMOUS, 'Anonymous')
Chris@0 182 end
Chris@0 183
Chris@0 184 private
Chris@909 185
Chris@0 186 def allowed_permissions
Chris@0 187 @allowed_permissions ||= permissions + Redmine::AccessControl.public_permissions.collect {|p| p.name}
Chris@0 188 end
Chris@0 189
Chris@0 190 def allowed_actions
Chris@0 191 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
Chris@0 192 end
Chris@909 193
Chris@0 194 def check_deletable
Chris@0 195 raise "Can't delete role" if members.any?
Chris@0 196 raise "Can't delete builtin role" if builtin?
Chris@0 197 end
Chris@909 198
Chris@909 199 def self.find_or_create_system_role(builtin, name)
Chris@1115 200 role = where(:builtin => builtin).first
Chris@909 201 if role.nil?
Chris@909 202 role = create(:name => name, :position => 0) do |r|
Chris@909 203 r.builtin = builtin
Chris@909 204 end
Chris@909 205 raise "Unable to create the #{name} role." if role.new_record?
Chris@909 206 end
Chris@909 207 role
Chris@909 208 end
Chris@0 209 end