Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Authentication, User to Controller, Model #268

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
clearance (1.0.0.rc4)
clearance (1.0.0.rc5)
bcrypt-ruby
email_validator
rails (>= 3.0)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -35,8 +35,8 @@ Make sure the development database exists. Then, run the generator:

The generator:

* inserts Clearance::User into your User model
* inserts Clearance::Authentication into your ApplicationController
* inserts `Clearance::User` into your `User` model
* inserts `Clearance::Controller` into your `ApplicationController`
* creates a migration that either creates a users table or adds only missing
columns

Expand Down Expand Up @@ -327,7 +327,7 @@ For example, in `spec/support/clearance.rb` or `test/test_helper.rb`:

require 'clearance/testing'

This will make `Clearance::Authentication` methods work in your controllers
This will make `Clearance::Controller` methods work in your controllers
during functional tests and provide access to helper methods like:

sign_in
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/3.0.20.gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: /Users/croaky/dev/clearance
specs:
clearance (1.0.0.rc4)
clearance (1.0.0.rc5)
bcrypt-ruby
email_validator
rails (>= 3.0)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/3.1.11.gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: /Users/croaky/dev/clearance
specs:
clearance (1.0.0.rc4)
clearance (1.0.0.rc5)
bcrypt-ruby
email_validator
rails (>= 3.0)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/3.2.12.gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: /Users/croaky/dev/clearance
specs:
clearance (1.0.0.rc4)
clearance (1.0.0.rc5)
bcrypt-ruby
email_validator
rails (>= 3.0)
Expand Down
2 changes: 1 addition & 1 deletion lib/clearance.rb
Expand Up @@ -2,7 +2,7 @@
require 'clearance/session'
require 'clearance/rack_session'
require 'clearance/back_door'
require 'clearance/authentication'
require 'clearance/controller'
require 'clearance/user'
require 'clearance/engine'
require 'clearance/password_strategies'
Expand Down
61 changes: 8 additions & 53 deletions lib/clearance/authentication.rb
Expand Up @@ -4,8 +4,14 @@ module Authentication

included do
helper_method :current_user, :signed_in?, :signed_out?
hide_action :authorize, :current_user, :current_user=, :deny_access,
:sign_in, :sign_out, :signed_in?, :signed_out?
hide_action(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just make these methods private, they don't need to be hidden.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calebthompson If we make them private methods, we get errors like the following:

private method `signed_in?' called for #<ForgeriesController:0x007f8338c2dc48>
private method `current_user=' called for #<Clearance::SessionsController:0x007f833c8bd848>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense. Why aren't these in a helper module?

On Sun, Mar 3, 2013 at 7:31 PM, Dan Croak notifications@github.com
wrote:

@@ -4,8 +4,14 @@ module Authentication

 included do
   helper_method :current_user, :signed_in?, :signed_out?
  •  hide_action :authorize, :current_user, :current_user=, :deny_access,
    
  •    :sign_in, :sign_out, :signed_in?, :signed_out?
    
  •  hide_action(
    
    @calebthompson If we make them private methods, we get errors like the following:
private method `signed_in?' called for #<ForgeriesController:0x007f8338c2dc48>
private method `current_user=' called for #<Clearance::SessionsController:0x007f833c8bd848>

Reply to this email directly or view it on GitHub:
https://github.com/thoughtbot/clearance/pull/268/files#r3219887

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I know what you mean by a helper module. Are you referring to the helper method?

http://api.rubyonrails.org/classes/AbstractController/Helpers/ClassMethods.html#method-i-helper

If the goal is to make current_user, signed_in?, and signed_out? available to view templates, that's handled by line 6. The other methods are intended to be used by controllers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's clear now. Never mind, I should have paid more attention to what the hidden actions were.

On Sun, Mar 3, 2013 at 8:04 PM, Dan Croak notifications@github.com
wrote:

@@ -4,8 +4,14 @@ module Authentication

 included do
   helper_method :current_user, :signed_in?, :signed_out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, cool. No worries. Thanks for the comments. Got me wondering whether this stuff was still necessary on current versions of Rails and refreshed my memory on how hide_action and module visibility worked.

:current_user,
:current_user=,
:sign_in,
:sign_out,
:signed_in?,
:signed_out?
)
end

def authenticate(params)
Expand All @@ -14,12 +20,6 @@ def authenticate(params)
)
end

def authorize
unless signed_in?
deny_access
end
end

def current_user
clearance_session.current_user
end
Expand All @@ -28,20 +28,6 @@ def current_user=(user)
clearance_session.sign_in user
end

def deny_access(flash_message = nil)
store_location

if flash_message
flash[:notice] = flash_message
end

if signed_in?
redirect_to url_after_denied_access_when_signed_in
else
redirect_to url_after_denied_access_when_signed_out
end
end

def sign_in(user)
clearance_session.sign_in user
end
Expand All @@ -67,39 +53,8 @@ def handle_unverified_request

protected

def clear_return_to
session[:return_to] = nil
end

def clearance_session
request.env[:clearance]
end

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

def redirect_back_or(default)
redirect_to(return_to || default)
clear_return_to
end

def redirect_to_root
redirect_to('/')
end

def return_to
session[:return_to] || params[:return_to]
end

def url_after_denied_access_when_signed_in
'/'
end

