Class: DoorMat::ActivityResetPassword

Inherits:
Activity
  • Object
show all
Defined in:
app/models/door_mat/activity_reset_password.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Activity

hash_token

Class Method Details

.for(email, controller) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/door_mat/activity_reset_password.rb', line 6

def self.for(email, controller)
  # BFP: prevent email flooding
  return nil unless self.where( actor: email.actor ).where(["created_at > ?", DoorMat.configuration.forgot_password_link_request_delay_minutes.minutes.ago]).blank?

  token = SecureRandom.uuid

  activity = self.new
  activity.actor = email.actor
  activity.email = email
  activity.link_hash = self.hash_token(token)
  activity.started!

  activity.send_email_with(token, controller)
  activity
end

.with(token, emails) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/door_mat/activity_reset_password.rb', line 22

def self.with(token, emails)

  self.where(type: "DoorMat::ActivityResetPassword", link_hash: self.hash_token(token)).started.each do |activity|
    if activity.created_at < DoorMat.configuration.forgot_password_link_expiration_delay_minutes.minutes.ago
      activity.failed!
    else
      return activity if emails.include? activity.email
    end
  end

  nil
end

Instance Method Details

#send_email_with(token, controller) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'app/models/door_mat/activity_reset_password.rb', line 35

def send_email_with(token, controller)
  parameters = {
      url: controller.send(:choose_new_password_url, token: token, email: self.email.to_urlsafe_encoded, protocol: DoorMat::UrlProtocol.url_protocol),
      address: self.email.address
  }
  DoorMat::ActivityMailer.reset_password(parameters).deliver_now
rescue Exception => e
  DoorMat.configuration.logger.error "ERROR: Failed to deliver reset password email for actor #{self.actor.id} to #{self.email.address} w #{parameters[:url]} - #{e}"
  raise e
end