Class: Mori::User

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.change_password(email, password, new_password) ⇒ Object



61
62
63
64
65
66
67
# File 'app/models/mori/user.rb', line 61

def self.change_password(email, password, new_password)
  user = User.find_by_normalized_email(email.normalize)
  raise "Passwords do not match" if ::BCrypt::Password.new(user.password) != password
  user.password = new_password
  user.save
  user
end

.confirm_email(email, token) ⇒ Object



68
69
70
71
72
73
74
75
# File 'app/models/mori/user.rb', line 68

def self.confirm_email(email, token)
  user = User.find_by_confirmation_token(token)
  raise 'Invalid Confirmation Token' if user.blank?
  raise 'Expired Confirmation Token' if user.confirmation_sent < Date.today - 2.weeks
  user.confirmed = true
  user.save
  user
end

.forgot_password(email) ⇒ Object



54
55
56
57
58
59
60
# File 'app/models/mori/user.rb', line 54

def self.forgot_password(email)
  user = User.find_by_email(email)
  user.password_reset_token = generate_token
  user.password_reset_sent = Date.today
  Mailer.password_reset_notification(user)
  user.save
end

.invite(email) ⇒ Object



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

def self.invite(email)
  user = User.create({:email => email, :invitation_token => generate_token, :invitation_sent => Date.today})
  Mailer.invite_user(user)
end

Instance Method Details

#accept_invitation(token, new_password) ⇒ Object



35
36
37
38
39
40
41
# File 'app/models/mori/user.rb', line 35

def accept_invitation(token,new_password)
  user = User.find_by_invitation_token(token)
  raise 'Expired Invitation Token' if user.invitation_sent < Date.today - 2.weeks
  user.password = new_password
  user.save
  user
end

#authenticate(password) ⇒ Object



76
77
78
79
# File 'app/models/mori/user.rb', line 76

def authenticate(password)
  raise 'Invalid Login' if ::BCrypt::Password.new(self.password) != password
  true
end

#encrypt_passwordObject

Methods called by ActiveRecord



23
24
25
# File 'app/models/mori/user.rb', line 23

def encrypt_password
  self.password = self.password.encrypt
end

#normalize_emailObject



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

def normalize_email
  self.email = self.email.normalize
end

#reset_password(token, new_password) ⇒ Object



42
43
44
45
46
47
48
# File 'app/models/mori/user.rb', line 42

def reset_password(token,new_password)
  raise 'Invalid Password Reset Token' unless token == self.password_reset_token
  raise 'Expired Reset Token' if self.password_reset_sent < Date.today - 2.weeks
  self.password = new_password
  self.save
  self
end

#send_email_confirmationObject



29
30
31
32
33
# File 'app/models/mori/user.rb', line 29

def send_email_confirmation
  self.confirmation_token = SecureRandom.hex(25)
  self.confirmation_sent = Date.today
  Mailer.confirm_email(self)
end