# == 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