Created
May 17, 2012 02:38
-
-
Save lscott3/2715797 to your computer and use it in GitHub Desktop.
Full Authentication, Authlogic
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
class UserSession < Authlogic::Session::Base | |
# Just telling Authlogic to use our method. | |
find_by_login_method :find_user_and_account | |
end |
This file contains 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
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