Created
January 12, 2012 09:36
-
-
Save igor-alexandrov/1599584 to your computer and use it in GitHub Desktop.
AuthLogic controller helpers for multiple sessions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Application::Controller::Authlogic | |
def self.included(base) | |
base.extend Application::Controller::Authlogic::ClassMethods | |
end | |
module ClassMethods | |
end | |
def current_session(id=nil) | |
if User.session_ids.include?(id) | |
var = (id.nil? ? "@current_session" : "@current_#{id.to_s}_session") | |
return instance_variable_get(var) if instance_variable_defined?(var) | |
return instance_variable_set(var, User::Session.find(id)) | |
else | |
return nil | |
end | |
end | |
def current_user(id=nil) | |
if User.session_ids.include?(id) | |
var = (id.nil? ? "@current_user" : "@current_#{id.to_s}_user") | |
return instance_variable_get(var) if instance_variable_defined?(var) | |
return instance_variable_set(var, current_session(id) && current_session(id).record) | |
else | |
return nil | |
end | |
end | |
def current_company(id=nil) | |
if User.session_ids.include?(id) | |
var = (id.nil? ? "@current_company" : "@current_#{id.to_s}_company") | |
return instance_variable_get(var) if instance_variable_defined?(var) | |
return instance_variable_set(var, current_user(id).try(:company) ) | |
else | |
return nil | |
end | |
end | |
def current_ability(id=nil) | |
if User.session_ids.include?(id) | |
current_user(id).present? ? current_user(id).ability : Ability::Base.new(nil) | |
else | |
return nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment