Skip to content

Instantly share code, notes, and snippets.

@lscott3
Created May 17, 2012 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lscott3/2715797 to your computer and use it in GitHub Desktop.
Save lscott3/2715797 to your computer and use it in GitHub Desktop.
Full Authentication, Authlogic
class ApplicationController < ActionController::Base
helper_method :current_account, :current_user # you may have others that go here too
private
# I added this so that way I can just use current_account just like current_user
def current_account
@current_account = Account.find_by_subdomain(request.subdomain)
return @current_account
end
def current_user_session
return @current_user_session if defined?(@current_user_session)
# This line here is the main thing that you need to add.
# Notice the current_account.user_sessions.find
@current_user_session = current_account.user_sessions.find unless current_account.nil?
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
end
class User < ActiveRecord::Base
belongs_to :account
acts_as_authentic do |c|
c.validations_scope = :account_id
end
def self.find_user_and_account(login)
find_by_email_and_account_id(login, $accountID) # we use the global here.
end
end
class UserSession < Authlogic::Session::Base
# Just telling Authlogic to use our method.
find_by_login_method :find_user_and_account
end
class UserSessionsController < ApplicationController
def create
$accountID=current_account.id # Here is the global
@user_session = current_account.user_sessions.build(params[:user_session])
if @user_session.save
redirect_to dashboard_url, :notice => "Login Successful!"
else
render :action => :new
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment