Sha256: a56f9104a516773be0da5212bd581ff53858cb5bcfd2a19bb3dc060faf53d8c3

Contents?: true

Size: 1.61 KB

Versions: 61

Compression:

Stored size: 1.61 KB

Contents

module SinatraMore
  module WardenHelpers
    # Returns the current authenticated user
    def current_user
      warden_handler.user
    end

    # Login the user through the specified warden strategy
    def authenticate_user!
      warden_handler.authenticate!
    end

    # Signs out the user
    def logout_user!
      warden_handler.logout
    end

    # Returns true if the user has authenticated
    def logged_in?
      warden_handler.authenticated?
    end

    # If a block is given, only yields the content if the user is authenticated
    # If no block is given, returns true if the user is logged in
    def authenticated?(&block)
      if block_given?
        return '' unless logged_in?
        authenticated_content = capture_html(&block)
        concat_content(authenticated_content)
      else
        return logged_in?
      end
    end
    
    # If a block is given, only yields the content if the user is unregistered
    # If no block is given, returns true if the user is not logged in
    def unregistered?(&block)
      if block_given?
        return '' if logged_in?
        unregistered_content = capture_html(&block)
        concat_content(unregistered_content)
      else
        return !logged_in?
      end      
    end

    # Forces a user to return to a fail path unless they are authorized
    # Used to require a user be authenticated before routing to an action
    def must_be_authorized!(failure_path=nil)
      redirect(failure_path ? failure_path : '/') unless authenticated?
    end

    # Returns the raw warden authentication handler
    def warden_handler
      request.env['warden']
    end
  end
end

Version data entries

61 entries across 61 versions & 1 rubygems

Version Path
sinatra_more-0.3.43 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.42 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.41 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.40 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.39 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.38 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.37 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.36 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.35 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.34 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.33 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.32 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.31 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.30 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.29 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.28 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.27 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.26 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.25 lib/sinatra_more/warden_plugin/warden_helpers.rb
sinatra_more-0.3.24 lib/sinatra_more/warden_plugin/warden_helpers.rb