Sha256: c1954624f13dcb046a2f7eff9339d1408ff8a148b7542d63ffa8a5e75df74fa7

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 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)
  end

# it the user is not authenticated returns a 422 and an informative error with the link for sign
  def authenticate_user!
    Rails.logger.info "env['warden'].authenticated?(:user): #{env['warden'].authenticated?(: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

6 entries across 6 versions & 1 rubygems

Version Path
cadenero-0.0.2.b4 app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.2.b2 app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.2.b1 app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.2.a3 app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.2.a2 app/extenders/controllers/application_controller_decorator.rb
cadenero-0.0.2.a1 app/extenders/controllers/application_controller_decorator.rb