Sha256: 9107e1db7c224fc796b228eb75414122e7bf7e09fcb7419a26f3b44f9e1545bb

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module SimpleAuth
  module ActionController
    class RequireLoginAction
      DEFAULT_UNLOGGED_IN_MESSAGE = "You must be logged in to access this page."
      DEFAULT_UNAUTHORIZED_MESSAGE =
        "You don't have permission to access this page."

      attr_reader :controller, :scope

      def initialize(controller, scope)
        @controller = controller
        @scope = scope
      end

      def valid?
        valid_session? && authorized?
      end

      def message
        return if valid?
        return unauthorized_message unless authorized?

        unlogged_message
      end

      private def valid_session?
        controller.send("#{scope}_session").valid?
      end

      private def authorized?
        controller.send("authorized_#{scope}?")
      end

      private def unauthorized_message
        translation_for("#{scope}.unauthorized", DEFAULT_UNAUTHORIZED_MESSAGE)
      end

      private def unlogged_message
        translation_for("#{scope}.unlogged_in", DEFAULT_UNLOGGED_IN_MESSAGE)
      end

      private def translation_for(translation_scope, default_message)
        I18n.t(translation_scope, scope: :simple_auth, default: default_message)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
simple_auth-3.1.4 lib/simple_auth/action_controller/require_login_action.rb
simple_auth-3.1.3 lib/simple_auth/action_controller/require_login_action.rb
simple_auth-3.1.2 lib/simple_auth/action_controller/require_login_action.rb
simple_auth-3.1.1 lib/simple_auth/action_controller/require_login_action.rb
simple_auth-3.1.0 lib/simple_auth/action_controller/require_login_action.rb