Sha256: aa3dc9708224ee7c0ba08808c9f36cd8c54ead0b588e55eeadc816208b2cd4a1

Contents?: true

Size: 1.86 KB

Versions: 36

Compression:

Stored size: 1.86 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.path)
          end
        end
      end

      # @!endgroup

      # @!group View Helpers

      # @return [String] the current user identifier. Historically called current_user
      def current_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

36 entries across 36 versions & 1 rubygems

Version Path
kadmin-0.7.3 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.7.2 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.7.1 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.7.0 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.21 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.19 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.18 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.17 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.16 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.13 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.12 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.11 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.10 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.9 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.8 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.7 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.6 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.5 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.4 app/controllers/kadmin/concerns/authorized_user.rb
kadmin-0.6.3 app/controllers/kadmin/concerns/authorized_user.rb