Class: Janus::PasswordsController

Inherits:
ApplicationController
  • Object
show all
Includes:
InternalHelpers
Defined in:
lib/janus/controllers/passwords_controller.rb

Overview

This controller is responsible for resetting a lost password. It sends an email with an unique token, on demand by the user. Then allows the user to change its password (as long as the token is valid).

Instance Method Summary collapse

Methods included from InternalHelpers

#authenticate!, #janus_scope, #mailer_class, #resource, #resource=, #resource_class, #resource_name

Instance Method Details

#after_password_change_url(resource) ⇒ Object

Where to redirect when the password has been changed.



84
85
86
# File 'lib/janus/controllers/passwords_controller.rb', line 84

def after_password_change_url(resource)
  root_url
end

#after_sending_reset_password_instructions_url(resource) ⇒ Object

Where to redirect when the instructions have been sent.



89
90
91
# File 'lib/janus/controllers/passwords_controller.rb', line 89

def after_sending_reset_password_instructions_url(resource)
  root_url
end

#createObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/janus/controllers/passwords_controller.rb', line 13

def create
  self.resource = resource_class.find_for_database_authentication(params[resource_name])

  if resource
    resource.generate_reset_password_token!
    deliver_reset_password_instructions(resource)

    respond_to do |format|
      format.html do
        redirect_to after_sending_reset_password_instructions_url(resource),
          :notice => t('flash.janus.passwords.create.email_sent')
      end
      format.any  { head :ok }
    end
  else
    respond_to do |format|
      format.html do
        self.resource = resource_class.new
        resource.errors.add(:base, :not_found)
        render "new"
      end
      format.any { head :precondition_failed }
    end
  end
end

#deliver_reset_password_instructions(resource) ⇒ Object

Simple wrapper for Mailer#reset_password_instructions.deliver to allow customization of the email (eg: to pass additional data).



69
70
71
# File 'lib/janus/controllers/passwords_controller.rb', line 69

def deliver_reset_password_instructions(resource)
  mailer_class.reset_password_instructions(resource).deliver
end

#editObject



39
40
41
42
# File 'lib/janus/controllers/passwords_controller.rb', line 39

def edit
  self.resource = resource_class.find_for_password_reset(params[:token])
  redirect_to root_url, :alert => t('flash.janus.passwords.edit.alert') unless resource
end

#newObject



9
10
11
# File 'lib/janus/controllers/passwords_controller.rb', line 9

def new
  self.resource = resource_class.new
end

#redirect_after_password_change(resource, options = {}) ⇒ Object

Either redirects the user to after_password_change_url or to params[:return_to] if present.



75
76
77
78
79
80
81
# File 'lib/janus/controllers/passwords_controller.rb', line 75

def redirect_after_password_change(resource, options = {})
  if params[:return_to].present?
    redirect_to params[:return_to], options
  else
    redirect_to after_password_change_url(resource), options
  end
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/janus/controllers/passwords_controller.rb', line 44

def update
  self.resource = resource_class.find_for_password_reset(params[resource_name][:reset_password_token])

  if resource
    if resource.reset_password!(params[resource_name])
      respond_to do |format|
        format.html { redirect_after_password_change(self.resource, :notice => t('flash.janus.passwords.update.password_updated')) }
        format.any  { head :ok }
      end
    else
      respond_to do |format|
        format.html { render 'edit' }
        format.any  { head :precondition_failed }
      end
    end
  else
    respond_to do |format|
      format.html { redirect_to root_url, :alert => t('flash.janus.passwords.update.invalid_token') }
      format.any  { head :precondition_failed }
    end
  end
end