mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
Do not include Devise internal helpers in application controller.
This commit is contained in:
parent
b5256d9765
commit
dede8af5b0
10 changed files with 47 additions and 33 deletions
|
@ -1,5 +1,5 @@
|
||||||
class ConfirmationsController < ApplicationController
|
class ConfirmationsController < ApplicationController
|
||||||
before_filter :is_devise_resource?
|
include Devise::Controllers::Helpers
|
||||||
|
|
||||||
# GET /resource/confirmation/new
|
# GET /resource/confirmation/new
|
||||||
def new
|
def new
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
class PasswordsController < ApplicationController
|
class PasswordsController < ApplicationController
|
||||||
before_filter :is_devise_resource?, :require_no_authentication
|
include Devise::Controllers::Helpers
|
||||||
|
|
||||||
|
before_filter :require_no_authentication
|
||||||
|
|
||||||
# GET /resource/password/new
|
# GET /resource/password/new
|
||||||
def new
|
def new
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
class SessionsController < ApplicationController
|
class SessionsController < ApplicationController
|
||||||
before_filter :is_devise_resource?
|
include Devise::Controllers::Helpers
|
||||||
|
|
||||||
before_filter :require_no_authentication, :only => [ :new, :create ]
|
before_filter :require_no_authentication, :only => [ :new, :create ]
|
||||||
|
|
||||||
# GET /resource/sign_in
|
# GET /resource/sign_in
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
module Devise
|
module Devise
|
||||||
module Controllers
|
module Controllers
|
||||||
|
# Those filters are convenience methods added to ApplicationController to
|
||||||
|
# deal with Warden.
|
||||||
module Filters
|
module Filters
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
|
@ -89,21 +91,6 @@ module Devise
|
||||||
METHODS
|
METHODS
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
# Helper for use in before_filters where no authentication is required.
|
|
||||||
#
|
|
||||||
# Example:
|
|
||||||
# before_filter :require_no_authentication, :only => :new
|
|
||||||
def require_no_authentication
|
|
||||||
redirect_to root_path if warden.authenticated?(resource_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Checks whether it's a devise mapped resource or not.
|
|
||||||
def is_devise_resource? #:nodoc:
|
|
||||||
raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
module Devise
|
module Devise
|
||||||
module Controllers
|
module Controllers
|
||||||
|
# Those helpers are used only inside Devise controllers and should not be
|
||||||
|
# included in ApplicationController since they all depend on the url being
|
||||||
|
# accessed.
|
||||||
module Helpers
|
module Helpers
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.class_eval do
|
base.class_eval do
|
||||||
helper_method :resource, :resource_name, :resource_class, :devise_mapping
|
helper_method :resource, :resource_name, :resource_class, :devise_mapping
|
||||||
|
hide_action :resource, :resource_name, :resource_class, :devise_mapping
|
||||||
|
|
||||||
|
before_filter :is_devise_resource?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,6 +29,11 @@ module Devise
|
||||||
devise_mapping.to
|
devise_mapping.to
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Attempt to find the mapped route for devise based on request path
|
||||||
|
def devise_mapping
|
||||||
|
@devise_mapping ||= Devise.find_mapping_by_path(request.path)
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# Redirects to stored uri before signing in or the default path and clear
|
# Redirects to stored uri before signing in or the default path and clear
|
||||||
|
@ -57,9 +68,9 @@ module Devise
|
||||||
respond_to?(home_path, true) ? send(home_path) : root_path
|
respond_to?(home_path, true) ? send(home_path) : root_path
|
||||||
end
|
end
|
||||||
|
|
||||||
# Attempt to find the mapped route for devise based on request path
|
# Checks whether it's a devise mapped resource or not.
|
||||||
def devise_mapping
|
def is_devise_resource? #:nodoc:
|
||||||
@devise_mapping ||= Devise.find_mapping_by_path(request.path)
|
raise ActionController::UnknownAction unless devise_mapping && devise_mapping.allows?(controller_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets the resource creating an instance variable
|
# Sets the resource creating an instance variable
|
||||||
|
@ -67,6 +78,14 @@ module Devise
|
||||||
instance_variable_set(:"@#{resource_name}", new_resource)
|
instance_variable_set(:"@#{resource_name}", new_resource)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Helper for use in before_filters where no authentication is required.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# before_filter :require_no_authentication, :only => :new
|
||||||
|
def require_no_authentication
|
||||||
|
redirect_to root_path if warden.authenticated?(resource_name)
|
||||||
|
end
|
||||||
|
|
||||||
# Sets the flash message with :key, using I18n. By default you are able
|
# Sets the flash message with :key, using I18n. By default you are able
|
||||||
# to setup your messages using specific resource scope, and if no one is
|
# to setup your messages using specific resource scope, and if no one is
|
||||||
# found we look to default scope.
|
# found we look to default scope.
|
||||||
|
|
|
@ -15,12 +15,14 @@ module Devise
|
||||||
#
|
#
|
||||||
# new_confirmation_path(:user) => new_user_confirmation_path
|
# new_confirmation_path(:user) => new_user_confirmation_path
|
||||||
# confirmation_path(:user) => user_confirmation_path
|
# confirmation_path(:user) => user_confirmation_path
|
||||||
|
#
|
||||||
|
# Those helpers are added to your ApplicationController.
|
||||||
module UrlHelpers
|
module UrlHelpers
|
||||||
|
|
||||||
[:session, :password, :confirmation].each do |module_name|
|
[:session, :password, :confirmation].each do |module_name|
|
||||||
[:path, :url].each do |path_or_url|
|
[:path, :url].each do |path_or_url|
|
||||||
actions = [ nil, :new_ ]
|
actions = [ nil, :new_ ]
|
||||||
actions << :edit_ if module_name == :password
|
actions << :edit_ if module_name == :password
|
||||||
actions << :destroy_ if module_name == :session
|
actions << :destroy_ if module_name == :session
|
||||||
|
|
||||||
actions.each do |action|
|
actions.each do |action|
|
||||||
|
|
|
@ -8,7 +8,6 @@ module ActionController::Routing
|
||||||
load_routes_without_devise!
|
load_routes_without_devise!
|
||||||
|
|
||||||
ActionController::Base.send :include, Devise::Controllers::Filters
|
ActionController::Base.send :include, Devise::Controllers::Filters
|
||||||
ActionController::Base.send :include, Devise::Controllers::Helpers
|
|
||||||
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
|
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
|
||||||
|
|
||||||
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
|
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
|
||||||
|
|
|
@ -83,13 +83,6 @@ class ControllerAuthenticableTest < ActionController::TestCase
|
||||||
@controller.admin_session
|
@controller.admin_session
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'require no authentication tests current mapping' do
|
|
||||||
@controller.expects(:resource_name).returns(:user)
|
|
||||||
@mock_warden.expects(:authenticated?).with(:user).returns(true)
|
|
||||||
@controller.expects(:redirect_to).with(root_path)
|
|
||||||
@controller.send :require_no_authentication
|
|
||||||
end
|
|
||||||
|
|
||||||
test 'sign in automatically proxy to set user on warden' do
|
test 'sign in automatically proxy to set user on warden' do
|
||||||
@mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true)
|
@mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true)
|
||||||
@controller.sign_in(:user, user)
|
@controller.sign_in(:user, user)
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
require 'test/test_helper'
|
require 'test/test_helper'
|
||||||
|
|
||||||
|
class MyController < ApplicationController
|
||||||
|
include Devise::Controllers::Helpers
|
||||||
|
end
|
||||||
|
|
||||||
class HelpersTest < ActionController::TestCase
|
class HelpersTest < ActionController::TestCase
|
||||||
tests ApplicationController
|
tests MyController
|
||||||
|
|
||||||
test 'get resource name from request path' do
|
test 'get resource name from request path' do
|
||||||
@request.path = '/users/session'
|
@request.path = '/users/session'
|
||||||
|
@ -37,4 +41,11 @@ class HelpersTest < ActionController::TestCase
|
||||||
test 'resources methods are not controller actions' do
|
test 'resources methods are not controller actions' do
|
||||||
assert @controller.class.action_methods.empty?
|
assert @controller.class.action_methods.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'require no authentication tests current mapping' do
|
||||||
|
@controller.expects(:resource_name).returns(:user)
|
||||||
|
@mock_warden.expects(:authenticated?).with(:user).returns(true)
|
||||||
|
@controller.expects(:redirect_to).with(root_path)
|
||||||
|
@controller.send :require_no_authentication
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,10 +25,10 @@ class FailureTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'setup a default message' do
|
test 'setup a default message' do
|
||||||
assert_equal 'You are being redirected to /users/sign_in', call_failure.last
|
assert_equal ['You are being redirected to /users/sign_in'], call_failure.last
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'pass in a different message' do
|
test 'pass in a different message' do
|
||||||
assert_equal 'Hello world', call_failure(:message => 'Hello world').last
|
assert_equal ['Hello world'], call_failure(:message => 'Hello world').last
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue