Sha256: 446b9ce8b88f3933b1cedc47ac240206c71e033f7431e23f498f0cba5958953b

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 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_by_id(user_id)
      end
    end
  end

# Check to see if there is an authenticated user
  def user_signed_in?
    env['warden'].authenticated?(:user) unless env['warden'].nil?
  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 = %Q{Please sign in. posting the user json credentials as: {"user": {"email": "testy2@example.com", "password": "changeme"}} to /v1/sessions}
      render json: {errors: @errors, links: "/v1/sessions"}, 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.b6 app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.2.b5 app/extenders/controllers/application_controller_decorator.rb