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
|
||||
@@mappings = ActiveSupport::OrderedHash.new
|
||||
|
||||
# Store groups.
|
||||
mattr_reader :groups
|
||||
@@groups = {}
|
||||
|
||||
# Omniauth configurations.
|
||||
mattr_reader :omniauth_configs
|
||||
@@omniauth_configs = ActiveSupport::OrderedHash.new
|
||||
|
@ -339,15 +335,6 @@ module Devise
|
|||
mapping
|
||||
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.
|
||||
#
|
||||
# == Options:
|
||||
|
|
|
@ -6,8 +6,12 @@ module Devise
|
|||
include Devise::Controllers::SignInOut
|
||||
include Devise::Controllers::StoreLocation
|
||||
|
||||
included do
|
||||
included do |base|
|
||||
helper_method :warden, :signed_in?, :devise_controller?
|
||||
|
||||
base.class_eval do
|
||||
extend GroupHelpers
|
||||
end
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
@ -68,67 +72,70 @@ module Devise
|
|||
end
|
||||
end
|
||||
|
||||
# Define authentication filters and accessor helpers for a group of mappings.
|
||||
# These methods are useful when you are working with multiple mappings that
|
||||
# share some functionality. They are pretty much the same as the ones
|
||||
# defined for normal mappings.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# Group:
|
||||
# blogger (contains User and Admin)
|
||||
#
|
||||
# Generated methods:
|
||||
# authenticate_blogger! # Redirects unless user or admin are signed in
|
||||
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
|
||||
# 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
|
||||
# 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 authenticate_#{group_name}!(favourite=nil, opts={})
|
||||
unless #{group_name}_signed_in?
|
||||
mappings = Devise.groups[:#{group_name}]
|
||||
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)
|
||||
module GroupHelpers
|
||||
# Define authentication filters and accessor helpers for a group of mappings.
|
||||
# These methods are useful when you are working with multiple mappings that
|
||||
# share some functionality. They are pretty much the same as the ones
|
||||
# defined for normal mappings.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# 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
|
||||
# blogger_signed_in? # Checks whether there is either a user or an admin signed in
|
||||
# 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
|
||||
# before_filter ->{ authenticate_blogger! :admin } # Redirects to the admin login page
|
||||
# current_blogger :user # Preferably returns a User if one is signed in
|
||||
#
|
||||
def devise_group(group_name, opts={})
|
||||
opts[:contains].map! { |m| ":#{m}" }
|
||||
mappings = "[#{ opts[:contains].join(',') }]"
|
||||
|
||||
ActionController::Base.class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
||||
def authenticate_#{group_name}!(favourite=nil, opts={})
|
||||
unless #{group_name}_signed_in?
|
||||
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
|
||||
|
||||
def #{group_name}_signed_in?
|
||||
Devise.groups[:#{group_name}].any? do |mapping|
|
||||
warden.authenticate?(scope: mapping)
|
||||
def #{group_name}_signed_in?
|
||||
#{mappings}.any? do |mapping|
|
||||
warden.authenticate?(scope: mapping)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def current_#{group_name}(favourite=nil)
|
||||
mappings = Devise.groups[:#{group_name}]
|
||||
mappings.unshift(mappings.delete favourite.to_sym) if favourite
|
||||
mappings.each do |mapping|
|
||||
current = warden.authenticate(scope: mapping)
|
||||
return current if current
|
||||
def current_#{group_name}(favourite=nil)
|
||||
mappings = #{mappings}
|
||||
mappings.unshift mappings.delete(favourite.to_sym) if favourite
|
||||
mappings.each do |mapping|
|
||||
current = warden.authenticate(scope: mapping)
|
||||
return current if current
|
||||
end
|
||||
nil
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
def current_#{group_name.to_s.pluralize}
|
||||
records = []
|
||||
Devise.groups[:#{group_name}].each do |mapping|
|
||||
records << warden.authenticate(scope: mapping)
|
||||
def current_#{group_name.to_s.pluralize}
|
||||
records = []
|
||||
#{mappings}.each do |mapping|
|
||||
records << warden.authenticate(scope: mapping)
|
||||
end
|
||||
records.compact
|
||||
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
|
||||
|
||||
|
|
|
@ -4,9 +4,6 @@ module Devise
|
|||
def self.define_helpers(mapping)
|
||||
end
|
||||
|
||||
def self.define_group_helpers(group_name)
|
||||
end
|
||||
|
||||
def omniauth_authorize_path(resource_or_scope, *args)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
_devise_route_context.send("#{scope}_omniauth_authorize_path", *args)
|
||||
|
|
|
@ -351,19 +351,6 @@ module ActionDispatch::Routing
|
|||
end
|
||||
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
|
||||
|
||||
def devise_session(mapping, controllers) #:nodoc:
|
||||
|
|
|
@ -26,7 +26,7 @@ class ControllerAuthenticatableTest < ActionController::TestCase
|
|||
end
|
||||
|
||||
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)
|
||||
end
|
||||
@controller.commenter_signed_in?
|
||||
|
@ -43,14 +43,14 @@ class ControllerAuthenticatableTest < ActionController::TestCase
|
|||
end
|
||||
|
||||
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)
|
||||
end
|
||||
@controller.current_commenter
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
@controller.current_commenters
|
||||
|
@ -77,7 +77,7 @@ class ControllerAuthenticatableTest < ActionController::TestCase
|
|||
end
|
||||
|
||||
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).returns(false)
|
||||
end
|
||||
|
|
|
@ -6,4 +6,6 @@ class ApplicationController < ActionController::Base
|
|||
before_filter :current_user, unless: :devise_controller?
|
||||
before_filter :authenticate_user!, if: :devise_controller?
|
||||
respond_to *Mime::SET.map(&:to_sym)
|
||||
|
||||
devise_group :commenter, contains: [:user, :admin]
|
||||
end
|
||||
|
|
|
@ -104,7 +104,5 @@ Rails.application.routes.draw do
|
|||
get "/unauthenticated", to: "home#unauthenticated"
|
||||
get "/custom_strategy/new"
|
||||
|
||||
devise_group :commenters, includes: [:admins, :users]
|
||||
|
||||
root to: "home#index", via: [:get, :post]
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue