Sha256: b8f0013a09daeb261813ac0b5095e5e7fbd4e32a734599ca7738ed941f76469e

Contents?: true

Size: 1.35 KB

Versions: 2

Compression:

Stored size: 1.35 KB

Contents

# == ApplicationController Extensions for Authentication
#
# Provide methods for manage authentication of users in a multitenant account environment.
#

::ApplicationController.class_eval do

# Returns the current account for the authenticated user.
#
# @return [Cadenero::V1::Account] the current account.
 def current_account
    if user_signed_in?
      @current_account ||= begin
        Cadenero::V1::Account.find_by_subdomain(request.subdomain)
      end
    end
  end

# Returns the current authenticated user.
#
# @return [Cadenero::User] the current account.
  def current_user
    if user_signed_in?
      @current_user ||= begin
        user_id = env['warden'].user(:scope => :user)
        Cadenero::User.find(user_id)
      end
    end
  end

# Check to see if there is an authenticated user  
  def user_signed_in?
    env['warden'].authenticated?(:user)
  end

# it the user is not authenticated returns a 422 and an informative error with the link for sign
  def authenticate_user!
    unless user_signed_in?
      errors = "Please sign in. posting the user json credentials to /v1/sign_in"
      render json: {errors: errors, links: "/v1/sign_in"}, status: 422
    end
  end

# Authenticate the provided user.
#
# @param user [Cadenero::User] the user to be authenthicated
  def force_authentication!(user)
    env['warden'].set_user(user.id, :scope => :user)
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cadenero-0.0.2.a app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.1 app/extenders/controllers/application_controller_decorator.rb