Sha256: 966eaa3fb52be088f6dcf8ee3b908a3a55cd01d152194023653f6bc21fec2689

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

module DeviseExtension
  module Controllers
    module Helpers
      extend ActiveSupport::Concern

      included do
        before_filter :handle_password_change
      end

      # controller instance methods
      private

      # lookup if an password change needed
      def handle_password_change
        return if warden.nil?
        if not devise_controller? and not ignore_password_expire? and not request.format.nil? #and request.format.html?
          Devise.mappings.keys.flatten.any? do |scope|
            if signed_in?(scope) and warden.session(scope)['password_expired']
              # re-check to avoid infinite loop if date changed after login attempt
              if send(:"current_#{scope}").try(:need_change_password?)
                session["#{scope}_return_to"] = request.original_fullpath if request.get?
                redirect_for_password_change scope
                return
              else
                warden.session(scope)[:password_expired] = false
              end
            end
          end
        end
      end

      # redirect for password update with alert message
      def redirect_for_password_change(scope)
        redirect_to change_password_required_path_for(scope), alert: 'Your password has expired. Please renew your password.'
      end

      # path for change password
      def change_password_required_path_for(resource_or_scope = nil)
        scope       = Devise::Mapping.find_scope!(resource_or_scope)
        change_path = "#{scope}_password_expired_path"
        send(change_path)
      end

      protected

      # allow to overwrite for some special handlings
      def ignore_password_expire?
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mno-enterprise-core-3.4.0 lib/devise/controllers/extension_helpers.rb
mno-enterprise-core-3.3.3 lib/devise/controllers/extension_helpers.rb