Commit b8ff38b1 authored by Robert Speicher's avatar Robert Speicher

Refactor PasswordsController to use before_actions

parent c7b43126
class PasswordsController < Devise::PasswordsController class PasswordsController < Devise::PasswordsController
before_action :resource_from_email, only: [:create]
def create before_action :prevent_ldap_reset, only: [:create]
email = resource_params[:email] before_action :throttle_reset, only: [:create]
self.resource = resource_class.find_by_email(email)
if resource && resource.ldap_user?
flash[:alert] = "Cannot reset password for LDAP user."
respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name)) and return
end
unless can_send_reset_email?
flash[:alert] = "Instructions about how to reset your password have already been sent recently. Please wait a few minutes to try again."
respond_with({}, location: new_password_path(resource_name)) and return
end
super
end
def edit def edit
super super
...@@ -37,10 +23,24 @@ class PasswordsController < Devise::PasswordsController ...@@ -37,10 +23,24 @@ class PasswordsController < Devise::PasswordsController
end end
end end
private protected
def resource_from_email
email = resource_params[:email]
self.resource = resource_class.find_by_email(email)
end
def prevent_ldap_reset
return unless resource && resource.ldap_user?
redirect_to after_sending_reset_password_instructions_path_for(resource_name),
alert: "Cannot reset password for LDAP user."
end
def throttle_reset
return unless resource && resource.recently_sent_password_reset?
def can_send_reset_email? redirect_to new_password_path(resource_name),
resource && (resource.reset_password_sent_at.blank? || alert: I18n.t('devise.passwords.recently_reset')
resource.reset_password_sent_at < 1.minute.ago)
end end
end end
...@@ -29,7 +29,7 @@ feature 'Password reset', feature: true do ...@@ -29,7 +29,7 @@ feature 'Password reset', feature: true do
visit root_path visit root_path
forgot_password(user) forgot_password(user)
expect(page).to have_content("Instructions about how to reset your password have already been sent recently. Please wait a few minutes to try again.") expect(page).to have_content(I18n.t('devise.passwords.recently_reset'))
expect(current_path).to eq new_user_password_path expect(current_path).to eq new_user_password_path
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment