Module: Devise::Controllers::Filters

Defined in:
lib/devise/controllers/filters.rb

Overview

Those filters are convenience methods added to ApplicationController to deal with Warden.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/devise/controllers/filters.rb', line 7

def self.included(base)
  base.class_eval do
    helper_method :warden, :signed_in?, :devise_controller?,
                  *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten

    # Use devise default_url_options. We have to declare it here to overwrite
    # default definitions.
    def default_url_options(options=nil)
      Devise::Mapping.default_url_options
    end
  end
end

Instance Method Details

#authenticate(scope) ⇒ Object

Attempts to authenticate the given scope by running authentication hooks, but does not redirect in case of failures.



36
37
38
# File 'lib/devise/controllers/filters.rb', line 36

def authenticate(scope)
  warden.authenticate(:scope => scope)
end

#authenticate!(scope) ⇒ Object

Attempts to authenticate the given scope by running authentication hooks, redirecting in case of failures.



42
43
44
# File 'lib/devise/controllers/filters.rb', line 42

def authenticate!(scope)
  warden.authenticate!(:scope => scope)
end

#devise_controller?Boolean

Return true if it’s a devise_controller. false to all controllers unless the controllers defined inside devise. Useful if you want to apply a before filter to all controller, except the ones in devise:

before_filter :my_filter, :unless => { |c| c.devise_controller? }

Returns:

  • (Boolean)


30
31
32
# File 'lib/devise/controllers/filters.rb', line 30

def devise_controller?
  false
end

#sign_in(scope, resource) ⇒ Object

Set the warden user with the scope, signing in the resource automatically, without running hooks.



54
55
56
# File 'lib/devise/controllers/filters.rb', line 54

def (scope, resource)
  warden.set_user(resource, :scope => scope)
end

#sign_out(scope, *args) ⇒ Object

Sign out based on scope.



59
60
61
62
63
# File 'lib/devise/controllers/filters.rb', line 59

def sign_out(scope, *args)
  warden.user(scope) # Without loading user here, before_logout hook is not called
  warden.raw_session.inspect # Without this inspect here. The session does not clear.
  warden.logout(scope, *args)
end

#signed_in?(scope) ⇒ Boolean

Check if the given scope is signed in session, without running authentication hooks.

Returns:

  • (Boolean)


48
49
50
# File 'lib/devise/controllers/filters.rb', line 48

def signed_in?(scope)
  warden.authenticated?(scope)
end

#wardenObject

The main accessor for the warden proxy instance



21
22
23
# File 'lib/devise/controllers/filters.rb', line 21

def warden
  request.env['warden']
end