Sha256: 89b65099029715c8fb197a424a91a17a2c8f76a269855d4e9fdc747725258b20

Contents?: true

Size: 1.8 KB

Versions: 1

Compression:

Stored size: 1.8 KB

Contents

require 'devise_security_extension/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 self.expire_password_after.is_a? Fixnum or self.expire_password_after.is_a? Float
          self.password_changed_at.nil? or self.password_changed_at < self.expire_password_after.ago
        else
          false
        end
      end

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

      # set a fake datetime so a password change is needed
      def need_change_password
        if self.expire_password_after.is_a? Fixnum or self.expire_password_after.is_a? Float
          self.password_changed_at = self.expire_password_after.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? or self.encrypted_password_changed?) and not self.password_changed_at_changed?
        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_extension-0.10.0 lib/devise_security_extension/models/password_expirable.rb