Class: User
- Includes:
- Redmine::SafeAttributes
- Defined in:
- app/models/user.rb
Overview
Direct Known Subclasses
Constant Summary collapse
- USER_FORMATS =
Different ways of displaying/sorting users
{ :firstname_lastname => { :string => '#{firstname} #{lastname}', :order => %w(firstname lastname id), :setting_order => 1 }, :firstname_lastinitial => { :string => '#{firstname} #{lastname.to_s.chars.first}.', :order => %w(firstname lastname id), :setting_order => 2 }, :firstinitial_lastname => { :string => '#{firstname.to_s.gsub(/(([[:alpha:]])[[:alpha:]]*\.?)/, \'\2.\')} #{lastname}', :order => %w(firstname lastname id), :setting_order => 2 }, :firstname => { :string => '#{firstname}', :order => %w(firstname id), :setting_order => 3 }, :lastname_firstname => { :string => '#{lastname} #{firstname}', :order => %w(lastname firstname id), :setting_order => 4 }, :lastnamefirstname => { :string => '#{lastname}#{firstname}', :order => %w(lastname firstname id), :setting_order => 5 }, :lastname_comma_firstname => { :string => '#{lastname}, #{firstname}', :order => %w(lastname firstname id), :setting_order => 6 }, :lastname => { :string => '#{lastname}', :order => %w(lastname id), :setting_order => 7 }, :username => { :string => '#{login}', :order => %w(login id), :setting_order => 8 }, }
- MAIL_NOTIFICATION_OPTIONS =
[ ['all', :label_user_mail_option_all], ['selected', :label_user_mail_option_selected], ['only_my_events', :label_user_mail_option_only_my_events], ['only_assigned', :label_user_mail_option_only_assigned], ['only_owner', :label_user_mail_option_only_owner], ['none', :label_user_mail_option_none] ]
- LOGIN_LENGTH_LIMIT =
60
- MAIL_LENGTH_LIMIT =
60
- CSS_CLASS_BY_STATUS =
{ STATUS_ANONYMOUS => 'anon', STATUS_ACTIVE => 'active', STATUS_REGISTERED => 'registered', STATUS_LOCKED => 'locked' }
Constants inherited from Principal
Principal::STATUS_ACTIVE, Principal::STATUS_ANONYMOUS, Principal::STATUS_LOCKED, Principal::STATUS_REGISTERED
Instance Attribute Summary collapse
-
#generate_password ⇒ Object
Returns the value of attribute generate_password.
-
#last_before_login_on ⇒ Object
Returns the value of attribute last_before_login_on.
-
#password ⇒ Object
Returns the value of attribute password.
-
#password_confirmation ⇒ Object
Returns the value of attribute password_confirmation.
-
#remote_ip ⇒ Object
Returns the value of attribute remote_ip.
Class Method Summary collapse
-
.anonymous ⇒ Object
Returns the anonymous user.
- .current ⇒ Object
- .current=(user) ⇒ Object
-
.default_admin_account_changed? ⇒ Boolean
Returns true if the default admin account can no longer be used.
-
.fields_for_order_statement(table = nil) ⇒ Object
Returns an array of fields names than can be used to make an order statement for users according to how user names are displayed Examples:.
- .find_by_api_key(key) ⇒ Object
-
.find_by_login(login) ⇒ Object
Find a user account by matching the exact login and then a case-insensitive version.
-
.find_by_mail(mail) ⇒ Object
Makes find_by_mail case-insensitive.
- .find_by_rss_key(key) ⇒ Object
- .find_or_initialize_by_identity_url(url) ⇒ Object
- .name_formatter(formatter = nil) ⇒ Object
-
.salt_unsalted_passwords! ⇒ Object
Salts all existing unsalted passwords It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) This method is used in the SaltPasswords migration and is to be kept as is.
-
.try_to_autologin(key) ⇒ Object
Returns the user who matches the given autologin
key
or nil. -
.try_to_login(login, password, active_only = true) ⇒ Object
Returns the user that matches provided login and password, or nil.
-
.valid_notification_options(user = nil) ⇒ Object
Only users that belong to more than 1 project can select projects for which they are notified.
-
.verify_session_token(user_id, token) ⇒ Object
Returns true if token is a valid session token for the user whose id is user_id.
Instance Method Summary collapse
- #activate ⇒ Object
- #activate! ⇒ Object
- #active? ⇒ Boolean
-
#allowed_to?(action, context, options = {}, &block) ⇒ Boolean
Return true if the user is allowed to do the specified action on a specific context Action can be: * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit') * a permission Symbol (eg. :edit_project) Context can be: * a project : returns true if user is allowed to do the specified action on this project * an array of projects : returns true if user is allowed on every project * nil with options set : check if user has at least one role allowed for this action, or falls back to Non Member / Anonymous permissions depending if the user is logged.
-
#allowed_to_globally?(action, options = {}, &block) ⇒ Boolean
Is the user allowed to do the specified action on any project? See allowed_to? for the actions and valid options.
- #allowed_to_view_all_time_entries?(context) ⇒ Boolean
- #anonymous? ⇒ Boolean
-
#api_key ⇒ Object
Return user's API key (a 40 chars long string), used to access the API.
- #base_reload ⇒ Object
-
#builtin_role ⇒ Object
Returns the user's bult-in role.
-
#change_password_allowed? ⇒ Boolean
Does the backend storage allow this user to change their password?.
-
#check_password?(clear_password) ⇒ Boolean
Returns true if
clear_password
is the correct user's password, otherwise false. - #css_classes ⇒ Object
- #delete_autologin_token(value) ⇒ Object
- #delete_session_token(value) ⇒ Object
- #force_default_language? ⇒ Boolean
-
#generate_autologin_token ⇒ Object
Generates a new autologin token and returns its value.
- #generate_password? ⇒ Boolean
-
#generate_session_token ⇒ Object
Generates a new session token and returns its value.
- #identity_url=(url) ⇒ Object
-
#is_or_belongs_to?(arg) ⇒ Boolean
Returns true if user is arg or belongs to arg.
- #language ⇒ Object
- #lock ⇒ Object
- #lock! ⇒ Object
- #locked? ⇒ Boolean
- #logged? ⇒ Boolean
- #mail ⇒ Object
- #mail=(arg) ⇒ Object
- #mail_changed? ⇒ Boolean
- #mails ⇒ Object
-
#managed_roles(project) ⇒ Object
Returns the roles that the user is allowed to manage for the given project.
-
#membership(project) ⇒ Object
Returns user's membership for the given project or nil if the user is not a member of project.
- #must_change_password? ⇒ Boolean
-
#name(formatter = nil) ⇒ Object
Return user's full name for display.
- #notified_project_ids=(ids) ⇒ Object
-
#notified_projects_ids ⇒ Object
Return an array of project ids for which the user has explicitly turned mail notifications on.
-
#notify_about?(object) ⇒ Boolean
Utility method to help check if a user should be notified about an event.
-
#own_account_deletable? ⇒ Boolean
Returns true if the user is allowed to delete the user's own account.
-
#password_expired? ⇒ Boolean
Returns true if the user password has expired.
- #pref ⇒ Object
-
#project_ids_by_role ⇒ Object
Returns a hash of project ids grouped by roles.
-
#projects_by_role ⇒ Object
Returns a hash of user's projects grouped by roles TODO: No longer used, should be deprecated.
-
#random_password(length = 40) ⇒ Object
Generate and set a random password on given length.
- #register ⇒ Object
- #register! ⇒ Object
- #registered? ⇒ Boolean
- #reload(*args) ⇒ Object
- #roles ⇒ Object
-
#roles_for_project(project) ⇒ Object
Return user's roles for project.
-
#rss_key ⇒ Object
Return user's RSS key (a 40 chars long string), used to access feeds.
-
#salt_password(clear_password) ⇒ Object
Generates a random salt and computes hashed_password for
clear_password
The hashed password is stored in the following form: SHA1(salt + SHA1(password)). - #set_mail_notification ⇒ Object
-
#time_to_date(time) ⇒ Object
Returns the day of
time
according to user's time zone. - #time_zone ⇒ Object
- #to_s ⇒ Object
-
#today ⇒ Object
Returns the current day according to user's time zone.
- #update_hashed_password ⇒ Object
- #valid_notification_options ⇒ Object
-
#visible_project_ids ⇒ Object
Returns the ids of visible projects.
- #wants_comments_in_reverse_order? ⇒ Boolean
Methods included from Redmine::SafeAttributes
#delete_unsafe_attributes, #safe_attribute?, #safe_attribute_names, #safe_attributes=
Methods inherited from Principal
#<=>, detect_by_keyword, #member_of?, #nullify_projects_default_assigned_to, #project_ids, #visible?
Instance Attribute Details
#generate_password ⇒ Object
Returns the value of attribute generate_password
98 99 100 |
# File 'app/models/user.rb', line 98 def generate_password @generate_password end |
#last_before_login_on ⇒ Object
Returns the value of attribute last_before_login_on
99 100 101 |
# File 'app/models/user.rb', line 99 def last_before_login_on @last_before_login_on end |
#password ⇒ Object
Returns the value of attribute password
98 99 100 |
# File 'app/models/user.rb', line 98 def password @password end |
#password_confirmation ⇒ Object
Returns the value of attribute password_confirmation
98 99 100 |
# File 'app/models/user.rb', line 98 def password_confirmation @password_confirmation end |
#remote_ip ⇒ Object
Returns the value of attribute remote_ip
100 101 102 |
# File 'app/models/user.rb', line 100 def remote_ip @remote_ip end |
Class Method Details
.anonymous ⇒ Object
Returns the anonymous user. If the anonymous user does not exist, it is created. There can be only one anonymous user per database.
797 798 799 800 801 802 803 804 |
# File 'app/models/user.rb', line 797 def self.anonymous anonymous_user = AnonymousUser.unscoped.first if anonymous_user.nil? anonymous_user = AnonymousUser.unscoped.create(:lastname => 'Anonymous', :firstname => '', :login => '', :status => 0) raise 'Unable to create the anonymous user.' if anonymous_user.new_record? end anonymous_user end |
.current ⇒ Object
791 792 793 |
# File 'app/models/user.rb', line 791 def self.current RequestStore.store[:current_user] ||= User.anonymous end |
.current=(user) ⇒ Object
787 788 789 |
# File 'app/models/user.rb', line 787 def self.current=(user) RequestStore.store[:current_user] = user end |
.default_admin_account_changed? ⇒ Boolean
Returns true if the default admin account can no longer be used
512 513 514 |
# File 'app/models/user.rb', line 512 def self.default_admin_account_changed? !User.active.find_by_login("admin").try(:check_password?, "admin") end |
.fields_for_order_statement(table = nil) ⇒ Object
Returns an array of fields names than can be used to make an order statement for users according to how user names are displayed Examples:
User.fields_for_order_statement => ['users.login', 'users.id']
User.fields_for_order_statement('authors') => ['authors.login', 'authors.id']
270 271 272 273 |
# File 'app/models/user.rb', line 270 def self.fields_for_order_statement(table=nil) table ||= table_name name_formatter[:order].map {|field| "#{table}.#{field}"} end |
.find_by_api_key(key) ⇒ Object
502 503 504 |
# File 'app/models/user.rb', line 502 def self.find_by_api_key(key) Token.find_active_user('api', key) end |
.find_by_login(login) ⇒ Object
Find a user account by matching the exact login and then a case-insensitive version. Exact matches will be given priority.
485 486 487 488 489 490 491 492 493 494 495 496 |
# File 'app/models/user.rb', line 485 def self.find_by_login(login) login = Redmine::CodesetUtil.replace_invalid_utf8(login.to_s) if login.present? # First look for an exact match user = where(:login => login).detect {|u| u.login == login} unless user # Fail over to case-insensitive if none was found user = where("LOWER(login) = ?", login.downcase).first end user end end |
.find_by_mail(mail) ⇒ Object
Makes find_by_mail case-insensitive
507 508 509 |
# File 'app/models/user.rb', line 507 def self.find_by_mail(mail) having_mail(mail).first end |
.find_by_rss_key(key) ⇒ Object
498 499 500 |
# File 'app/models/user.rb', line 498 def self.find_by_rss_key(key) Token.find_active_user('feeds', key) end |
.find_or_initialize_by_identity_url(url) ⇒ Object
198 199 200 201 202 203 204 205 |
# File 'app/models/user.rb', line 198 def self.find_or_initialize_by_identity_url(url) user = where(:identity_url => url).first unless user user = User.new user.identity_url = url end user end |
.name_formatter(formatter = nil) ⇒ Object
260 261 262 |
# File 'app/models/user.rb', line 260 def self.name_formatter(formatter = nil) USER_FORMATS[formatter || Setting.user_format] || USER_FORMATS[:firstname_lastname] end |
.salt_unsalted_passwords! ⇒ Object
Salts all existing unsalted passwords It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) This method is used in the SaltPasswords migration and is to be kept as is
809 810 811 812 813 814 815 816 817 818 |
# File 'app/models/user.rb', line 809 def self.salt_unsalted_passwords! transaction do User.where("salt IS NULL OR salt = ''").find_each do |user| next if user.hashed_password.blank? salt = User.generate_salt hashed_password = User.hash_password("#{salt}#{user.hashed_password}") User.where(:id => user.id).update_all(:salt => salt, :hashed_password => hashed_password) end end end |
.try_to_autologin(key) ⇒ Object
Returns the user who matches the given autologin key
or nil
252 253 254 255 256 257 258 |
# File 'app/models/user.rb', line 252 def self.try_to_autologin(key) user = Token.find_active_user('autologin', key, Setting.autologin.to_i) if user user.update_column(:last_login_on, Time.now) user end end |
.try_to_login(login, password, active_only = true) ⇒ Object
Returns the user that matches provided login and password, or nil
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'app/models/user.rb', line 221 def self.try_to_login(login, password, active_only=true) login = login.to_s.strip password = password.to_s # Make sure no one can sign in with an empty login or password return nil if login.empty? || password.empty? user = find_by_login(login) if user # user is already in local database return nil unless user.check_password?(password) return nil if !user.active? && active_only else # user is not yet registered, try to authenticate with available sources attrs = AuthSource.authenticate(login, password) if attrs user = new(attrs) user.login = login user.language = Setting.default_language if user.save user.reload logger.info("User '#{user.login}' created from external auth source: #{user.auth_source.type} - #{user.auth_source.name}") if logger && user.auth_source end end end user.update_column(:last_login_on, Time.now) if user && !user.new_record? && user.active? user rescue => text raise text end |
.valid_notification_options(user = nil) ⇒ Object
Only users that belong to more than 1 project can select projects for which they are notified
473 474 475 476 477 478 479 480 481 |
# File 'app/models/user.rb', line 473 def self.(user=nil) # Note that @user.membership.size would fail since AR ignores # :include association option when doing a count if user.nil? || user.memberships.length < 1 MAIL_NOTIFICATION_OPTIONS.reject {|option| option.first == 'selected'} else MAIL_NOTIFICATION_OPTIONS end end |
.verify_session_token(user_id, token) ⇒ Object
Returns true if token is a valid session token for the user whose id is user_id
435 436 437 438 439 440 441 442 443 444 445 446 |
# File 'app/models/user.rb', line 435 def self.verify_session_token(user_id, token) return false if user_id.blank? || token.blank? scope = Token.where(:user_id => user_id, :value => token.to_s, :action => 'session') if Setting.session_lifetime? scope = scope.where("created_on > ?", Setting.session_lifetime.to_i.minutes.ago) end if Setting.session_timeout? scope = scope.where("updated_on > ?", Setting.session_timeout.to_i.minutes.ago) end scope.update_all(:updated_on => Time.now) == 1 end |
Instance Method Details
#activate ⇒ Object
297 298 299 |
# File 'app/models/user.rb', line 297 def activate self.status = STATUS_ACTIVE end |
#activate! ⇒ Object
309 310 311 |
# File 'app/models/user.rb', line 309 def activate! update_attribute(:status, STATUS_ACTIVE) end |
#active? ⇒ Boolean
285 286 287 |
# File 'app/models/user.rb', line 285 def active? self.status == STATUS_ACTIVE end |
#allowed_to?(action, context, options = {}, &block) ⇒ Boolean
Return true if the user is allowed to do the specified action on a specific context Action can be:
-
a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
-
a permission Symbol (eg. :edit_project)
Context can be:
-
a project : returns true if user is allowed to do the specified action on this project
-
an array of projects : returns true if user is allowed on every project
-
nil with options set : check if user has at least one role allowed for this action, or falls back to Non Member / Anonymous permissions depending if the user is logged
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'app/models/user.rb', line 676 def allowed_to?(action, context, ={}, &block) if context && context.is_a?(Project) return false unless context.allows_to?(action) # Admin users are authorized for anything else return true if admin? roles = roles_for_project(context) return false unless roles roles.any? {|role| (context.is_public? || role.member?) && role.allowed_to?(action) && (block_given? ? yield(role, self) : true) } elsif context && context.is_a?(Array) if context.empty? false else # Authorize if user is authorized on every element of the array context.map {|project| allowed_to?(action, project, , &block)}.reduce(:&) end elsif context raise ArgumentError.new("#allowed_to? context argument must be a Project, an Array of projects or nil") elsif [:global] # Admin users are always authorized return true if admin? # authorize if user has at least one role that has this permission roles = self.roles.to_a | [builtin_role] roles.any? {|role| role.allowed_to?(action) && (block_given? ? yield(role, self) : true) } else false end end |
#allowed_to_globally?(action, options = {}, &block) ⇒ Boolean
Is the user allowed to do the specified action on any project? See allowed_to? for the actions and valid options.
NB: this method is not used anywhere in the core codebase as of 2.5.2, but it's used by many plugins so if we ever want to remove it it has to be carefully deprecated for a version or two.
719 720 721 |
# File 'app/models/user.rb', line 719 def allowed_to_globally?(action, ={}, &block) allowed_to?(action, nil, .reverse_merge(:global => true), &block) end |
#allowed_to_view_all_time_entries?(context) ⇒ Boolean
723 724 725 726 727 |
# File 'app/models/user.rb', line 723 def allowed_to_view_all_time_entries?(context) allowed_to?(:view_time_entries, context) do |role, user| role.time_entries_visibility == 'all' end end |
#anonymous? ⇒ Boolean
553 554 555 |
# File 'app/models/user.rb', line 553 def anonymous? !logged? end |
#api_key ⇒ Object
Return user's API key (a 40 chars long string), used to access the API
407 408 409 410 411 412 |
# File 'app/models/user.rb', line 407 def api_key if api_token.nil? create_api_token(:action => 'api') end api_token.value end |
#base_reload ⇒ Object
166 |
# File 'app/models/user.rb', line 166 alias :base_reload :reload |
#builtin_role ⇒ Object
Returns the user's bult-in role
573 574 575 |
# File 'app/models/user.rb', line 573 def builtin_role @builtin_role ||= Role.non_member end |
#change_password_allowed? ⇒ Boolean
Does the backend storage allow this user to change their password?
339 340 341 342 |
# File 'app/models/user.rb', line 339 def change_password_allowed? return true if auth_source.nil? return auth_source.allow_password_changes? end |
#check_password?(clear_password) ⇒ Boolean
Returns true if clear_password
is the correct user's
password, otherwise false
322 323 324 325 326 327 328 |
# File 'app/models/user.rb', line 322 def check_password?(clear_password) if auth_source_id.present? auth_source.authenticate(self.login, clear_password) else User.hash_password("#{salt}#{User.hash_password clear_password}") == hashed_password end end |
#css_classes ⇒ Object
527 528 529 |
# File 'app/models/user.rb', line 527 def css_classes "user #{CSS_CLASS_BY_STATUS[status]}" end |
#delete_autologin_token(value) ⇒ Object
430 431 432 |
# File 'app/models/user.rb', line 430 def delete_autologin_token(value) Token.where(:user_id => id, :action => 'autologin', :value => value).delete_all end |
#delete_session_token(value) ⇒ Object
420 421 422 |
# File 'app/models/user.rb', line 420 def delete_session_token(value) Token.where(:user_id => id, :action => 'session', :value => value).delete_all end |
#force_default_language? ⇒ Boolean
382 383 384 |
# File 'app/models/user.rb', line 382 def force_default_language? Setting.force_default_language_for_loggedin? end |
#generate_autologin_token ⇒ Object
Generates a new autologin token and returns its value
425 426 427 428 |
# File 'app/models/user.rb', line 425 def generate_autologin_token token = Token.create!(:user_id => id, :action => 'autologin') token.value end |
#generate_password? ⇒ Boolean
359 360 361 |
# File 'app/models/user.rb', line 359 def generate_password? generate_password == '1' || generate_password == true end |
#generate_session_token ⇒ Object
Generates a new session token and returns its value
415 416 417 418 |
# File 'app/models/user.rb', line 415 def generate_session_token token = Token.create!(:user_id => id, :action => 'session') token.value end |
#identity_url=(url) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'app/models/user.rb', line 207 def identity_url=(url) if url.blank? write_attribute(:identity_url, '') else begin write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) rescue OpenIdAuthentication::InvalidOpenId # Invalid url, don't save end end self.read_attribute(:identity_url) end |
#is_or_belongs_to?(arg) ⇒ Boolean
Returns true if user is arg or belongs to arg
657 658 659 660 661 662 663 664 665 |
# File 'app/models/user.rb', line 657 def is_or_belongs_to?(arg) if arg.is_a?(User) self == arg elsif arg.is_a?(Group) arg.users.include?(self) else false end end |
#language ⇒ Object
386 387 388 389 390 391 392 |
# File 'app/models/user.rb', line 386 def language if force_default_language? Setting.default_language else super end end |
#lock ⇒ Object
305 306 307 |
# File 'app/models/user.rb', line 305 def lock self.status = STATUS_LOCKED end |
#lock! ⇒ Object
317 318 319 |
# File 'app/models/user.rb', line 317 def lock! update_attribute(:status, STATUS_LOCKED) end |
#locked? ⇒ Boolean
293 294 295 |
# File 'app/models/user.rb', line 293 def locked? self.status == STATUS_LOCKED end |
#logged? ⇒ Boolean
549 550 551 |
# File 'app/models/user.rb', line 549 def logged? true end |
#mail ⇒ Object
181 182 183 |
# File 'app/models/user.rb', line 181 def mail email_address.try(:address) end |
#mail=(arg) ⇒ Object
185 186 187 188 |
# File 'app/models/user.rb', line 185 def mail=(arg) email = email_address || build_email_address email.address = arg end |
#mail_changed? ⇒ Boolean
190 191 192 |
# File 'app/models/user.rb', line 190 def mail_changed? email_address.try(:address_changed?) end |
#mails ⇒ Object
194 195 196 |
# File 'app/models/user.rb', line 194 def mails email_addresses.pluck(:address) end |
#managed_roles(project) ⇒ Object
Returns the roles that the user is allowed to manage for the given project
648 649 650 651 652 653 654 |
# File 'app/models/user.rb', line 648 def managed_roles(project) if admin? @managed_roles ||= Role.givable.to_a else membership(project).try(:managed_roles) || [] end end |
#membership(project) ⇒ Object
Returns user's membership for the given project or nil if the user is not a member of project
559 560 561 562 563 564 565 566 |
# File 'app/models/user.rb', line 559 def membership(project) project_id = project.is_a?(Project) ? project.id : project @membership_by_project_id ||= Hash.new {|h, project_id| h[project_id] = memberships.where(:project_id => project_id).first } @membership_by_project_id[project_id] end |
#must_change_password? ⇒ Boolean
355 356 357 |
# File 'app/models/user.rb', line 355 def must_change_password? (must_change_passwd? || password_expired?) && change_password_allowed? end |
#name(formatter = nil) ⇒ Object
Return user's full name for display
276 277 278 279 280 281 282 283 |
# File 'app/models/user.rb', line 276 def name(formatter = nil) f = self.class.name_formatter(formatter) if formatter eval('"' + f[:string] + '"') else @name ||= eval('"' + f[:string] + '"') end end |
#notified_project_ids=(ids) ⇒ Object
453 454 455 456 |
# File 'app/models/user.rb', line 453 def notified_project_ids=(ids) @notified_projects_ids_changed = true @notified_projects_ids = ids.map(&:to_i).uniq.select {|n| n > 0} end |
#notified_projects_ids ⇒ Object
Return an array of project ids for which the user has explicitly turned mail notifications on
449 450 451 |
# File 'app/models/user.rb', line 449 def notified_projects_ids @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) end |
#notify_about?(object) ⇒ Boolean
Utility method to help check if a user should be notified about an event.
TODO: only supports Issue events currently
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 |
# File 'app/models/user.rb', line 763 def notify_about?(object) if mail_notification == 'all' true elsif mail_notification.blank? || mail_notification == 'none' false else case object when Issue case mail_notification when 'selected', 'only_my_events' # user receives notifications for created/assigned issues on unselected projects object. == self || is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) when 'only_assigned' is_or_belongs_to?(object.assigned_to) || is_or_belongs_to?(object.assigned_to_was) when 'only_owner' object. == self end when News # always send to project members except when mail_notification is set to 'none' true end end end |
#own_account_deletable? ⇒ Boolean
Returns true if the user is allowed to delete the user's own account
730 731 732 733 |
# File 'app/models/user.rb', line 730 def own_account_deletable? Setting.unsubscribe? && (!admin? || User.active.admin.where("id <> ?", id).exists?) end |
#password_expired? ⇒ Boolean
Returns true if the user password has expired
345 346 347 348 349 350 351 352 353 |
# File 'app/models/user.rb', line 345 def password_expired? period = Setting.password_max_age.to_i if period.zero? false else changed_on = self.passwd_changed_on || Time.at(0) changed_on < period.days.ago end end |
#pref ⇒ Object
374 375 376 |
# File 'app/models/user.rb', line 374 def pref self.preference ||= UserPreference.new(:user => self) end |
#project_ids_by_role ⇒ Object
Returns a hash of project ids grouped by roles. Includes the projects that the user is a member of and the projects that grant custom permissions to the builtin groups.
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'app/models/user.rb', line 605 def project_ids_by_role # Clear project condition for when called from chained scopes # eg. project.children.visible(user) Project.unscoped do return @project_ids_by_role if @project_ids_by_role group_class = anonymous? ? GroupAnonymous : GroupNonMember group_id = group_class.pluck(:id).first members = Member.joins(:project, :member_roles). where("#{Project.table_name}.status <> 9"). where("#{Member.table_name}.user_id = ? OR (#{Project.table_name}.is_public = ? AND #{Member.table_name}.user_id = ?)", self.id, true, group_id). pluck(:user_id, :role_id, :project_id) hash = {} members.each do |user_id, role_id, project_id| # Ignore the roles of the builtin group if the user is a member of the project next if user_id != id && project_ids.include?(project_id) hash[role_id] ||= [] hash[role_id] << project_id end result = Hash.new([]) if hash.present? roles = Role.where(:id => hash.keys).to_a hash.each do |role_id, proj_ids| role = roles.detect {|r| r.id == role_id} if role result[role] = proj_ids.uniq end end end @project_ids_by_role = result end end |
#projects_by_role ⇒ Object
Returns a hash of user's projects grouped by roles TODO: No longer used, should be deprecated
592 593 594 595 596 597 598 599 600 |
# File 'app/models/user.rb', line 592 def projects_by_role return @projects_by_role if @projects_by_role result = Hash.new([]) project_ids_by_role.each do |role, ids| result[role] = Project.where(:id => ids).to_a end @projects_by_role = result end |
#random_password(length = 40) ⇒ Object
Generate and set a random password on given length
364 365 366 367 368 369 370 371 372 |
# File 'app/models/user.rb', line 364 def random_password(length=40) chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a chars -= %w(0 O 1 l) password = '' length.times {|i| password << chars[SecureRandom.random_number(chars.size)] } self.password = password self.password_confirmation = password self end |
#register ⇒ Object
301 302 303 |
# File 'app/models/user.rb', line 301 def register self.status = STATUS_REGISTERED end |
#register! ⇒ Object
313 314 315 |
# File 'app/models/user.rb', line 313 def register! update_attribute(:status, STATUS_REGISTERED) end |
#registered? ⇒ Boolean
289 290 291 |
# File 'app/models/user.rb', line 289 def registered? self.status == STATUS_REGISTERED end |
#reload(*args) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/user.rb', line 167 def reload(*args) @name = nil @roles = nil @projects_by_role = nil @project_ids_by_role = nil @membership_by_project_id = nil @notified_projects_ids = nil @notified_projects_ids_changed = false @builtin_role = nil @visible_project_ids = nil @managed_roles = nil base_reload(*args) end |
#roles ⇒ Object
568 569 570 |
# File 'app/models/user.rb', line 568 def roles @roles ||= Role.joins(members: :project).where(["#{Project.table_name}.status <> ?", Project::STATUS_ARCHIVED]).where(Member.arel_table[:user_id].eq(id)).distinct end |
#roles_for_project(project) ⇒ Object
Return user's roles for project
578 579 580 581 582 583 584 585 586 587 588 |
# File 'app/models/user.rb', line 578 def roles_for_project(project) # No role on archived projects return [] if project.nil? || project.archived? if membership = membership(project) membership.roles.to_a elsif project.is_public? project.override_roles(builtin_role) else [] end end |
#rss_key ⇒ Object
Return user's RSS key (a 40 chars long string), used to access feeds
399 400 401 402 403 404 |
# File 'app/models/user.rb', line 399 def rss_key if rss_token.nil? create_rss_token(:action => 'feeds') end rss_token.value end |
#salt_password(clear_password) ⇒ Object
Generates a random salt and computes hashed_password for
clear_password
The hashed password is stored in the following
form: SHA1(salt + SHA1(password))
332 333 334 335 336 |
# File 'app/models/user.rb', line 332 def salt_password(clear_password) self.salt = User.generate_salt self.hashed_password = User.hash_password("#{salt}#{User.hash_password clear_password}") self.passwd_changed_on = Time.now.change(:usec => 0) end |
#set_mail_notification ⇒ Object
154 155 156 157 |
# File 'app/models/user.rb', line 154 def set_mail_notification self.mail_notification = Setting.default_notification_option if self.mail_notification.blank? true end |
#time_to_date(time) ⇒ Object
Returns the day of time
according to user's time zone
541 542 543 544 545 546 547 |
# File 'app/models/user.rb', line 541 def time_to_date(time) if time_zone.nil? time.to_date else time.in_time_zone(time_zone).to_date end end |
#time_zone ⇒ Object
378 379 380 |
# File 'app/models/user.rb', line 378 def time_zone @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) end |
#to_s ⇒ Object
516 517 518 |
# File 'app/models/user.rb', line 516 def to_s name end |
#today ⇒ Object
Returns the current day according to user's time zone
532 533 534 535 536 537 538 |
# File 'app/models/user.rb', line 532 def today if time_zone.nil? Date.today else time_zone.today end end |
#update_hashed_password ⇒ Object
159 160 161 162 163 164 |
# File 'app/models/user.rb', line 159 def update_hashed_password # update hashed_password if password was set if self.password && self.auth_source_id.blank? salt_password(password) end end |
#valid_notification_options ⇒ Object
468 469 470 |
# File 'app/models/user.rb', line 468 def self.class.(self) end |
#visible_project_ids ⇒ Object
Returns the ids of visible projects
643 644 645 |
# File 'app/models/user.rb', line 643 def visible_project_ids @visible_project_ids ||= Project.visible(self).pluck(:id) end |
#wants_comments_in_reverse_order? ⇒ Boolean
394 395 396 |
# File 'app/models/user.rb', line 394 def wants_comments_in_reverse_order? self.pref[:comments_sorting] == 'desc' end |