Skip to content

Commit

Permalink
Renamed Clearance::App:Controller::ApplicationController to Clearance…
Browse files Browse the repository at this point in the history
…::Authentication
  • Loading branch information
jferris committed Mar 26, 2009
1 parent a5a901f commit 6762956
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 93 deletions.
2 changes: 1 addition & 1 deletion app/controllers/confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ConfirmationsController < ActionController::Base

include Clearance::App::Controllers::ApplicationController
include Clearance::Authentication

before_filter :forbid_confirmed_user, :only => :new
before_filter :forbid_missing_token, :only => :new
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class PasswordsController < ActionController::Base

include Clearance::App::Controllers::ApplicationController
include Clearance::Authentication

before_filter :forbid_missing_token, :only => [:edit, :update]
before_filter :forbid_non_existant_user, :only => [:edit, :update]
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class SessionsController < ActionController::Base

include Clearance::App::Controllers::ApplicationController
include Clearance::Authentication

protect_from_forgery :except => :create
filter_parameter_logging :password
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class UsersController < ActionController::Base

include Clearance::App::Controllers::ApplicationController
include Clearance::Authentication

before_filter :redirect_to_root, :only => [:new, :create], :if => :signed_in?
filter_parameter_logging :password
Expand Down
4 changes: 2 additions & 2 deletions generators/clearance/clearance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def manifest
file = "app/controllers/application.rb"
end
if File.exists?(file)
m.insert_into file, "include Clearance::App::Controllers::ApplicationController"
else
m.insert_into file, "include Clearance::Authentication"
else # will this file ever not exist?
m.file file, file
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
include Clearance::App::Controllers::ApplicationController
include Clearance::Authentication
end
2 changes: 1 addition & 1 deletion lib/clearance.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'clearance/lib/extensions/errors'
require 'clearance/lib/extensions/rescue'
require 'clearance/app/controllers/application_controller'
require 'clearance/authentication'
require 'clearance/app/models/clearance_mailer'
require 'clearance/app/models/user'
require 'clearance/test/functional/confirmations_controller_test'
Expand Down
84 changes: 0 additions & 84 deletions lib/clearance/app/controllers/application_controller.rb

This file was deleted.

80 changes: 80 additions & 0 deletions lib/clearance/authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
module Clearance
module Authentication

def self.included(controller)
controller.send(:include, InstanceMethods)

controller.class_eval do
helper_method :current_user
helper_method :signed_in?

hide_action :current_user, :signed_in?
end
end

module InstanceMethods
def current_user
@_current_user ||= (user_from_cookie || user_from_session)
end

def signed_in?
! current_user.nil?
end

protected

def authenticate
deny_access unless signed_in?
end

def user_from_session
if session[:user_id]
return nil unless user = User.find_by_id(session[:user_id])
return user if user.email_confirmed?
end
end

def user_from_cookie
if token = cookies[:remember_token]
return nil unless user = User.find_by_token(token)
return user if user.remember?
end
end

def sign_user_in(user)
sign_in(user)
end

def sign_in(user)
if user
session[:user_id] = user.id
end
end

def redirect_back_or(default)
session[:return_to] ||= params[:return_to]
if session[:return_to]
redirect_to(session[:return_to])
else
redirect_to(default)
end
session[:return_to] = nil
end

def redirect_to_root
redirect_to root_url
end

def store_location
session[:return_to] = request.request_uri if request.get?
end

def deny_access(flash_message = nil, opts = {})
store_location
flash[:failure] = flash_message if flash_message
redirect_to new_session_url
end
end

end
end
2 changes: 1 addition & 1 deletion test/rails_root/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery
include Clearance::App::Controllers::ApplicationController
include Clearance::Authentication
end

0 comments on commit 6762956

Please sign in to comment.