Andrey Khataev
Andrey Khataev

Reputation: 1343

how to override devise security extension PasswordExpiredController

I want to override show logic in order to permit change password not only if it is expired, but some days earlier. I need to modify or replace this before_filter logic

def skip_password_change
    return if !resource.nil? && resource.need_change_password?
    redirect_to :root
end

I want to make my controller like this:

class PasswordsController < Devise::PasswordExpiredController
    # ...
    def skip_password_change
        return if !resource.nil? && (resource.need_change_password? || ... )
        redirect_to :root
    end
end

How to achieve my goal?

UPD: all answers below are kind of right, I missed one thing - my custom controller was placed inside controllers/admin directory, so I should name it Admin::CustomPasswordExpiredController, but I missed Namespace prefix Admin and rails fell into circular dependency.

Upvotes: 1

Views: 1058

Answers (3)

nikolayp
nikolayp

Reputation: 17919

Just extend devise controller with your custom controller:

# config/routes.rb
devise_for :users, controllers: { passwords: 'custom_passwords' }

# app/controllers/custom_passwords_controller.rb
class CustomPasswordsController < Devise::PasswordsController
  def edit
    resource = resource_class.new
    return unless resource.need_change_password? # your middleware logic here
    super
  end
end

More facilities can be found in devise PasswordsController documentation (by clicking "View source")

Upvotes: 1

Arisah Ijeoma
Arisah Ijeoma

Reputation: 21

Go to your routes file and overwrite the controller methods there.

Something like devise_for :users, controllers: {x: 'y'} where x is the name of the controller from devise that you want to overwrite and y is the name of your custom controller you want to overwrite with

Upvotes: 1

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You can try this workaround, first skip the default before_action for skip_password_change method, then add a custom method to wrap it in a condition. Try this

class PasswordsController < Devise::PasswordExpiredController
  skip_before_action :skip_password_change, only: :show
  before_action :skip_password_change_show, only: :show

  def skip_password_change_show
    return if !resource.nil? && (resource.need_change_password? || #yourcondition )
    redirect_to :root
  end
end

Hope that helps!

Upvotes: 1

Related Questions