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

#icon, #to_sym

Constructor Details

#initialize(attributes = nil) ⇒ User

Returns a new instance of User.



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

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

Instance Attribute Details

#password_needs_confirmationObject

Returns the value of attribute password_needs_confirmation.



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

def password_needs_confirmation
  @password_needs_confirmation
end

Class Method Details

.authenticate(login, pass) ⇒ Object



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

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

.authenticate_by_token(id, token) ⇒ Object



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

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

Instance Method Details

#change_password(pass, confirm = nil) ⇒ Object



83
84
85
86
87
# File 'app/models/user.rb', line 83

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

#controllerObject



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

def controller
  self.class.name.downcase
end

#generate_security_tokenObject



74
75
76
77
78
79
80
81
# File 'app/models/user.rb', line 74

def generate_security_token
  if self.security_token.nil? or self.token_expiry.nil? or (Clock.now.to_i + token_lifetime / 2) >= self.token_expiry.to_i
    token = new_security_token
    return token
  else
    return self.security_token
  end
end

#includes?(user) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#nameObject



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

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

#token_expired?Boolean

Returns:

  • (Boolean)


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

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

#token_lifetimeObject



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

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