Skip to content

Commit

Permalink
Readd files
Browse files Browse the repository at this point in the history
  • Loading branch information
binarylogic committed Mar 22, 2009
1 parent 769823e commit fc8d167
Show file tree
Hide file tree
Showing 45 changed files with 2,154 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/authlogic/session/activation.rb
@@ -0,0 +1,56 @@
module Authlogic
module Session
# Activating Authlogic requires that you pass it an Authlogic::ControllerAdapters::AbstractAdapter object, or a class that extends it.
# This is sort of like a database connection for an ORM library, Authlogic can't do anything until it is "connected" to a controller.
# If you are using a supported framework, Authlogic takes care of this for you.
module Activation
class NotActivatedError < ::StandardError # :nodoc:
def initialize(session)
super("You must activate the Authlogic::Session::Base.controller with a controller object before creating objects")
end
end

def self.included(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
end
end

module ClassMethods
# Returns true if a controller has been set and can be used properly. This MUST be set before anything can be done.
# Similar to how ActiveRecord won't allow you to do anything without establishing a DB connection. In your framework
# environment this is done for you, but if you are using Authlogic outside of your framework, you need to assign a controller
# object to Authlogic via Authlogic::Session::Base.controller = obj. See the controller= method for more information.
def activated?
!controller.nil?
end

# This accepts a controller object wrapped with the Authlogic controller adapter. The controller adapters close the gap
# between the different controllers in each framework. That being said, Authlogic is expecting your object's class to
# extend Authlogic::ControllerAdapters::AbstractAdapter. See Authlogic::ControllerAdapters for more info.
def controller=(value)
Thread.current[:authlogic_controller] = value
end

# The current controller object
def controller
Thread.current[:authlogic_controller]
end
end

module InstanceMethods
# Making sure we are activated before we start creating objects
def initialize(*args)
raise NotActivatedError.new(self) unless self.class.activated?
super
end

private
def controller
self.class.controller
end
end
end
end
end
89 changes: 89 additions & 0 deletions lib/authlogic/session/existence.rb
@@ -0,0 +1,89 @@
module Authlogic
module Session
# Provides methods to create and destroy objects. Basically controls their "existence".
module Existence
class SessionInvalidError < ::StandardError # :nodoc:
def initialize(session)
super("Your session is invalid and has the following errors: #{session.errors.full_messages.to_sentence}")
end
end

def self.included(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
attr_accessor :new_session, :record
end
end

module ClassMethods
# A convenince method. The same as:
#
# session = UserSession.new(*args)
# session.create
#
# Instead you can do:
#
# UserSession.create(*args)
def create(*args, &block)
session = new(*args)
session.save(&block)
end

# Same as create but calls create!, which raises an exception when validation fails.
def create!(*args)
session = new(*args)
session.save!
end
end

module InstanceMethods
# Clears all errors and the associated record, you should call this terminate a session, thus requring
# the user to authenticate again if it is needed.
def destroy
before_destroy
errors.clear
@record = nil
after_destroy
true
end

# Returns true if the session has not been saved yet.
def new_session?
new_session != false
end

# After you have specified all of the details for your session you can try to save it. This will
# run validation checks and find the associated record, if all validation passes. If validation
# does not pass, the save will fail and the erorrs will be stored in the errors object.
def save(&block)
result = nil
if valid?
self.record = attempted_record

before_save
new_session? ? before_create : before_update
new_session? ? after_create : after_update
after_save

save_record
self.new_session = false
result = true
else
result = false
end

yield result if block_given?
result
end

# Same as save but raises an exception of validation errors when validation fails
def save!
result = save
raise SessionInvalidError.new(self) unless result
result
end
end
end
end
end
63 changes: 63 additions & 0 deletions lib/authlogic/session/foundation.rb
@@ -0,0 +1,63 @@
module Authlogic
module Session
# Sort of like an interface, it sets the foundation for the class, such as the required methods. This also allows
# other modules to overwrite methods and call super on them. It's also a place to put "utility" methods used
# throughout Authlogic.
module Foundation
def self.included(klass)
klass.class_eval do
extend ClassMethods
include InstanceMethods
end
end

module ClassMethods
private
def config(key, value, default_value = nil, read_value = nil)
if value == read_value
return read_inheritable_attribute(key) if inheritable_attributes.include?(key)
write_inheritable_attribute(key, default_value)
else
write_inheritable_attribute(key, value)
end
end
end

module InstanceMethods
def initialize(*args)
self.credentials = args
end

# The credentials you passed to create your session. See credentials= for more info.
def credentials
[]
end

# Set your credentials before you save your session. You can pass a hash of credentials:
#
# session.credentials = {:login => "my login", :password => "my password", :remember_me => true}
#
# or you can pass an array of objects:
#
# session.credentails = [my_user_object, true]
#
# and if you need to set an id, just pass it last. This value need be the last item in the array you pass, since the id is something that
# you control yourself, it should never be set from a hash or a form. Examples:
#
# session.credentials = [{:login => "my login", :password => "my password", :remember_me => true}, :my_id]
# session.credentials = [my_user_object, true, :my_id]
def credentials=(values)
end

def inspect
"#<#{self.class.name}: #{credentials.blank? ? "no credentials provided" : credentials.inspect}>"
end

private
def build_key(last_part)
last_part
end
end
end
end
end
41 changes: 41 additions & 0 deletions lib/authlogic/session/id.rb
@@ -0,0 +1,41 @@
module Authlogic
module Session
# Allows you to separate sessions with an id, ultimately letting you create multiple sessions for the same user.
module Id
def self.included(klass)
klass.class_eval do
attr_writer :id
end
end

# Setting the id if it is passed in the credentials.
def credentials=(value)
super
values = value.is_a?(Array) ? value : [value]
self.id = values.last if values.last.is_a?(Symbol)
end

# Allows you to set a unique identifier for your session, so that you can have more than 1 session at a time.
# A good example when this might be needed is when you want to have a normal user session and a "secure" user session.
# The secure user session would be created only when they want to modify their billing information, or other sensitive
# information. Similar to me.com. This requires 2 user sessions. Just use an id for the "secure" session and you should be good.
#
# You can set the id during initialization (see initialize for more information), or as an attribute:
#
# session.id = :my_id
#
# Just be sure and set your id before you save your session.
#
# Lastly, to retrieve your session with the id check out the find class method.
def id
@id
end

private
# Used for things like cookie_key, session_key, etc.
def build_key(last_part)
[id, super].compact.join("_")
end
end
end
end
75 changes: 75 additions & 0 deletions lib/authlogic/session/klass.rb
@@ -0,0 +1,75 @@
module Authlogic
module Session
# Handles authenticating via a traditional username and password.
module Klass
def self.included(klass)
klass.class_eval do
extend Config
include InstanceMethods

class << self
attr_accessor :configured_klass_methods
end
end
end

module Config
# Lets you change which model to use for authentication.
#
# * <tt>Default:</tt> inferred from the class name. UserSession would automatically try User
# * <tt>Accepts:</tt> an ActiveRecord class
def authenticate_with(klass)
@klass_name = klass.name
@klass = klass
end
alias_method :authenticate_with=, :authenticate_with

# The name of the class that this session is authenticating with. For example, the UserSession class will
# authenticate with the User class unless you specify otherwise in your configuration. See authenticate_with
# for information on how to change this value.
def klass
@klass ||=
if klass_name
klass_name.constantize
else
nil
end
end

# Same as klass, just returns a string instead of the actual constant.
def klass_name
@klass_name ||=
if guessed_name = name.scan(/(.*)Session/)[0]
@klass_name = guessed_name[0]
end
end
end

module InstanceMethods
# Creating an alias method for the "record" method based on the klass name, so that we can do:
#
# session.user
#
# instead of:
#
# session.record
def initialize(*args)
if !self.class.configured_klass_methods
self.class.send(:alias_method, klass_name.demodulize.underscore.to_sym, :record)
self.class.configured_klass_methods = true
end
super
end

private
def klass
self.class.klass
end

def klass_name
self.class.klass_name
end
end
end
end
end

0 comments on commit fc8d167

Please sign in to comment.