module Softwear module Library module ControllerAuthentication extend ActiveSupport::Concern class NotSignedInError < StandardError end included do rescue_from NotSignedInError, with: :user_not_signed_in rescue_from Softwear::Auth::Model::AuthServerDown, with: :auth_server_down helper_method :current_user helper_method :user_signed_in? helper_method :destroy_user_session_path helper_method :users_path helper_method :user_path helper_method :edit_user_path end def user_class if Softwear::Auth::Model.descendants.size > 1 raise "More than one descendent of Softwear::Auth::Model is not supported." elsif Softwear::Auth::Model.descendants.size == 0 # Assume there is a "User" model begin User.inspect return User if User.ancestors.include?(Softwear::Auth::Model) rescue NameError => _ end raise "Please define a user model that extends Softwear::Auth::Model." end Softwear::Auth::Model.descendants.first end # ==================== # Action called when a NotSignedInError is raised. # ==================== def user_not_signed_in if Softwear::Auth::Model::STUBBED self.user_token = "dummy-token" authenticate_user! else redirect_to softwear_hub_url + "/users/sign_in?#{{return_to: request.original_url}.to_param}" end end # ==================== # Action called when a NotSignedInError is raised. # ==================== def auth_server_down(error) respond_to do |format| format.html do render inline: \ "