Class: User
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- User
- Defined in:
- app/models/user.rb
Direct Known Subclasses
Constant Summary
- STATUS_ANONYMOUS =
Account statuses
0- STATUS_ACTIVE =
1- STATUS_REGISTERED =
2- STATUS_LOCKED =
3- USER_FORMATS =
{ :firstname_lastname => '#{firstname} #{lastname}', :firstname => '#{firstname}', :lastname_firstname => '#{lastname} #{firstname}', :lastname_coma_firstname => '#{lastname}, #{firstname}', :username => '#{login}' }
Instance Attribute Summary (collapse)
-
- (Object) last_before_login_on
Returns the value of attribute last_before_login_on.
-
- (Object) password
Returns the value of attribute password.
-
- (Object) password_confirmation
Returns the value of attribute password_confirmation.
Class Method Summary (collapse)
- + (Object) anonymous
- + (Object) current
- + (Object) current=(user)
-
+ (Object) find_by_mail(mail)
Makes find_by_mail case-insensitive.
- + (Object) find_by_rss_key(key)
-
+ (Object) try_to_autologin(key)
Returns the user who matches the given autologin key or nil.
-
+ (Object) try_to_login(login, password)
Returns the user that matches provided login and password, or nil.
Instance Method Summary (collapse)
-
- (Object) <=>(user)
Sort users by their display names.
- - (Boolean) active?
-
- (Boolean) allowed_to?(action, project, options = {})
Return true if the user is allowed to do the specified action on project action can be:
-
a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
-
a permission Symbol (eg. :edit_project).
-
- - (Boolean) anonymous?
- - (Object) before_create
- - (Object) before_save
- - (Boolean) check_password?(clear_password)
- - (Object) identity_url=(url)
- - (Boolean) locked?
- - (Boolean) logged?
-
- (Boolean) member_of?(project)
Return true if the user is a member of project.
-
- (Object) name(formatter = nil)
Return user's full name for display.
- - (Object) notified_project_ids=(ids)
-
- (Object) notified_projects_ids
Return an array of project ids for which the user has explicitly turned mail notifications on.
- - (Object) pref
-
- (Object) random_password
Generate and set a random password.
- - (Boolean) registered?
- - (Object) reload(*args)
-
- (Object) role_for_project(project)
Return user's role for project.
-
- (Object) rss_key
Return user's RSS key (a 40 chars long string), used to access feeds.
- - (Object) time_zone
- - (Object) to_s
- - (Boolean) wants_comments_in_reverse_order?
Instance Attribute Details
- (Object) last_before_login_on
Returns the value of attribute last_before_login_on
51 52 53 |
# File 'app/models/user.rb', line 51 def last_before_login_on @last_before_login_on end |
- (Object) password
Returns the value of attribute password
50 51 52 |
# File 'app/models/user.rb', line 50 def password @password end |
- (Object) password_confirmation
Returns the value of attribute password_confirmation
50 51 52 |
# File 'app/models/user.rb', line 50 def password_confirmation @password_confirmation end |
Class Method Details
+ (Object) anonymous
289 290 291 292 293 294 295 296 |
# File 'app/models/user.rb', line 289 def self.anonymous anonymous_user = AnonymousUser.find(:first) if anonymous_user.nil? anonymous_user = AnonymousUser.create(:lastname => 'Anonymous', :firstname => '', :mail => '', :login => '', :status => 0) raise 'Unable to create the anonymous user.' if anonymous_user.new_record? end anonymous_user end |
+ (Object) current
285 286 287 |
# File 'app/models/user.rb', line 285 def self.current @current_user ||= User.anonymous end |
+ (Object) current=(user)
281 282 283 |
# File 'app/models/user.rb', line 281 def self.current=(user) @current_user = user end |
+ (Object) find_by_mail(mail)
Makes find_by_mail case-insensitive
212 213 214 |
# File 'app/models/user.rb', line 212 def self.find_by_mail(mail) find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase]) end |
+ (Object) find_by_rss_key(key)
206 207 208 209 |
# File 'app/models/user.rb', line 206 def self.find_by_rss_key(key) token = Token.find_by_value(key) token && token.user.active? ? token.user : nil end |
+ (Object) try_to_autologin(key)
Returns the user who matches the given autologin key or nil
131 132 133 134 135 136 137 |
# File 'app/models/user.rb', line 131 def self.try_to_autologin(key) token = Token.find_by_action_and_value('autologin', key) if token && (token.created_on > Setting.autologin.to_i.day.ago) && token.user && token.user.active? token.user.update_attribute(:last_login_on, Time.now) token.user end end |
+ (Object) try_to_login(login, password)
Returns the user that matches provided login and password, or nil
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'app/models/user.rb', line 97 def self.try_to_login(login, password) # Make sure no one can sign in with an empty password return nil if password.to_s.empty? user = find(:first, :conditions => ["login=?", login]) if user # user is already in local database return nil if !user.active? if user.auth_source # user has an external authentication method return nil unless user.auth_source.authenticate(login, password) else # authentication with local password return nil unless User.hash_password(password) == user.hashed_password end 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 the LDAP") if logger end end end user.update_attribute(:last_login_on, Time.now) if user && !user.new_record? user rescue => text raise text end |
Instance Method Details
- (Object) <=>(user)
Sort users by their display names
217 218 219 |
# File 'app/models/user.rb', line 217 def <=>(user) self.to_s.downcase <=> user.to_s.downcase end |
- (Boolean) active?
148 149 150 |
# File 'app/models/user.rb', line 148 def active? self.status == STATUS_ACTIVE end |
- (Boolean) allowed_to?(action, project, options = {})
Return true if the user is allowed to do the specified action on project action can be:
-
a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
-
a permission Symbol (eg. :edit_project)
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/models/user.rb', line 259 def allowed_to?(action, project, ={}) if project # No action allowed on archived projects return false unless project.active? # No action allowed on disabled modules return false unless project.allows_to?(action) # Admin users are authorized for anything else return true if admin? role = role_for_project(project) return false unless role role.allowed_to?(action) && (project.is_public? || role.member?) elsif [:global] # authorize if user has at least one role that has this permission roles = memberships.collect {|m| m.role}.uniq roles.detect {|r| r.allowed_to?(action)} || (self.logged? ? Role.non_member.allowed_to?(action) : Role.anonymous.allowed_to?(action)) else false end end |
- (Boolean) anonymous?
229 230 231 |
# File 'app/models/user.rb', line 229 def anonymous? !logged? end |
- (Object) before_create
68 69 70 71 |
# File 'app/models/user.rb', line 68 def before_create self.mail_notification = false true end |
- (Object) before_save
73 74 75 76 |
# File 'app/models/user.rb', line 73 def before_save # update hashed_password if password was set self.hashed_password = User.hash_password(self.password) if self.password end |
- (Boolean) check_password?(clear_password)
160 161 162 |
# File 'app/models/user.rb', line 160 def check_password?(clear_password) User.hash_password(clear_password) == self.hashed_password end |
- (Object) identity_url=(url)
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/models/user.rb', line 83 def identity_url=(url) if url.blank? write_attribute(:identity_url, '') else begin write_attribute(:identity_url, OpenIdAuthentication.normalize_identifier(url)) rescue OpenIdAuthentication::InvalidOpenId # Invlaid url, don't save end end self.read_attribute(:identity_url) end |
- (Boolean) locked?
156 157 158 |
# File 'app/models/user.rb', line 156 def locked? self.status == STATUS_LOCKED end |
- (Boolean) logged?
225 226 227 |
# File 'app/models/user.rb', line 225 def logged? true end |
- (Boolean) member_of?(project)
Return true if the user is a member of project
251 252 253 |
# File 'app/models/user.rb', line 251 def member_of?(project) role_for_project(project).member? end |
- (Object) name(formatter = nil)
Return user's full name for display
140 141 142 143 144 145 146 |
# File 'app/models/user.rb', line 140 def name(formatter = nil) if formatter eval('"' + (USER_FORMATS[formatter] || USER_FORMATS[:firstname_lastname]) + '"') else @name ||= eval('"' + (USER_FORMATS[Setting.user_format] || USER_FORMATS[:firstname_lastname]) + '"') end end |
- (Object) notified_project_ids=(ids)
199 200 201 202 203 204 |
# File 'app/models/user.rb', line 199 def notified_project_ids=(ids) Member.update_all("mail_notification = #{connection.quoted_false}", ['user_id = ?', id]) Member.update_all("mail_notification = #{connection.quoted_true}", ['user_id = ? AND project_id IN (?)', id, ids]) if ids && !ids.empty? @notified_projects_ids = nil notified_projects_ids end |
- (Object) notified_projects_ids
Return an array of project ids for which the user has explicitly turned mail notifications on
195 196 197 |
# File 'app/models/user.rb', line 195 def notified_projects_ids @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) end |
- (Object) pref
176 177 178 |
# File 'app/models/user.rb', line 176 def pref self.preference ||= UserPreference.new(:user => self) end |
- (Object) random_password
Generate and set a random password. Useful for automated user creation Based on Token#generate_token_value
167 168 169 170 171 172 173 174 |
# File 'app/models/user.rb', line 167 def random_password chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a password = '' 40.times { |i| password << chars[rand(chars.size-1)] } self.password = password self.password_confirmation = password self end |
- (Boolean) registered?
152 153 154 |
# File 'app/models/user.rb', line 152 def registered? self.status == STATUS_REGISTERED end |
- (Object) reload(*args)
78 79 80 81 |
# File 'app/models/user.rb', line 78 def reload(*args) @name = nil super end |
- (Object) role_for_project(project)
Return user's role for project
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'app/models/user.rb', line 234 def role_for_project(project) # No role on archived projects return nil unless project && project.active? if logged? # Find project membership membership = memberships.detect {|m| m.project_id == project.id} if membership membership.role else @role_non_member ||= Role.non_member end else @role_anonymous ||= Role.anonymous end end |
- (Object) rss_key
Return user's RSS key (a 40 chars long string), used to access feeds
189 190 191 192 |
# File 'app/models/user.rb', line 189 def rss_key token = self.rss_token || Token.create(:user => self, :action => 'feeds') token.value end |
- (Object) time_zone
180 181 182 |
# File 'app/models/user.rb', line 180 def time_zone @time_zone ||= (self.pref.time_zone.blank? ? nil : ActiveSupport::TimeZone[self.pref.time_zone]) end |
- (Object) to_s
221 222 223 |
# File 'app/models/user.rb', line 221 def to_s name end |
- (Boolean) wants_comments_in_reverse_order?
184 185 186 |
# File 'app/models/user.rb', line 184 def wants_comments_in_reverse_order? self.pref[:comments_sorting] == 'desc' end |