Class: Clearance::PasswordsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/clearance/passwords_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



12
13
14
15
16
17
18
19
# File 'app/controllers/clearance/passwords_controller.rb', line 12

def create
  if user = find_user_for_create
    user.forgot_password!
    deliver_email(user)
  end

  render template: "passwords/create"
end

#deliver_email(user) ⇒ Object (private)



47
48
49
50
# File 'app/controllers/clearance/passwords_controller.rb', line 47

def deliver_email(user)
  mail = ::ClearanceMailer.change_password(user)
  mail.deliver_later
end

#editObject



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/clearance/passwords_controller.rb', line 21

def edit
  @user = find_user_for_edit

  if params[:token]
    session[:password_reset_token] = params[:token]
    redirect_to url_for
  else
    render template: "passwords/edit"
  end
end

#email_from_password_paramsObject (private)



64
65
66
# File 'app/controllers/clearance/passwords_controller.rb', line 64

def email_from_password_params
  params.dig(:password, :email)
end

#ensure_email_presentObject (private)



81
82
83
84
85
86
# File 'app/controllers/clearance/passwords_controller.rb', line 81

def ensure_email_present
  if email_from_password_params.blank?
    flash_failure_when_missing_email
    render template: "passwords/new"
  end
end

#ensure_existing_userObject (private)



88
89
90
91
92
93
# File 'app/controllers/clearance/passwords_controller.rb', line 88

def ensure_existing_user
  unless find_user_by_id_and_confirmation_token
    flash_failure_when_forbidden
    render template: "passwords/new"
  end
end

#find_user_by_id_and_confirmation_tokenObject (private)



56
57
58
59
60
61
62
# File 'app/controllers/clearance/passwords_controller.rb', line 56

def find_user_by_id_and_confirmation_token
  user_param = Clearance.configuration.user_id_parameter
  token = params[:token] || session[:password_reset_token]

  Clearance.configuration.user_model.
    find_by_id_and_confirmation_token params[user_param], token.to_s
end

#find_user_for_createObject (private)



68
69
70
71
# File 'app/controllers/clearance/passwords_controller.rb', line 68

def find_user_for_create
  Clearance.configuration.user_model.
    find_by_normalized_email(email_from_password_params)
end

#find_user_for_editObject (private)



73
74
75
# File 'app/controllers/clearance/passwords_controller.rb', line 73

def find_user_for_edit
  find_user_by_id_and_confirmation_token
end

#find_user_for_updateObject (private)



77
78
79
# File 'app/controllers/clearance/passwords_controller.rb', line 77

def find_user_for_update
  find_user_by_id_and_confirmation_token
end

#flash_failure_after_updateObject (private)



101
102
103
104
105
# File 'app/controllers/clearance/passwords_controller.rb', line 101

def flash_failure_after_update
  flash.now[:alert] = translate(:blank_password,
    scope: [:clearance, :controllers, :passwords],
    default: t("flashes.failure_after_update"))
end

#flash_failure_when_forbiddenObject (private)



95
96
97
98
99
# File 'app/controllers/clearance/passwords_controller.rb', line 95

def flash_failure_when_forbidden
  flash.now[:alert] = translate(:forbidden,
    scope: [:clearance, :controllers, :passwords],
    default: t("flashes.failure_when_forbidden"))
end

#flash_failure_when_missing_emailObject (private)



107
108
109
110
111
# File 'app/controllers/clearance/passwords_controller.rb', line 107

def flash_failure_when_missing_email
  flash.now[:alert] = translate(:missing_email,
    scope: [:clearance, :controllers, :passwords],
    default: t("flashes.failure_when_missing_email"))
end

#newObject



8
9
10
# File 'app/controllers/clearance/passwords_controller.rb', line 8

def new
  render template: "passwords/new"
end

#password_from_password_reset_paramsObject (private)



52
53
54
# File 'app/controllers/clearance/passwords_controller.rb', line 52

def password_from_password_reset_params
  params.dig(:password_reset, :password)
end

#updateObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/clearance/passwords_controller.rb', line 32

def update
  @user = find_user_for_update

  if @user.update_password(password_from_password_reset_params)
     @user
    redirect_to url_after_update
    session[:password_reset_token] = nil
  else
    flash_failure_after_update
    render template: "passwords/edit"
  end
end

#url_after_updateObject (private)



113
114
115
# File 'app/controllers/clearance/passwords_controller.rb', line 113

def url_after_update
  Clearance.configuration.redirect_url
end