Module: Obscured::Doorman::Helpers
- Defined in:
- lib/obscured-doorman/helpers.rb
Instance Method Summary collapse
-
#authenticate(*args) ⇒ Object
(also: #login)
Authenticate a user against defined strategies.
-
#authenticated?(scope = nil) ⇒ Boolean
(also: #logged_in?)
Check the current session is authenticated to a given scope.
-
#authorize!(failure_path = nil) ⇒ Object
Require authorization for an action.
-
#authorized?(format = :json) ⇒ Boolean
Require authorization for example ajax calls, returns 403 is not authenticated.
-
#logout(scopes = nil) ⇒ Object
Terminate the current session.
-
#session_info(scope = nil) ⇒ Object
Return session info.
-
#user(scope = nil) ⇒ Object
(also: #current_user)
Access the user from the current session.
-
#warden ⇒ Object
The main accessor to the warden middleware.
Instance Method Details
#authenticate(*args) ⇒ Object Also known as: login
Authenticate a user against defined strategies
18 19 20 |
# File 'lib/obscured-doorman/helpers.rb', line 18 def authenticate(*args) warden.authenticate!(*args) end |
#authenticated?(scope = nil) ⇒ Boolean Also known as: logged_in?
Check the current session is authenticated to a given scope
12 13 14 |
# File 'lib/obscured-doorman/helpers.rb', line 12 def authenticated?(scope = nil) scope ? warden.authenticated?(scope: scope) : warden.authenticated? end |
#authorize!(failure_path = nil) ⇒ Object
Require authorization for an action
48 49 50 51 52 53 |
# File 'lib/obscured-doorman/helpers.rb', line 48 def (failure_path = nil) unless authenticated? session[:return_to] = request.path if Doorman.configuration.use_referrer redirect(failure_path || Doorman.configuration.paths[:login]) end end |
#authorized?(format = :json) ⇒ Boolean
Require authorization for example ajax calls, returns 403 is not authenticated
58 59 60 61 62 63 |
# File 'lib/obscured-doorman/helpers.rb', line 58 def (format = :json) unless authenticated? halt 403, { 'Content-Type' => 'application/json' }, { message: 'Unauthorized' }.to_json if format == :json halt 403 end end |
#logout(scopes = nil) ⇒ Object
Terminate the current session
33 34 35 |
# File 'lib/obscured-doorman/helpers.rb', line 33 def logout(scopes = nil) scopes ? warden.logout(scopes) : warden.logout(warden.config.default_scope) end |
#session_info(scope = nil) ⇒ Object
Return session info
26 27 28 |
# File 'lib/obscured-doorman/helpers.rb', line 26 def session_info(scope = nil) scope ? warden.session(scope) : scope end |
#user(scope = nil) ⇒ Object Also known as: current_user
Access the user from the current session
40 41 42 |
# File 'lib/obscured-doorman/helpers.rb', line 40 def user(scope = nil) scope ? warden.user(scope) : warden.user end |
#warden ⇒ Object
The main accessor to the warden middleware
7 8 9 |
# File 'lib/obscured-doorman/helpers.rb', line 7 def warden request.env['warden'] end |