Class: User

Inherits:
Party
  • Object
show all
Defined in:
app/models/user.rb

Overview

this model expects a certain database layout and its based on the name/login pattern. class User < ActiveRecord::Base

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Party

#current_speed, #icon, #to_sym

Constructor Details

#initialize(attributes = nil) ⇒ User

Returns a new instance of User.



41
42
43
44
# File 'app/models/user.rb', line 41

def initialize(attributes = nil)
  super
  @password_needs_confirmation = false
end

Instance Attribute Details

#password_needs_confirmationObject

Returns the value of attribute password_needs_confirmation.



13
14
15
# File 'app/models/user.rb', line 13

def password_needs_confirmation
  @password_needs_confirmation
end

Class Method Details

.authenticate(login, pass) ⇒ Object



50
51
52
53
54
# File 'app/models/user.rb', line 50

def self.authenticate(, pass)
  u = find(:first, :conditions => ["login = ? AND verified = ? AND deleted = ?", , true, false])
  return nil if u.nil?
  find(:first, :conditions => ["login = ? AND salted_password = ? AND verified = ?", , salted_password(u.salt, hashed(pass)), true])
end

.authenticate_by_token(id, token) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/user.rb', line 56

def self.authenticate_by_token(id, token)
  # Allow logins for deleted accounts, but only via this method (and
  # not the regular authenticate call)
  logger.info "Attempting authorization of #{id} with #{token}"
  u = find(:first, :conditions => ["id = ? AND security_token = ?", id, token])
  if u
    logger.info "Authenticated by token: #{u.inspect}"
  else
    logger.info "Not authenticated" if u.nil?
  end
  return nil if (u.nil? or u.token_expired?)
  u.update_attribute :verified, true
  return u
end

.token_lifetimeObject



94
95
96
# File 'app/models/user.rb', line 94

def self.token_lifetime
  UserSystem::CONFIG[:security_token_life_hours] * 60 * 60
end

Instance Method Details

#change_password(pass, confirm = nil) ⇒ Object



88
89
90
91
92
# File 'app/models/user.rb', line 88

def change_password(pass, confirm = nil)
  self.password = pass
  self.password_confirmation = confirm.nil? ? pass : confirm
  @password_needs_confirmation = true
end

#controllerObject



46
47
48
# File 'app/models/user.rb', line 46

def controller
  self.class.name.downcase
end

#generate_security_tokenObject



79
80
81
82
83
84
85
86
# File 'app/models/user.rb', line 79

def generate_security_token
  if token_stale?
    token = new_security_token
    return token
  else
    return self.security_token
  end
end

#includes?(user) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/user.rb', line 102

def includes?(user)
  return user == self
end

#nameObject



98
99
100
# File 'app/models/user.rb', line 98

def name
  [first_name, last_name].compact.join(' ')
end

#token_expired?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'app/models/user.rb', line 71

def token_expired?
  self.security_token.nil? or self.token_expiry.nil? or (Clock.now > self.token_expiry)
end

#token_stale?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/user.rb', line 75

def token_stale?
  token_expired? or Clock.now.to_i >= (self.token_expiry.to_i - self.class.token_lifetime / 2)
end