Sha256: f2a31e49613444f0f7b41b4aecc8de538502da0fc031fd060390bf24997d6686

Contents?: true

Size: 1.89 KB

Versions: 13

Compression:

Stored size: 1.89 KB

Contents

module Kadmin
  module Concerns
    module AuthorizedUser
      extend ActiveSupport::Concern

      included do
        if respond_to?(:helper_method)
          helper_method :current_user
          helper_method :authorized_user
          helper_method :logged_in?
          helper_method :authorized?
        end
      end

      # @!group before_action

      # Add as a before_action whenever you wish to authorize a user for a particular
      # resource. The app provided user model will perform authorization of the resource.
      # @see Kadmin::Auth::User
      # @example
      #   before_action :authorize, except: [:index] # exclude index from authorization
      def authorize
        if Kadmin::Auth.config.enabled?
          if logged_in?
            unless authorized?
              redirect_to Kadmin::Engine.routes.url_helpers.auth_unauthorized_path
            end
          else
            redirect_to Kadmin::Engine.routes.url_helpers.auth_login_path(origin: request.fullpath)
          end
        end
      end

      # @!endgroup

      # @!group View Helpers

      # @return [String] the current user identifier. Historically called current_user
      def current_user
        Kadmin::Auth.test_user || session[Kadmin::AuthController::SESSION_KEY]
      end

      # @see Kadmin::Concerns::AuthorizedUser#current_user
      # @return [Kadmin::Auth::User] instance of the user identified by current_user
      def authorized_user
        return Kadmin::Auth.users.get(current_user)
      end

      # @!endgroup

      # @!group Helpers

      # @return [Boolean] true if the user is logged in, false otherwise
      def logged_in?
        return current_user.present?
      end

      # @see Kadmin::Auth::User
      # @return [Boolean] true if the user is authorized in, false otherwise
      def authorized?
        return authorized_user&.authorized?(request)
      end

      # @!endgroup
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
kadmin-1.3.0 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.2.1 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.2.0 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.1.1 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.1.0 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.9 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.8 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.7 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.6 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.5 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.4 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.3 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-1.0.2 app/controllers/kadmin/concerns/authorized_user.rb