Sha256: b775f58ed0ba47c51f787a8c144756601cf0a0c188630e620b879ff2c2c2eab8

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Ubiquitously
  module Account
    module Authorizable
      def self.included(base)
        base.extend ClassMethods
        base.validates_presence_of :username, :password
        base.send :include, InstanceMethods
      end
      
      module ClassMethods
        def uses(*protocols)
          @uses = protocols.flatten.map(&:to_s) unless protocols.blank?
          @uses ||= []
          @uses
        end
        
        def uses?(protocol)
          uses.include?(protocol.to_s)
        end
      end
      
      module InstanceMethods
        def authorize!(condition)
          authorize(condition)
          
          unless logged_in?
            raise AuthenticationError.new("Invalid username or password for #{service.titleize}")
          end
          
          logged_in?
        end
        
        def authorize(condition)
          @logged_in = !!condition
        end
        
        def logged_in?
          @logged_in == true
        end
        
        def uses?(protocol)
          self.class.uses?(protocol)
        end
        
        def requires_credentials?
          uses?(:oauth)
        end
        
        def authorized?
          return true if logged_in?
          return (credentials? && cookies?) if requires_credentials?
          return cookies?
        end
        
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ubiquitously-0.1.0 lib/ubiquitously/models/service/account/authorizable.rb