master

laravel/framework

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

Authorizable.php

TLDR

The provided file Authorizable.php is a trait that adds authorization capabilities to an entity. It contains several methods for checking if the entity has certain abilities.

Methods

can($abilities, $arguments = [])

This method determines if the entity has the given abilities. It takes in an iterable or string of abilities and an array or mixed type of arguments (optional). The method uses the Gate class to check if the entity is authorized for the specified abilities with the provided arguments. It returns true if the entity has the abilities, otherwise false.

canAny($abilities, $arguments = [])

This method determines if the entity has any of the given abilities. It takes in the same parameters as the can() method. It uses the Gate class to check if the entity is authorized for any of the specified abilities with the provided arguments. It returns true if the entity has at least one of the abilities, otherwise false.

cant($abilities, $arguments = [])

This method determines if the entity does not have the given abilities. It takes in the same parameters as the can() method. It calls the can() method and returns the negation of its result. It returns true if the entity does not have the abilities, otherwise false.

cannot($abilities, $arguments = [])

This method is an alias for the cant() method. It takes in the same parameters and behaves the same way.

END

<?php

namespace Illuminate\Foundation\Auth\Access;

use Illuminate\Contracts\Auth\Access\Gate;

trait Authorizable
{
    /**
     * Determine if the entity has the given abilities.
     *
     * @param  iterable|string  $abilities
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function can($abilities, $arguments = [])
    {
        return app(Gate::class)->forUser($this)->check($abilities, $arguments);
    }

    /**
     * Determine if the entity has any of the given abilities.
     *
     * @param  iterable|string  $abilities
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function canAny($abilities, $arguments = [])
    {
        return app(Gate::class)->forUser($this)->any($abilities, $arguments);
    }

    /**
     * Determine if the entity does not have the given abilities.
     *
     * @param  iterable|string  $abilities
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function cant($abilities, $arguments = [])
    {
        return ! $this->can($abilities, $arguments);
    }

    /**
     * Determine if the entity does not have the given abilities.
     *
     * @param  iterable|string  $abilities
     * @param  array|mixed  $arguments
     * @return bool
     */
    public function cannot($abilities, $arguments = [])
    {
        return $this->cant($abilities, $arguments);
    }
}