Class: Authenticate::Lifecycle
- Inherits:
-
Object
- Object
- Authenticate::Lifecycle
- Includes:
- Debug
- Defined in:
- lib/authenticate/lifecycle.rb
Overview
Lifecycle stores and runs callbacks for authorization events.
Heavily borrowed from warden (github.com/hassox/warden).
Events:
:set_user - called after the user object is loaded, either through id/password or via session token.
:authentication - called after the user authenticates with id & password
Callbacks are added via after_set_user or after_authentication.
Callbacks can throw(:failure,message) to signal an authentication/authorization failure, or perform actions on the user or session.
Options
The callback options may optionally specify when to run the callback:
only - executes the callback only if it matches the event(s) given
except - executes the callback except if it matches the event(s) given
The callback may also specify a ‘name’ key in options. This is for debugging purposes only.
Callback block parameters
Callbacks are invoked with the following block parameters: |user, session, opts|
user - the user object just loaded
session - the Authenticate::Session
opts - any you want passed into the callback
Example
# A callback to track the users successful logins:
Authenticate.lifecycle.after_set_user do |user, session, opts|
user.sign_in_count += 1
end
Constant Summary collapse
- @@conditions =
[:only, :except, :event]
Instance Method Summary collapse
-
#after_authentication(options = {}, method = :push, &block) ⇒ Object
A callback to run after the user successfully authenticates, during the login process.
-
#after_set_user(options = {}, method = :push, &block) ⇒ Object
This callback is triggered after the first time a user is set during per-hit authorization, or during login.
- #prepend_after_authentication(options = {}, &block) ⇒ Object
-
#run_callbacks(kind, *args) ⇒ Object
Run callbacks of the given kind.
Methods included from Debug
Instance Method Details
#after_authentication(options = {}, method = :push, &block) ⇒ Object
A callback to run after the user successfully authenticates, during the login process. Mechanically identical to [#after_set_user].
51 52 53 |
# File 'lib/authenticate/lifecycle.rb', line 51 def after_authentication( = {}, method = :push, &block) add_callback(after_authentication_callbacks, , method, &block) end |
#after_set_user(options = {}, method = :push, &block) ⇒ Object
This callback is triggered after the first time a user is set during per-hit authorization, or during login.
43 44 45 |
# File 'lib/authenticate/lifecycle.rb', line 43 def after_set_user( = {}, method = :push, &block) add_callback(after_set_user_callbacks, , method, &block) end |
#prepend_after_authentication(options = {}, &block) ⇒ Object
88 89 90 |
# File 'lib/authenticate/lifecycle.rb', line 88 def prepend_after_authentication( = {}, &block) after_authentication(, :unshift, &block) end |
#run_callbacks(kind, *args) ⇒ Object
Run callbacks of the given kind.
-
kind - :authenticate or :after_set_user
-
args - user, session, opts hash. Opts is an optional event, e.g. { event: :authentication }
Example:
Authenticate.lifecycle.run_callbacks(:after_set_user, @current_user, self, { event: :authentication })
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/authenticate/lifecycle.rb', line 64 def run_callbacks(kind, *args) # args - |user, session, opts| # Last callback arg MUST be a Hash = args.last debug "START Lifecycle.run_callbacks kind:#{kind} options:#{options.inspect}" # each callback has 'conditions' stored with it send("#{kind}_callbacks").each do |callback, conditions| conditions = conditions.dup # make a copy, we mutate it debug "Lifecycle.running callback -- #{conditions.inspect}" conditions.delete_if {|key, _val| !@@conditions.include? key} # debug "conditions after filter:#{conditions.inspect}" invalid = conditions.find do |key, value| # debug "!!!!!!! conditions key:#{key} value:#{value} options[key]:#{options[key].inspect}" # debug("!value.include?(options[key]):#{!value.include?(options[key])}") if value.is_a?(Array) value.is_a?(Array) ? !value.include?([key]) : (value != [key]) end debug "Lifecycle.callback invalid? #{invalid.inspect}" callback.call(*args) unless invalid end debug "FINISHED Lifecycle.run_callbacks #{kind}" nil end |