Sha256: 4809216dcd6301d413de8c97524ae71a8c4194c93f7fe8cc783f06580d4ca945

Contents?: true

Size: 1.43 KB

Versions: 15

Compression:

Stored size: 1.43 KB

Contents

module Controller
  module CurrentUserConcern
    extend ActiveSupport::Concern

    included do
      helper_method :current_user_session, :current_user, :user_signed_in?
    end

    private

    def after_sign_out_url
      "/#{I18n.locale}"
    end

    def authenticate_user!
      unless user_signed_in?
        store_location
        handle_authentication_failure
        return false
      end
      true
    end

    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = ::Ecm::UserArea::UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end

    def user_authentication_failed_path
      ecm_user_area.new_user_session_path
    end

    def user_signed_in?
      !current_user.nil?
    end

    def sign_out_user
      @current_user = nil
      redirect_to after_sign_out_url
    end

    def current_session(type)
      type = type.to_s
      type << '_session' unless type.end_with?('_session')
      send("current_#{type}".to_sym)
    end

    def handle_authentication_failure
      respond_to do |format|
        format.json { render json: { error: :forbidden }, status: :forbidden }
        format.html { redirect_to(user_authentication_failed_path, notice: t('messages.failures.ecm_user_area.authentication_failed')) }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ecm_user_area2-4.0.2 app/concerns/controller/current_user_concern.rb
ecm_user_area2-4.0.1 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.1.2 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.1.1 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.1.0 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.0.3 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.0.2 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.0.1 app/concerns/controller/current_user_concern.rb
ecm_user_area2-3.0.0 app/concerns/controller/current_user_concern.rb
ecm_user_area2-2.3.0 app/concerns/controller/current_user_concern.rb
ecm_user_area2-2.2.0 app/concerns/controller/current_user_concern.rb
ecm_user_area2-2.0.0 app/concerns/controller/current_user_concern.rb
ecm_user_area2-1.2.10 app/concerns/controller/current_user_concern.rb
ecm_user_area2-1.2.9 app/concerns/controller/current_user_concern.rb
ecm_user_area2-1.2.8 app/concerns/controller/current_user_concern.rb