master

laravel/framework

Last updated at: 29/12/2023 09:20

AuthorizationException.php

TLDR

The provided file, AuthorizationException.php, is a PHP file that defines the AuthorizationException class. This class extends the Exception class and is used to handle authorization exceptions in the Laravel framework. It includes methods for getting and setting the response from the gate, setting the HTTP response status code, determining if the status code has been set, and creating a deny response object from the exception.

Methods

__construct($message = null, $code = null, Throwable $previous = null)

This method is the constructor of the AuthorizationException class. It initializes the exception with the provided $message, $code, and $previous values. If the $message parameter is null, the default message "This action is unauthorized." is used. The constructor also sets the $code value and calls the parent constructor with the provided parameters.

response()

This method returns the response from the gate.

setResponse($response)

This method sets the response from the gate to the provided $response value. It returns the instance of AuthorizationException to allow method chaining.

withStatus($status)

This method sets the HTTP response status code to the provided $status value. It returns the instance of AuthorizationException to allow method chaining.

asNotFound()

This method sets the HTTP response status code to 404. It returns the instance of AuthorizationException to allow method chaining.

hasStatus()

This method checks if the HTTP status code has been set. It returns a boolean value indicating whether the status code has been set.

status()

This method returns the HTTP status code.

toResponse()

This method creates a deny response object from the exception. It returns an instance of \Illuminate\Auth\Access\Response with the exception message and code, and sets the HTTP status code accordingly.

<?php

namespace Illuminate\Auth\Access;

use Exception;
use Throwable;

class AuthorizationException extends Exception
{
    /**
     * The response from the gate.
     *
     * @var \Illuminate\Auth\Access\Response
     */
    protected $response;

    /**
     * The HTTP response status code.
     *
     * @var int|null
     */
    protected $status;

    /**
     * Create a new authorization exception instance.
     *
     * @param  string|null  $message
     * @param  mixed  $code
     * @param  \Throwable|null  $previous
     * @return void
     */
    public function __construct($message = null, $code = null, Throwable $previous = null)
    {
        parent::__construct($message ?? 'This action is unauthorized.', 0, $previous);

        $this->code = $code ?: 0;
    }

    /**
     * Get the response from the gate.
     *
     * @return \Illuminate\Auth\Access\Response
     */
    public function response()
    {
        return $this->response;
    }

    /**
     * Set the response from the gate.
     *
     * @param  \Illuminate\Auth\Access\Response  $response
     * @return $this
     */
    public function setResponse($response)
    {
        $this->response = $response;

        return $this;
    }

    /**
     * Set the HTTP response status code.
     *
     * @param  int|null  $status
     * @return $this
     */
    public function withStatus($status)
    {
        $this->status = $status;

        return $this;
    }

    /**
     * Set the HTTP response status code to 404.
     *
     * @return $this
     */
    public function asNotFound()
    {
        return $this->withStatus(404);
    }

    /**
     * Determine if the HTTP status code has been set.
     *
     * @return bool
     */
    public function hasStatus()
    {
        return $this->status !== null;
    }

    /**
     * Get the HTTP status code.
     *
     * @return int|null
     */
    public function status()
    {
        return $this->status;
    }

    /**
     * Create a deny response object from this exception.
     *
     * @return \Illuminate\Auth\Access\Response
     */
    public function toResponse()
    {
        return Response::deny($this->message, $this->code)->withStatus($this->status);
    }
}