Class: User

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ User

hmmm, how does this interact with the developer’s own User model initialize? We would have to insist that the User.initialize method called ‘super’



58
59
60
61
# File 'app/models/user.rb', line 58

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

Instance Attribute Details

#new_passwordObject

Returns the value of attribute new_password.



2
3
4
# File 'app/models/user.rb', line 2

def new_password
  @new_password
end

Class Method Details

.authenticate(login, pass) ⇒ Object



24
25
26
27
28
# File 'app/models/user.rb', line 24

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

.authenticate_by_token(id, token) ⇒ Object



30
31
32
33
34
35
36
37
# File 'app/models/user.rb', line 30

def User.authenticate_by_token(id, token)
	# Allow logins for deleted accounts, but only via this method (and
	# not the regular authenticate call)
	u = find(:first, :conditions => ["#{User.primary_key} = ? AND security_token = ?", id, token])
	return nil if u.nil? or u.token_expired?
	return nil if false == u.update_expiry
	u
end

Instance Method Details

#change_password(pass, confirm = nil) ⇒ Object



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

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

#generate_security_token(hours = nil) ⇒ Object



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

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

#set_delete_afterObject



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

def set_delete_after
	hours = LOGIN_ENGINE[:delayed_delete_days] * 24
	write_attribute('deleted', 1)
	write_attribute('delete_after', Time.at(Time.now.to_i + hours * 60 * 60))

	# Generate and return a token here, so that it expires at
	# the same time that the account deletion takes effect.
	return generate_security_token(hours)
end

#token_expired?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/user.rb', line 63

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

#update_expiryObject



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

def update_expiry
	write_attribute('token_expiry', [self.token_expiry, Time.at(Time.now.to_i + 600 * 1000)].min)
	write_attribute('authenticated_by_token', true)
	write_attribute("verified", 1)
	update_without_callbacks
end