PHP code example of gilsonsouza / lara-polices

1. Go to this page and download the library: Download gilsonsouza/lara-polices library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gilsonsouza / lara-polices example snippets


 protected $middleware = [
        // other middleware ommited
    	\LaraPolices\Middlewares\PolicesMiddleware::class,
 ];

// file START ommited
    'providers' => [
        // other pro


namespace LaraPolicesTests;

use LaraPolices\Polices\AbstractPolice;

class MockPolice extends AbstractPolice
{
    public function mockTrueMethod($request)
    {
        return (bool) ($request->owner_id == $this->user->group['owner_id'] &&
            $request->owner_type == $this->user->group['owner_type']
        );
    }


namespace LaraPolices\Polices;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use LaraPolices\Exceptions\ObjectNotFoundException;

abstract class AbstractPolice
{
    /**
     * Define the Autenticatable Interface
     * @var Authenticatable
     */
    protected $user;

    /**
     * @var array Objects storage
     */
    private $objects = array();

    /**
     * AbstractPolice constructor.
     * @param Request $request
     * @param Authenticatable $user
     */
    public function __construct(Authenticatable $user)
    {
        $this->user = $user;
    }

    /**
     * Store object in police
     *
     * @param mixed $object
     * @return $this
     */
    public function pushObject($object)
    {
        $objectReflection = new \ReflectionClass($object);
        $this->objects[$objectReflection->getShortName()] = $object;

        return $this;
    }

    /**
     * Get object from police
     *
     * @param $name
     * @return mixed
     * @throws ObjectNotFoundException
     */
    public function getObject($name)
    {
        if (!isset($this->objects[$name])) {
            if (class_exists($name)) {
                $this->pushObject(App::make($name));

                return $this->getObject($name);
            }

            throw new ObjectNotFoundException("Object not found.");
        }

        return $this->objects[$name];
    }

    /**
     * Function to call action method to authorize resource permission to user.
     * This function should be return a boolean.
     * @param Request $request Request to validate
     * @param string $actionToValidate Police action to validate
     * @return bool
     */
    public function canMakeAction(Request $request, $actionToValidate)
    {
        return (bool) $this->$actionToValidate($request);
    }