To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / .svn / text-base / user.rb.svn-base @ 441:cbce1fd3b1b7
History | View | Annotate | Download (19.9 KB)
| 1 | 0:513646585e45 | Chris | # Redmine - project management software |
|---|---|---|---|
| 2 | 441:cbce1fd3b1b7 | Chris | # Copyright (C) 2006-2011 Jean-Philippe Lang |
| 3 | 0:513646585e45 | Chris | # |
| 4 | # This program is free software; you can redistribute it and/or |
||
| 5 | # modify it under the terms of the GNU General Public License |
||
| 6 | # as published by the Free Software Foundation; either version 2 |
||
| 7 | # of the License, or (at your option) any later version. |
||
| 8 | # |
||
| 9 | # This program is distributed in the hope that it will be useful, |
||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 12 | # GNU General Public License for more details. |
||
| 13 | # |
||
| 14 | # You should have received a copy of the GNU General Public License |
||
| 15 | # along with this program; if not, write to the Free Software |
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
| 17 | |||
| 18 | require "digest/sha1" |
||
| 19 | |||
| 20 | class User < Principal |
||
| 21 | 119:8661b858af72 | Chris | include Redmine::SafeAttributes |
| 22 | |||
| 23 | 0:513646585e45 | Chris | # Account statuses |
| 24 | STATUS_ANONYMOUS = 0 |
||
| 25 | STATUS_ACTIVE = 1 |
||
| 26 | STATUS_REGISTERED = 2 |
||
| 27 | STATUS_LOCKED = 3 |
||
| 28 | |||
| 29 | USER_FORMATS = {
|
||
| 30 | :firstname_lastname => '#{firstname} #{lastname}',
|
||
| 31 | :firstname => '#{firstname}',
|
||
| 32 | :lastname_firstname => '#{lastname} #{firstname}',
|
||
| 33 | :lastname_coma_firstname => '#{lastname}, #{firstname}',
|
||
| 34 | :username => '#{login}'
|
||
| 35 | } |
||
| 36 | |||
| 37 | 37:94944d00e43c | chris | MAIL_NOTIFICATION_OPTIONS = [ |
| 38 | 119:8661b858af72 | Chris | ['all', :label_user_mail_option_all], |
| 39 | ['selected', :label_user_mail_option_selected], |
||
| 40 | ['only_my_events', :label_user_mail_option_only_my_events], |
||
| 41 | ['only_assigned', :label_user_mail_option_only_assigned], |
||
| 42 | ['only_owner', :label_user_mail_option_only_owner], |
||
| 43 | ['none', :label_user_mail_option_none] |
||
| 44 | ] |
||
| 45 | 37:94944d00e43c | chris | |
| 46 | 0:513646585e45 | Chris | has_and_belongs_to_many :groups, :after_add => Proc.new {|user, group| group.user_added(user)},
|
| 47 | :after_remove => Proc.new {|user, group| group.user_removed(user)}
|
||
| 48 | has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify |
||
| 49 | has_many :changesets, :dependent => :nullify |
||
| 50 | has_one :preference, :dependent => :destroy, :class_name => 'UserPreference' |
||
| 51 | 128:07fa8a8b56a8 | Chris | has_one :rss_token, :class_name => 'Token', :conditions => "action='feeds'" |
| 52 | has_one :api_token, :class_name => 'Token', :conditions => "action='api'" |
||
| 53 | 0:513646585e45 | Chris | belongs_to :auth_source |
| 54 | |||
| 55 | # Active non-anonymous users scope |
||
| 56 | named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
|
||
| 57 | |||
| 58 | acts_as_customizable |
||
| 59 | |||
| 60 | attr_accessor :password, :password_confirmation |
||
| 61 | attr_accessor :last_before_login_on |
||
| 62 | # Prevents unauthorized assignments |
||
| 63 | 119:8661b858af72 | Chris | attr_protected :login, :admin, :password, :password_confirmation, :hashed_password |
| 64 | 0:513646585e45 | Chris | |
| 65 | validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
|
||
| 66 | validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false
|
||
| 67 | validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false
|
||
| 68 | # Login must contain lettres, numbers, underscores only |
||
| 69 | validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i |
||
| 70 | validates_length_of :login, :maximum => 30 |
||
| 71 | validates_length_of :firstname, :lastname, :maximum => 30 |
||
| 72 | validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_nil => true
|
||
| 73 | validates_length_of :mail, :maximum => 60, :allow_nil => true |
||
| 74 | validates_confirmation_of :password, :allow_nil => true |
||
| 75 | 119:8661b858af72 | Chris | validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true |
| 76 | 0:513646585e45 | Chris | |
| 77 | 128:07fa8a8b56a8 | Chris | before_destroy :remove_references_before_destroy |
| 78 | |||
| 79 | 441:cbce1fd3b1b7 | Chris | named_scope :in_group, lambda {|group|
|
| 80 | group_id = group.is_a?(Group) ? group.id : group.to_i |
||
| 81 | { :conditions => ["#{User.table_name}.id IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
|
||
| 82 | } |
||
| 83 | named_scope :not_in_group, lambda {|group|
|
||
| 84 | group_id = group.is_a?(Group) ? group.id : group.to_i |
||
| 85 | { :conditions => ["#{User.table_name}.id NOT IN (SELECT gu.user_id FROM #{table_name_prefix}groups_users#{table_name_suffix} gu WHERE gu.group_id = ?)", group_id] }
|
||
| 86 | } |
||
| 87 | |||
| 88 | 0:513646585e45 | Chris | def before_create |
| 89 | 37:94944d00e43c | chris | self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? |
| 90 | 0:513646585e45 | Chris | true |
| 91 | end |
||
| 92 | |||
| 93 | def before_save |
||
| 94 | # update hashed_password if password was set |
||
| 95 | 245:051f544170fe | Chris | if self.password && self.auth_source_id.blank? |
| 96 | salt_password(password) |
||
| 97 | end |
||
| 98 | 0:513646585e45 | Chris | end |
| 99 | |||
| 100 | def reload(*args) |
||
| 101 | @name = nil |
||
| 102 | 441:cbce1fd3b1b7 | Chris | @projects_by_role = nil |
| 103 | 0:513646585e45 | Chris | super |
| 104 | end |
||
| 105 | |||
| 106 | 1:cca12e1c1fd4 | Chris | def mail=(arg) |
| 107 | write_attribute(:mail, arg.to_s.strip) |
||
| 108 | end |
||
| 109 | |||
| 110 | 0:513646585e45 | Chris | def identity_url=(url) |
| 111 | if url.blank? |
||
| 112 | write_attribute(:identity_url, '') |
||
| 113 | else |
||
| 114 | begin |
||
| 115 | write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) |
||
| 116 | rescue OpenIdAuthentication::InvalidOpenId |
||
| 117 | # Invlaid url, don't save |
||
| 118 | end |
||
| 119 | end |
||
| 120 | self.read_attribute(:identity_url) |
||
| 121 | end |
||
| 122 | |||
| 123 | # Returns the user that matches provided login and password, or nil |
||
| 124 | def self.try_to_login(login, password) |
||
| 125 | # Make sure no one can sign in with an empty password |
||
| 126 | return nil if password.to_s.empty? |
||
| 127 | user = find_by_login(login) |
||
| 128 | if user |
||
| 129 | # user is already in local database |
||
| 130 | return nil if !user.active? |
||
| 131 | if user.auth_source |
||
| 132 | # user has an external authentication method |
||
| 133 | return nil unless user.auth_source.authenticate(login, password) |
||
| 134 | else |
||
| 135 | # authentication with local password |
||
| 136 | 245:051f544170fe | Chris | return nil unless user.check_password?(password) |
| 137 | 0:513646585e45 | Chris | end |
| 138 | else |
||
| 139 | # user is not yet registered, try to authenticate with available sources |
||
| 140 | attrs = AuthSource.authenticate(login, password) |
||
| 141 | if attrs |
||
| 142 | user = new(attrs) |
||
| 143 | user.login = login |
||
| 144 | user.language = Setting.default_language |
||
| 145 | if user.save |
||
| 146 | user.reload |
||
| 147 | logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source
|
||
| 148 | end |
||
| 149 | end |
||
| 150 | end |
||
| 151 | user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? |
||
| 152 | user |
||
| 153 | rescue => text |
||
| 154 | raise text |
||
| 155 | end |
||
| 156 | |||
| 157 | # Returns the user who matches the given autologin +key+ or nil |
||
| 158 | def self.try_to_autologin(key) |
||
| 159 | tokens = Token.find_all_by_action_and_value('autologin', key)
|
||
| 160 | # Make sure there's only 1 token that matches the key |
||
| 161 | if tokens.size == 1 |
||
| 162 | token = tokens.first |
||
| 163 | if (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active? |
||
| 164 | token.user.update_attribute(:last_login_on, Time.now) |
||
| 165 | token.user |
||
| 166 | end |
||
| 167 | end |
||
| 168 | end |
||
| 169 | |||
| 170 | # Return user's full name for display |
||
| 171 | def name(formatter = nil) |
||
| 172 | if formatter |
||
| 173 | eval('"' + (USER_FORMATS[formatter] || USER_FORMATS[:firstname_lastname]) + '"')
|
||
| 174 | else |
||
| 175 | @name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"')
|
||
| 176 | end |
||
| 177 | end |
||
| 178 | |||
| 179 | def active? |
||
| 180 | self.status == STATUS_ACTIVE |
||
| 181 | end |
||
| 182 | |||
| 183 | def registered? |
||
| 184 | self.status == STATUS_REGISTERED |
||
| 185 | end |
||
| 186 | |||
| 187 | def locked? |
||
| 188 | self.status == STATUS_LOCKED |
||
| 189 | end |
||
| 190 | |||
| 191 | 14:1d32c0a0efbf | Chris | def activate |
| 192 | self.status = STATUS_ACTIVE |
||
| 193 | end |
||
| 194 | |||
| 195 | def register |
||
| 196 | self.status = STATUS_REGISTERED |
||
| 197 | end |
||
| 198 | |||
| 199 | def lock |
||
| 200 | self.status = STATUS_LOCKED |
||
| 201 | end |
||
| 202 | |||
| 203 | def activate! |
||
| 204 | update_attribute(:status, STATUS_ACTIVE) |
||
| 205 | end |
||
| 206 | |||
| 207 | def register! |
||
| 208 | update_attribute(:status, STATUS_REGISTERED) |
||
| 209 | end |
||
| 210 | |||
| 211 | def lock! |
||
| 212 | update_attribute(:status, STATUS_LOCKED) |
||
| 213 | end |
||
| 214 | |||
| 215 | 245:051f544170fe | Chris | # Returns true if +clear_password+ is the correct user's password, otherwise false |
| 216 | 0:513646585e45 | Chris | def check_password?(clear_password) |
| 217 | if auth_source_id.present? |
||
| 218 | auth_source.authenticate(self.login, clear_password) |
||
| 219 | else |
||
| 220 | 245:051f544170fe | Chris | User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password
|
| 221 | 0:513646585e45 | Chris | end |
| 222 | end |
||
| 223 | 245:051f544170fe | Chris | |
| 224 | # Generates a random salt and computes hashed_password for +clear_password+ |
||
| 225 | # The hashed password is stored in the following form: SHA1(salt + SHA1(password)) |
||
| 226 | def salt_password(clear_password) |
||
| 227 | self.salt = User.generate_salt |
||
| 228 | self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}")
|
||
| 229 | end |
||
| 230 | 0:513646585e45 | Chris | |
| 231 | # Does the backend storage allow this user to change their password? |
||
| 232 | def change_password_allowed? |
||
| 233 | return true if auth_source_id.blank? |
||
| 234 | return auth_source.allow_password_changes? |
||
| 235 | end |
||
| 236 | |||
| 237 | # Generate and set a random password. Useful for automated user creation |
||
| 238 | # Based on Token#generate_token_value |
||
| 239 | # |
||
| 240 | def random_password |
||
| 241 | chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
|
||
| 242 | password = '' |
||
| 243 | 40.times { |i| password << chars[rand(chars.size-1)] }
|
||
| 244 | self.password = password |
||
| 245 | self.password_confirmation = password |
||
| 246 | self |
||
| 247 | end |
||
| 248 | |||
| 249 | def pref |
||
| 250 | self.preference ||= UserPreference.new(:user => self) |
||
| 251 | end |
||
| 252 | |||
| 253 | def time_zone |
||
| 254 | @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) |
||
| 255 | end |
||
| 256 | |||
| 257 | def wants_comments_in_reverse_order? |
||
| 258 | self.pref[:comments_sorting] == 'desc' |
||
| 259 | end |
||
| 260 | |||
| 261 | # Return user's RSS key (a 40 chars long string), used to access feeds |
||
| 262 | def rss_key |
||
| 263 | token = self.rss_token || Token.create(:user => self, :action => 'feeds') |
||
| 264 | token.value |
||
| 265 | end |
||
| 266 | |||
| 267 | # Return user's API key (a 40 chars long string), used to access the API |
||
| 268 | def api_key |
||
| 269 | token = self.api_token || self.create_api_token(:action => 'api') |
||
| 270 | token.value |
||
| 271 | end |
||
| 272 | |||
| 273 | # Return an array of project ids for which the user has explicitly turned mail notifications on |
||
| 274 | def notified_projects_ids |
||
| 275 | @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id)
|
||
| 276 | end |
||
| 277 | |||
| 278 | def notified_project_ids=(ids) |
||
| 279 | Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id])
|
||
| 280 | Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty?
|
||
| 281 | @notified_projects_ids = nil |
||
| 282 | notified_projects_ids |
||
| 283 | end |
||
| 284 | |||
| 285 | 128:07fa8a8b56a8 | Chris | def valid_notification_options |
| 286 | self.class.valid_notification_options(self) |
||
| 287 | end |
||
| 288 | |||
| 289 | 37:94944d00e43c | chris | # Only users that belong to more than 1 project can select projects for which they are notified |
| 290 | 128:07fa8a8b56a8 | Chris | def self.valid_notification_options(user=nil) |
| 291 | 37:94944d00e43c | chris | # Note that @user.membership.size would fail since AR ignores |
| 292 | # :include association option when doing a count |
||
| 293 | 128:07fa8a8b56a8 | Chris | if user.nil? || user.memberships.length < 1 |
| 294 | MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'}
|
||
| 295 | 37:94944d00e43c | chris | else |
| 296 | MAIL_NOTIFICATION_OPTIONS |
||
| 297 | end |
||
| 298 | end |
||
| 299 | |||
| 300 | 0:513646585e45 | Chris | # Find a user account by matching the exact login and then a case-insensitive |
| 301 | # version. Exact matches will be given priority. |
||
| 302 | def self.find_by_login(login) |
||
| 303 | # force string comparison to be case sensitive on MySQL |
||
| 304 | type_cast = (ActiveRecord::Base.connection.adapter_name == 'MySQL') ? 'BINARY' : '' |
||
| 305 | |||
| 306 | # First look for an exact match |
||
| 307 | user = first(:conditions => ["#{type_cast} login = ?", login])
|
||
| 308 | # Fail over to case-insensitive if none was found |
||
| 309 | user ||= first(:conditions => ["#{type_cast} LOWER(login) = ?", login.to_s.downcase])
|
||
| 310 | end |
||
| 311 | |||
| 312 | def self.find_by_rss_key(key) |
||
| 313 | token = Token.find_by_value(key) |
||
| 314 | token && token.user.active? ? token.user : nil |
||
| 315 | end |
||
| 316 | |||
| 317 | def self.find_by_api_key(key) |
||
| 318 | token = Token.find_by_action_and_value('api', key)
|
||
| 319 | token && token.user.active? ? token.user : nil |
||
| 320 | end |
||
| 321 | |||
| 322 | # Makes find_by_mail case-insensitive |
||
| 323 | def self.find_by_mail(mail) |
||
| 324 | find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase]) |
||
| 325 | end |
||
| 326 | |||
| 327 | def to_s |
||
| 328 | name |
||
| 329 | end |
||
| 330 | |||
| 331 | # Returns the current day according to user's time zone |
||
| 332 | def today |
||
| 333 | if time_zone.nil? |
||
| 334 | Date.today |
||
| 335 | else |
||
| 336 | Time.now.in_time_zone(time_zone).to_date |
||
| 337 | end |
||
| 338 | end |
||
| 339 | |||
| 340 | def logged? |
||
| 341 | true |
||
| 342 | end |
||
| 343 | |||
| 344 | def anonymous? |
||
| 345 | !logged? |
||
| 346 | end |
||
| 347 | |||
| 348 | # Return user's roles for project |
||
| 349 | def roles_for_project(project) |
||
| 350 | roles = [] |
||
| 351 | # No role on archived projects |
||
| 352 | return roles unless project && project.active? |
||
| 353 | if logged? |
||
| 354 | # Find project membership |
||
| 355 | membership = memberships.detect {|m| m.project_id == project.id}
|
||
| 356 | if membership |
||
| 357 | roles = membership.roles |
||
| 358 | else |
||
| 359 | @role_non_member ||= Role.non_member |
||
| 360 | roles << @role_non_member |
||
| 361 | end |
||
| 362 | else |
||
| 363 | @role_anonymous ||= Role.anonymous |
||
| 364 | roles << @role_anonymous |
||
| 365 | end |
||
| 366 | roles |
||
| 367 | end |
||
| 368 | |||
| 369 | # Return true if the user is a member of project |
||
| 370 | def member_of?(project) |
||
| 371 | !roles_for_project(project).detect {|role| role.member?}.nil?
|
||
| 372 | end |
||
| 373 | |||
| 374 | 441:cbce1fd3b1b7 | Chris | # Returns a hash of user's projects grouped by roles |
| 375 | def projects_by_role |
||
| 376 | return @projects_by_role if @projects_by_role |
||
| 377 | |||
| 378 | @projects_by_role = Hash.new {|h,k| h[k]=[]}
|
||
| 379 | memberships.each do |membership| |
||
| 380 | membership.roles.each do |role| |
||
| 381 | @projects_by_role[role] << membership.project if membership.project |
||
| 382 | end |
||
| 383 | end |
||
| 384 | @projects_by_role.each do |role, projects| |
||
| 385 | projects.uniq! |
||
| 386 | end |
||
| 387 | |||
| 388 | @projects_by_role |
||
| 389 | end |
||
| 390 | |||
| 391 | 37:94944d00e43c | chris | # Return true if the user is allowed to do the specified action on a specific context |
| 392 | # Action can be: |
||
| 393 | 0:513646585e45 | Chris | # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') |
| 394 | # * a permission Symbol (eg. :edit_project) |
||
| 395 | 37:94944d00e43c | chris | # Context can be: |
| 396 | # * a project : returns true if user is allowed to do the specified action on this project |
||
| 397 | 441:cbce1fd3b1b7 | Chris | # * an array of projects : returns true if user is allowed on every project |
| 398 | 37:94944d00e43c | chris | # * nil with options[:global] set : check if user has at least one role allowed for this action, |
| 399 | # or falls back to Non Member / Anonymous permissions depending if the user is logged |
||
| 400 | 441:cbce1fd3b1b7 | Chris | def allowed_to?(action, context, options={}, &block)
|
| 401 | 37:94944d00e43c | chris | if context && context.is_a?(Project) |
| 402 | 0:513646585e45 | Chris | # No action allowed on archived projects |
| 403 | 37:94944d00e43c | chris | return false unless context.active? |
| 404 | 0:513646585e45 | Chris | # No action allowed on disabled modules |
| 405 | 37:94944d00e43c | chris | return false unless context.allows_to?(action) |
| 406 | 0:513646585e45 | Chris | # Admin users are authorized for anything else |
| 407 | return true if admin? |
||
| 408 | |||
| 409 | 37:94944d00e43c | chris | roles = roles_for_project(context) |
| 410 | 0:513646585e45 | Chris | return false unless roles |
| 411 | 441:cbce1fd3b1b7 | Chris | roles.detect {|role|
|
| 412 | (context.is_public? || role.member?) && |
||
| 413 | role.allowed_to?(action) && |
||
| 414 | (block_given? ? yield(role, self) : true) |
||
| 415 | } |
||
| 416 | 37:94944d00e43c | chris | elsif context && context.is_a?(Array) |
| 417 | # Authorize if user is authorized on every element of the array |
||
| 418 | context.map do |project| |
||
| 419 | 441:cbce1fd3b1b7 | Chris | allowed_to?(action, project, options, &block) |
| 420 | 37:94944d00e43c | chris | end.inject do |memo,allowed| |
| 421 | memo && allowed |
||
| 422 | end |
||
| 423 | 0:513646585e45 | Chris | elsif options[:global] |
| 424 | # Admin users are always authorized |
||
| 425 | return true if admin? |
||
| 426 | |||
| 427 | # authorize if user has at least one role that has this permission |
||
| 428 | roles = memberships.collect {|m| m.roles}.flatten.uniq
|
||
| 429 | 441:cbce1fd3b1b7 | Chris | roles << (self.logged? ? Role.non_member : Role.anonymous) |
| 430 | roles.detect {|role|
|
||
| 431 | role.allowed_to?(action) && |
||
| 432 | (block_given? ? yield(role, self) : true) |
||
| 433 | } |
||
| 434 | 0:513646585e45 | Chris | else |
| 435 | false |
||
| 436 | end |
||
| 437 | end |
||
| 438 | 22:40f7cfd4df19 | chris | |
| 439 | # Is the user allowed to do the specified action on any project? |
||
| 440 | # See allowed_to? for the actions and valid options. |
||
| 441 | 441:cbce1fd3b1b7 | Chris | def allowed_to_globally?(action, options, &block) |
| 442 | allowed_to?(action, nil, options.reverse_merge(:global => true), &block) |
||
| 443 | 22:40f7cfd4df19 | chris | end |
| 444 | 119:8661b858af72 | Chris | |
| 445 | safe_attributes 'login', |
||
| 446 | 'firstname', |
||
| 447 | 'lastname', |
||
| 448 | 'mail', |
||
| 449 | 'mail_notification', |
||
| 450 | 'language', |
||
| 451 | 'custom_field_values', |
||
| 452 | 'custom_fields', |
||
| 453 | 'identity_url' |
||
| 454 | |||
| 455 | safe_attributes 'status', |
||
| 456 | 'auth_source_id', |
||
| 457 | :if => lambda {|user, current_user| current_user.admin?}
|
||
| 458 | |||
| 459 | safe_attributes 'group_ids', |
||
| 460 | :if => lambda {|user, current_user| current_user.admin? && !user.new_record?}
|
||
| 461 | 0:513646585e45 | Chris | |
| 462 | 37:94944d00e43c | chris | # Utility method to help check if a user should be notified about an |
| 463 | # event. |
||
| 464 | # |
||
| 465 | # TODO: only supports Issue events currently |
||
| 466 | def notify_about?(object) |
||
| 467 | 119:8661b858af72 | Chris | case mail_notification |
| 468 | when 'all' |
||
| 469 | 37:94944d00e43c | chris | true |
| 470 | 119:8661b858af72 | Chris | when 'selected' |
| 471 | 210:0579821a129a | Chris | # user receives notifications for created/assigned issues on unselected projects |
| 472 | if object.is_a?(Issue) && (object.author == self || object.assigned_to == self) |
||
| 473 | true |
||
| 474 | else |
||
| 475 | false |
||
| 476 | end |
||
| 477 | 119:8661b858af72 | Chris | when 'none' |
| 478 | 37:94944d00e43c | chris | false |
| 479 | 119:8661b858af72 | Chris | when 'only_my_events' |
| 480 | 37:94944d00e43c | chris | if object.is_a?(Issue) && (object.author == self || object.assigned_to == self) |
| 481 | true |
||
| 482 | else |
||
| 483 | false |
||
| 484 | end |
||
| 485 | 119:8661b858af72 | Chris | when 'only_assigned' |
| 486 | 37:94944d00e43c | chris | if object.is_a?(Issue) && object.assigned_to == self |
| 487 | true |
||
| 488 | else |
||
| 489 | false |
||
| 490 | end |
||
| 491 | 119:8661b858af72 | Chris | when 'only_owner' |
| 492 | 37:94944d00e43c | chris | if object.is_a?(Issue) && object.author == self |
| 493 | true |
||
| 494 | else |
||
| 495 | false |
||
| 496 | end |
||
| 497 | else |
||
| 498 | false |
||
| 499 | end |
||
| 500 | end |
||
| 501 | |||
| 502 | 0:513646585e45 | Chris | def self.current=(user) |
| 503 | @current_user = user |
||
| 504 | end |
||
| 505 | |||
| 506 | def self.current |
||
| 507 | @current_user ||= User.anonymous |
||
| 508 | end |
||
| 509 | |||
| 510 | # Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only |
||
| 511 | # one anonymous user per database. |
||
| 512 | def self.anonymous |
||
| 513 | anonymous_user = AnonymousUser.find(:first) |
||
| 514 | if anonymous_user.nil? |
||
| 515 | anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) |
||
| 516 | raise 'Unable to create the anonymous user.' if anonymous_user.new_record? |
||
| 517 | end |
||
| 518 | anonymous_user |
||
| 519 | end |
||
| 520 | 245:051f544170fe | Chris | |
| 521 | # Salts all existing unsalted passwords |
||
| 522 | # It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) |
||
| 523 | # This method is used in the SaltPasswords migration and is to be kept as is |
||
| 524 | def self.salt_unsalted_passwords! |
||
| 525 | transaction do |
||
| 526 | User.find_each(:conditions => "salt IS NULL OR salt = ''") do |user| |
||
| 527 | next if user.hashed_password.blank? |
||
| 528 | salt = User.generate_salt |
||
| 529 | hashed_password = User.hash_password("#{salt}#{user.hashed_password}")
|
||
| 530 | User.update_all("salt = '#{salt}', hashed_password = '#{hashed_password}'", ["id = ?", user.id] )
|
||
| 531 | end |
||
| 532 | end |
||
| 533 | end |
||
| 534 | 0:513646585e45 | Chris | |
| 535 | protected |
||
| 536 | |||
| 537 | def validate |
||
| 538 | # Password length validation based on setting |
||
| 539 | if !password.nil? && password.size < Setting.password_min_length.to_i |
||
| 540 | errors.add(:password, :too_short, :count => Setting.password_min_length.to_i) |
||
| 541 | end |
||
| 542 | end |
||
| 543 | |||
| 544 | private |
||
| 545 | 128:07fa8a8b56a8 | Chris | |
| 546 | # Removes references that are not handled by associations |
||
| 547 | # Things that are not deleted are reassociated with the anonymous user |
||
| 548 | def remove_references_before_destroy |
||
| 549 | return if self.id.nil? |
||
| 550 | |||
| 551 | substitute = User.anonymous |
||
| 552 | Attachment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 553 | Comment.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 554 | Issue.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 555 | Issue.update_all 'assigned_to_id = NULL', ['assigned_to_id = ?', id] |
||
| 556 | Journal.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
||
| 557 | JournalDetail.update_all ['old_value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND old_value = ?", id.to_s] |
||
| 558 | JournalDetail.update_all ['value = ?', substitute.id.to_s], ["property = 'attr' AND prop_key = 'assigned_to_id' AND value = ?", id.to_s] |
||
| 559 | Message.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 560 | News.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 561 | # Remove private queries and keep public ones |
||
| 562 | Query.delete_all ['user_id = ? AND is_public = ?', id, false] |
||
| 563 | Query.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
||
| 564 | TimeEntry.update_all ['user_id = ?', substitute.id], ['user_id = ?', id] |
||
| 565 | Token.delete_all ['user_id = ?', id] |
||
| 566 | Watcher.delete_all ['user_id = ?', id] |
||
| 567 | WikiContent.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 568 | WikiContent::Version.update_all ['author_id = ?', substitute.id], ['author_id = ?', id] |
||
| 569 | end |
||
| 570 | 0:513646585e45 | Chris | |
| 571 | # Return password digest |
||
| 572 | def self.hash_password(clear_password) |
||
| 573 | Digest::SHA1.hexdigest(clear_password || "") |
||
| 574 | end |
||
| 575 | 245:051f544170fe | Chris | |
| 576 | # Returns a 128bits random salt as a hex string (32 chars long) |
||
| 577 | def self.generate_salt |
||
| 578 | ActiveSupport::SecureRandom.hex(16) |
||
| 579 | end |
||
| 580 | |||
| 581 | 0:513646585e45 | Chris | end |
| 582 | |||
| 583 | class AnonymousUser < User |
||
| 584 | |||
| 585 | def validate_on_create |
||
| 586 | # There should be only one AnonymousUser in the database |
||
| 587 | errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first) |
||
| 588 | end |
||
| 589 | |||
| 590 | def available_custom_fields |
||
| 591 | [] |
||
| 592 | end |
||
| 593 | |||
| 594 | # Overrides a few properties |
||
| 595 | def logged?; false end |
||
| 596 | def admin; false end |
||
| 597 | def name(*args); I18n.t(:label_user_anonymous) end |
||
| 598 | def mail; nil end |
||
| 599 | def time_zone; nil end |
||
| 600 | def rss_key; nil end |
||
| 601 | 128:07fa8a8b56a8 | Chris | |
| 602 | # Anonymous user can not be destroyed |
||
| 603 | def destroy |
||
| 604 | false |
||
| 605 | end |
||
| 606 | 0:513646585e45 | Chris | end |