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