Module: Authlogic::Session::Scopes

Included in:
Base
Defined in:
lib/authlogic/session/scopes.rb

Overview

Scopes

Authentication can be scoped, but scoping authentication can get a little tricky. Checkout the section “Scoping” in the readme for more details.

What with_scopes focuses on is scoping the query when finding the object and the name of the cookie / session. It works very similar to ActiveRecord::Base#with_scopes. It accepts a hash with any of the following options:

  • find_options: any options you can pass into ActiveRecord::Base.find. This is used when trying to find the record.

  • id: The id of the session, this gets merged with the real id. For information ids see the id method.

Here is how you use it:

UserSession.with_scope(:find_options => {:conditions => "account_id = 2"}, :id => "account_2") do
  UserSession.find
end

Eseentially what the above does is scope the searching of the object with the sql you provided. So instead of:

User.find(:first, :conditions => "login = 'ben'")

it would be:

User.find(:first, :conditions => "login = 'ben' and account_id = 2")

You will also notice the :id option. This works just like the id method. It scopes your cookies. So the name of your cookie will be:


instead of:

user_credentials

What is also nifty about scoping with an :id is that it merges your id's. So if you do:

UserSession.with_scope(:find_options => {:conditions => "account_id = 2"}, :id => "account_2") do
  session = UserSession.new
  session.id = :secure
end

The name of your cookies will be:


Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/authlogic/session/scopes.rb', line 46

def self.included(klass)
  klass.extend(ClassMethods)
  klass.send(:include, InstanceMethods)
  klass.class_eval do
    attr_writer :scope
    alias_method_chain :initialize, :scopes
    alias_method_chain :search_for_record, :scopes
  end
end