Sha256: 94eeb7a710cf3a10eff3cf5dfe5a5c9f300d0a450c6df256dd0d31c988eac8c7

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

# TODO: Make User not hard-coded
module ClassyCAS
  module Strategies
    class DeviseDatabase < Base
      attr_accessor :authentication_hash, :password
      
      def valid?
        params_authenticatable? && with_authentication_hash(params)
      end
      
      def authenticate!
        resource = valid_password? && User.find_for_database_authentication(authentication_hash)

        if validate(resource){ resource.valid_password?(password) }
          resource.after_database_authentication
          success!(resource)
        else
          fail(:invalid)
        end
      end
      
      private
      # Simply invokes valid_for_authentication? with the given block and deal with the result.
      def validate(resource, &block)
        result = resource && resource.valid_for_authentication?(&block)

        case result
        when Symbol, String
          fail!(result)
        else
          result
        end
      end

      def valid_password?
        password.present?
      end

      # Check if the model accepts this strategy as params authenticatable.
      def params_authenticatable?
        User.params_authenticatable?(authenticatable_name)
      end

      # Sets the authentication hash and the password from params_auth_hash or http_auth_hash.
      def with_authentication_hash(hash)
        self.authentication_hash = {:email => hash[:username]}
        self.password = hash[:password]
      end

      # Holds the authentication keys.
      def authentication_keys
        @authentication_keys ||= User.authentication_keys
      end

      # Holds the authenticatable name for this class. Devise::Strategies::DatabaseAuthenticatable
      # becomes simply :database.
      def authenticatable_name
        @authenticatable_name ||=
          self.class.name.split("::").last.underscore.sub("_authenticatable", "").to_sym
      end
    end
  end
end

Warden::Strategies.add(:cas_devise_database, ClassyCAS::Strategies::DeviseDatabase)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
classy_cas-0.9.3 lib/strategies/devise_database.rb
classy_cas-0.9.2 lib/strategies/devise_database.rb
classy_cas-0.9.1 lib/strategies/devise_database.rb