master

laravel/framework

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

HandlesAuthorization.php

TLDR

This file contains a trait called HandlesAuthorization. It provides several methods for handling authorization in Laravel applications.

Methods

allow($message = null, $code = null)

This method creates a new access response. It accepts an optional message and code, and returns an instance of the Illuminate\Auth\Access\Response class.

deny($message = null, $code = null)

This method throws an unauthorized exception. It accepts an optional message and code, and returns an instance of the Illuminate\Auth\Access\Response class.

denyWithStatus($status, $message = null, $code = null)

This method denies access with a specified HTTP status code. It accepts a status code, an optional message, and an optional code. It returns an instance of the Illuminate\Auth\Access\Response class.

denyAsNotFound($message = null, $code = null)

This method denies access with a HTTP 404 status code. It accepts an optional message and code, and returns an instance of the Illuminate\Auth\Access\Response class.

<?php

namespace Illuminate\Auth\Access;

trait HandlesAuthorization
{
    /**
     * Create a new access response.
     *
     * @param  string|null  $message
     * @param  mixed  $code
     * @return \Illuminate\Auth\Access\Response
     */
    protected function allow($message = null, $code = null)
    {
        return Response::allow($message, $code);
    }

    /**
     * Throws an unauthorized exception.
     *
     * @param  string|null  $message
     * @param  mixed|null  $code
     * @return \Illuminate\Auth\Access\Response
     */
    protected function deny($message = null, $code = null)
    {
        return Response::deny($message, $code);
    }

    /**
     * Deny with a HTTP status code.
     *
     * @param  int  $status
     * @param  string|null  $message
     * @param  int|null  $code
     * @return \Illuminate\Auth\Access\Response
     */
    public function denyWithStatus($status, $message = null, $code = null)
    {
        return Response::denyWithStatus($status, $message, $code);
    }

    /**
     * Deny with a 404 HTTP status code.
     *
     * @param  string|null  $message
     * @param  int|null  $code
     * @return \Illuminate\Auth\Access\Response
     */
    public function denyAsNotFound($message = null, $code = null)
    {
        return Response::denyWithStatus(404, $message, $code);
    }
}