def url_after_denied_access_when_signed_out
sign_in_url
end
end
end
62 changes: 62 additions & 0 deletions lib/clearance/authorization.rb
@@ -0,0 +1,62 @@
module Clearance
module Authorization
extend ActiveSupport::Concern

included do
hide_action :authorize, :deny_access
end

def authorize
unless signed_in?
deny_access
end
end

def deny_access(flash_message = nil)
store_location

if flash_message
flash[:notice] = flash_message
end

if signed_in?
redirect_to url_after_denied_access_when_signed_in
else
redirect_to url_after_denied_access_when_signed_out
end
end

protected

def clear_return_to
session[:return_to] = nil
end

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

def redirect_back_or(default)
redirect_to(return_to || default)
clear_return_to
end

def redirect_to_root
redirect_to('/')
end

def return_to
session[:return_to] || params[:return_to]
end

def url_after_denied_access_when_signed_in
'/'
end

def url_after_denied_access_when_signed_out
sign_in_url
end
end
end
2 changes: 1 addition & 1 deletion lib/clearance/back_door.rb
Expand Up @@ -34,7 +34,7 @@ def sign_in_through_the_back_door(env)
user_id = params['as']

if user_id.present?
user = ::User.find(user_id)
user = Clearance.configuration.user_model.find(user_id)
env[:clearance].sign_in(user)
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/clearance/controller.rb
@@ -0,0 +1,11 @@
require 'clearance/authentication'
require 'clearance/authorization'

module Clearance
module Controller
extend ActiveSupport::Concern

include Clearance::Authentication
include Clearance::Authorization
end
end
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
include Clearance::Authentication
include Clearance::Controller

def show
render :text => '', :layout => 'application'
Expand Down
6 changes: 4 additions & 2 deletions lib/clearance/user.rb
Expand Up @@ -11,8 +11,10 @@ module User

include Validations
include Callbacks
include (Clearance.configuration.password_strategy ||
Clearance::PasswordStrategies::BCrypt)
include(
Clearance.configuration.password_strategy ||
Clearance::PasswordStrategies::BCrypt
)
end

module ClassMethods
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/clearance/install/install_generator.rb
Expand Up @@ -15,7 +15,7 @@ def inject_clearance_into_application_controller
inject_into(
ApplicationController,
'app/controllers/application_controller.rb',
'include Clearance::Authentication'
'include Clearance::Controller'
)
end

Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/denies_controller_spec.rb
@@ -1,7 +1,8 @@
require 'spec_helper'

class DeniesController < ActionController::Base
include Clearance::Authentication
include Clearance::Controller

before_filter :authorize, :only => :show

def new
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/flashes_controller_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'

class FlashesController < ActionController::Base
include Clearance::Authentication
include Clearance::Controller

def set_flash
flash[:notice] = params[:message]
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/forgeries_controller_spec.rb
@@ -1,7 +1,8 @@
require 'spec_helper'

class ForgeriesController < ActionController::Base
include Clearance::Authentication
include Clearance::Controller

protect_from_forgery
before_filter :authorize

Expand Down
1 change: 0 additions & 1 deletion spec/models/bcrypt_migration_from_sha1_spec.rb
Expand Up @@ -30,7 +30,6 @@
it 'sets the pasword on the subject' do
subject.password.should be_present
end

end

describe '#authenticated?' do
Expand Down
20 changes: 7 additions & 13 deletions spec/models/user_spec.rb
Expand Up @@ -33,33 +33,28 @@
end

it 'is authenticated with correct email and password' do
(Clearance.configuration.user_model.authenticate(@user.email, @password)).
should be
User.authenticate(@user.email, @password).should eq(@user)
@user.should be_authenticated(@password)
end

it 'is authenticated with correct uppercased email and correct password' do
(Clearance.configuration.user_model.authenticate(@user.email.upcase, @password)).
should be
User.authenticate(@user.email.upcase, @password).should eq(@user)
@user.should be_authenticated(@password)
end

it 'is authenticated with incorrect credentials' do
(Clearance.configuration.user_model.authenticate(@user.email, 'bad_password')).
should_not be
User.authenticate(@user.email, 'bad_password').should be_nil
@user.should_not be_authenticated('bad password')
end

it 'is retrieved via a case-insensitive search' do
(Clearance.configuration.user_model.find_by_normalized_email(@user.email.upcase)).
should be
@user
User.find_by_normalized_email(@user.email.upcase).should eq(@user)
end
end

describe 'when resetting authentication with reset_remember_token!' do
before do
@user = create(:user)
@user = create(:user)
@user.remember_token = 'old-token'
@user.reset_remember_token!
end
Expand Down Expand Up @@ -145,6 +140,7 @@
describe 'a user with an optional email' do
before do
@user = User.new

class << @user
def email_optional?
true
Expand Down Expand Up @@ -185,9 +181,7 @@ def password_optional?
let(:email) { 'Jo hn.Do e @exa mp le.c om' }

it 'downcases the address and strips spaces' do
(Clearance.configuration.user_model.normalize_email(email)).
should be
'john.doe@example.com'
User.normalize_email(email).should eq 'john.doe@example.com'
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/clearance.rb
Expand Up @@ -4,7 +4,7 @@
end

class ApplicationController < ActionController::Base
include Clearance::Authentication
include Clearance::Controller
end

class User < ActiveRecord::Base
Expand Down