Sha256: f33fdf4a93ae150440d5766b5f7597382f123c1e1d6a819dee444a2c47bfb82f

Contents?: true

Size: 1.87 KB

Versions: 1

Compression:

Stored size: 1.87 KB

Contents

require 'devise-security/hooks/password_expirable'

module Devise
  module Models

    # PasswordExpirable takes care of change password after
    module PasswordExpirable
      extend ActiveSupport::Concern

      included do
        before_save :update_password_changed
      end

      # is an password change required?
      def need_change_password?
        if expired_password_after_numeric?
          self.password_changed_at.nil? || self.password_changed_at < self.expire_password_after.seconds.ago
        else
          false
        end
      end

      # set a fake datetime so a password change is needed and save the record
      def need_change_password!
        if expired_password_after_numeric?
          need_change_password
          self.save(:validate => false)
        end
      end

      # set a fake datetime so a password change is needed
      def need_change_password
        if expired_password_after_numeric?
          self.password_changed_at = self.expire_password_after.seconds.ago
        end

        # is date not set it will set default to need set new password next login
        need_change_password if self.password_changed_at.nil?

        self.password_changed_at
      end

      def expire_password_after
        self.class.expire_password_after
      end

      private

        # is password changed then update password_cahanged_at
        def update_password_changed
          self.password_changed_at = Time.now if (self.new_record? || self.encrypted_password_changed?) && !self.password_changed_at_changed?
        end

        def expired_password_after_numeric?
          return @_numeric if defined?(@_numeric)
          @_numeric ||= self.expire_password_after.is_a?(1.class) ||
            self.expire_password_after.is_a?(Float)
        end

      module ClassMethods
        ::Devise::Models.config(self, :expire_password_after)
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise-security-0.11.1 lib/devise-security/models/password_expirable.rb