Module: Obscured::Doorman::Helpers

Defined in:
lib/obscured-doorman/helpers.rb

Instance Method Summary collapse

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

Returns:

  • (Boolean)


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

Parameters:

  • failure_path (String) (defaults to: nil)

    path to redirect to if user is unauthenticated



48
49
50
51
52
53
# File 'lib/obscured-doorman/helpers.rb', line 48

def authorize!(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

Parameters:

  • format (Symbol) (defaults to: :json)

Returns:

  • (Boolean)


58
59
60
61
62
63
# File 'lib/obscured-doorman/helpers.rb', line 58

def authorized?(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

Parameters:

  • scopes (Symbol) (defaults to: nil)

    the session scope to terminate



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

Parameters:

  • scope (Symbol) (defaults to: nil)

    the scope to retrieve session info for



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

Parameters:

  • scope (Symbol) (defaults to: nil)

    for the logged in user



40
41
42
# File 'lib/obscured-doorman/helpers.rb', line 40

def user(scope = nil)
  scope ? warden.user(scope) : warden.user
end

#wardenObject

The main accessor to the warden middleware



7
8
9
# File 'lib/obscured-doorman/helpers.rb', line 7

def warden
  request.env['warden']
end