mirror of
https://github.com/heartcombo/devise.git
synced 2022-11-09 12:18:31 -05:00
moved helpers definition from routes to controllers
This commit is contained in:
parent
8df6a2f38b
commit
cb1d6c4684
7 changed files with 66 additions and 88 deletions
|
@ -256,10 +256,6 @@ module Devise
|
||||||
mattr_reader :mappings
|
mattr_reader :mappings
|
||||||
@@mappings = ActiveSupport::OrderedHash.new
|
@@mappings = ActiveSupport::OrderedHash.new
|
||||||
|
|
||||||
# Store groups.
|
|
||||||
mattr_reader :groups
|
|
||||||
@@groups = {}
|
|
||||||
|
|
||||||
# Omniauth configurations.
|
# Omniauth configurations.
|
||||||
mattr_reader :omniauth_configs
|
mattr_reader :omniauth_configs
|
||||||
@@omniauth_configs = ActiveSupport::OrderedHash.new
|
@@omniauth_configs = ActiveSupport::OrderedHash.new
|
||||||
|
@ -339,15 +335,6 @@ module Devise
|
||||||
mapping
|
mapping
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds a group to Devise.
|
|
||||||
def self.add_group(group_name, resources)
|
|
||||||
singular_sym = lambda { |thing| thing.to_s.singularize.to_sym }
|
|
||||||
|
|
||||||
group_name = singular_sym.call(group_name)
|
|
||||||
Devise.groups[group_name] = resources.map!(&singular_sym)
|
|
||||||
@@helpers.each { |h| h.define_group_helpers(group_name) }
|
|
||||||
end
|
|
||||||
|
|
||||||
# Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
|
# Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
|
||||||
#
|
#
|
||||||
# == Options:
|
# == Options:
|
||||||
|
|
|
@ -6,8 +6,12 @@ module Devise
|
||||||
include Devise::Controllers::SignInOut
|
include Devise::Controllers::SignInOut
|
||||||
include Devise::Controllers::StoreLocation
|
include Devise::Controllers::StoreLocation
|
||||||
|
|
||||||
included do
|
included do |base|
|
||||||
helper_method :warden, :signed_in?, :devise_controller?
|
helper_method :warden, :signed_in?, :devise_controller?
|
||||||
|
|
||||||
|
base.class_eval do
|
||||||
|
extend GroupHelpers
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module ClassMethods
|
module ClassMethods
|
||||||
|
@ -68,67 +72,70 @@ module Devise
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Define authentication filters and accessor helpers for a group of mappings.
|
module GroupHelpers
|
||||||
# These methods are useful when you are working with multiple mappings that
|
# Define authentication filters and accessor helpers for a group of mappings.
|
||||||
# share some functionality. They are pretty much the same as the ones
|
# These methods are useful when you are working with multiple mappings that
|
||||||
# defined for normal mappings.
|
# share some functionality. They are pretty much the same as the ones
|
||||||
#
|
# defined for normal mappings.
|
||||||
# Example:
|
#
|
||||||
#
|
# Example:
|
||||||
# Group:
|
#
|
||||||
# blogger (contains User and Admin)
|
# inside BlogsController (or any other controller, it doesn't matter which):
|
||||||
#
|
# devise_helpers_for :blogger, contains: [:user, :admin]
|
||||||
# Generated methods:
|
#
|
||||||
# authenticate_blogger! # Redirects unless user or admin are signed in
|
# Generated methods:
|
||||||
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
|
# authenticate_blogger! # Redirects unless user or admin are signed in
|
||||||
# current_blogger # Currently signed in user or admin
|
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
|
||||||
# current_bloggers # Currently signed in user and admin
|
# current_blogger # Currently signed in user or admin
|
||||||
#
|
# current_bloggers # Currently signed in user and admin
|
||||||
# Use:
|
#
|
||||||
# before_filter :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
|
# Use:
|
||||||
# before_filter ->{ authenticate_blogger! :admin } # Redirects to the admin login page
|
# before_filter :authenticate_blogger! # Redirects unless either a user or an admin are authenticated
|
||||||
# current_blogger :user # Preferably returns a User if one is signed in
|
# before_filter ->{ authenticate_blogger! :admin } # Redirects to the admin login page
|
||||||
#
|
# current_blogger :user # Preferably returns a User if one is signed in
|
||||||
def self.define_group_helpers(group_name)
|
#
|
||||||
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
def devise_group(group_name, opts={})
|
||||||
def authenticate_#{group_name}!(favourite=nil, opts={})
|
opts[:contains].map! { |m| ":#{m}" }
|
||||||
unless #{group_name}_signed_in?
|
mappings = "[#{ opts[:contains].join(',') }]"
|
||||||
mappings = Devise.groups[:#{group_name}]
|
|
||||||
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
ActionController::Base.class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
||||||
mappings.each do |mapping|
|
def authenticate_#{group_name}!(favourite=nil, opts={})
|
||||||
opts[:scope] = mapping
|
unless #{group_name}_signed_in?
|
||||||
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
|
mappings = #{mappings}
|
||||||
|
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
||||||
|
mappings.each do |mapping|
|
||||||
|
opts[:scope] = mapping
|
||||||
|
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def #{group_name}_signed_in?
|
def #{group_name}_signed_in?
|
||||||
Devise.groups[:#{group_name}].any? do |mapping|
|
#{mappings}.any? do |mapping|
|
||||||
warden.authenticate?(scope: mapping)
|
warden.authenticate?(scope: mapping)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def current_#{group_name}(favourite=nil)
|
def current_#{group_name}(favourite=nil)
|
||||||
mappings = Devise.groups[:#{group_name}]
|
mappings = #{mappings}
|
||||||
mappings.unshift(mappings.delete favourite.to_sym) if favourite
|
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
||||||
mappings.each do |mapping|
|
mappings.each do |mapping|
|
||||||
current = warden.authenticate(scope: mapping)
|
current = warden.authenticate(scope: mapping)
|
||||||
return current if current
|
return current if current
|
||||||
|
end
|
||||||
|
nil
|
||||||
end
|
end
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def current_#{group_name.to_s.pluralize}
|
def current_#{group_name.to_s.pluralize}
|
||||||
records = []
|
records = []
|
||||||
Devise.groups[:#{group_name}].each do |mapping|
|
#{mappings}.each do |mapping|
|
||||||
records << warden.authenticate(scope: mapping)
|
records << warden.authenticate(scope: mapping)
|
||||||
|
end
|
||||||
|
records.compact
|
||||||
end
|
end
|
||||||
records.compact
|
|
||||||
end
|
|
||||||
METHODS
|
|
||||||
|
|
||||||
ActiveSupport.on_load(:action_controller) do
|
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
|
||||||
helper_method "current_#{group_name}", "current_#{group_name.to_s.pluralize}", "#{group_name}_signed_in?"
|
METHODS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,6 @@ module Devise
|
||||||
def self.define_helpers(mapping)
|
def self.define_helpers(mapping)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.define_group_helpers(group_name)
|
|
||||||
end
|
|
||||||
|
|
||||||
def omniauth_authorize_path(resource_or_scope, *args)
|
def omniauth_authorize_path(resource_or_scope, *args)
|
||||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||||
_devise_route_context.send("#{scope}_omniauth_authorize_path", *args)
|
_devise_route_context.send("#{scope}_omniauth_authorize_path", *args)
|
||||||
|
|
|
@ -351,19 +351,6 @@ module ActionDispatch::Routing
|
||||||
end
|
end
|
||||||
alias :as :devise_scope
|
alias :as :devise_scope
|
||||||
|
|
||||||
# Creates a group that can contain multiple scopes in order to abstract them
|
|
||||||
# over some functionality, the only thing it really does is defining helper methods.
|
|
||||||
#
|
|
||||||
# For example
|
|
||||||
#
|
|
||||||
# devise_group :bloggers, includes: [:users, :admins]
|
|
||||||
#
|
|
||||||
# will define: current_blogger, current_bloggers, authenticate_blogger! and blogger_signed_in?
|
|
||||||
#
|
|
||||||
def devise_group(group_name, opts={})
|
|
||||||
Devise.add_group(group_name.to_s.singularize, opts[:includes])
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def devise_session(mapping, controllers) #:nodoc:
|
def devise_session(mapping, controllers) #:nodoc:
|
||||||
|
|
|
@ -26,7 +26,7 @@ class ControllerAuthenticatableTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'proxy [group]_signed_in? to authenticate? with each scope' do
|
test 'proxy [group]_signed_in? to authenticate? with each scope' do
|
||||||
Devise.groups[:commenter].each do |scope|
|
[:user, :admin].each do |scope|
|
||||||
@mock_warden.expects(:authenticate?).with(scope: scope).returns(false)
|
@mock_warden.expects(:authenticate?).with(scope: scope).returns(false)
|
||||||
end
|
end
|
||||||
@controller.commenter_signed_in?
|
@controller.commenter_signed_in?
|
||||||
|
@ -43,14 +43,14 @@ class ControllerAuthenticatableTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'proxy current_[group] to authenticate with each scope' do
|
test 'proxy current_[group] to authenticate with each scope' do
|
||||||
Devise.groups[:commenter].each do |scope|
|
[:user, :admin].each do |scope|
|
||||||
@mock_warden.expects(:authenticate).with(scope: scope).returns(nil)
|
@mock_warden.expects(:authenticate).with(scope: scope).returns(nil)
|
||||||
end
|
end
|
||||||
@controller.current_commenter
|
@controller.current_commenter
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'proxy current_[plural_group] to authenticate with each scope' do
|
test 'proxy current_[plural_group] to authenticate with each scope' do
|
||||||
Devise.groups[:commenter].each do |scope|
|
[:user, :admin].each do |scope|
|
||||||
@mock_warden.expects(:authenticate).with(scope: scope)
|
@mock_warden.expects(:authenticate).with(scope: scope)
|
||||||
end
|
end
|
||||||
@controller.current_commenters
|
@controller.current_commenters
|
||||||
|
@ -77,7 +77,7 @@ class ControllerAuthenticatableTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
test 'proxy authenticate_[group]! to authenticate!? with each scope' do
|
test 'proxy authenticate_[group]! to authenticate!? with each scope' do
|
||||||
Devise.groups[:commenter].each do |scope|
|
[:user, :admin].each do |scope|
|
||||||
@mock_warden.expects(:authenticate!).with(scope: scope)
|
@mock_warden.expects(:authenticate!).with(scope: scope)
|
||||||
@mock_warden.expects(:authenticate?).with(scope: scope).returns(false)
|
@mock_warden.expects(:authenticate?).with(scope: scope).returns(false)
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,4 +6,6 @@ class ApplicationController < ActionController::Base
|
||||||
before_filter :current_user, unless: :devise_controller?
|
before_filter :current_user, unless: :devise_controller?
|
||||||
before_filter :authenticate_user!, if: :devise_controller?
|
before_filter :authenticate_user!, if: :devise_controller?
|
||||||
respond_to *Mime::SET.map(&:to_sym)
|
respond_to *Mime::SET.map(&:to_sym)
|
||||||
|
|
||||||
|
devise_group :commenter, contains: [:user, :admin]
|
||||||
end
|
end
|
||||||
|
|
|
@ -104,7 +104,5 @@ Rails.application.routes.draw do
|
||||||
get "/unauthenticated", to: "home#unauthenticated"
|
get "/unauthenticated", to: "home#unauthenticated"
|
||||||
get "/custom_strategy/new"
|
get "/custom_strategy/new"
|
||||||
|
|
||||||
devise_group :commenters, includes: [:admins, :users]
|
|
||||||
|
|
||||||
root to: "home#index", via: [:get, :post]
|
root to: "home#index", via: [:get, :post]
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue