PHP code example of somnambulist / policy-bundle

1. Go to this page and download the library: Download somnambulist/policy-bundle 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/ */

    

somnambulist / policy-bundle example snippets


use Somnambulist\Bundles\PolicyBundle\Policies\AbstractPolicy;
use Symfony\Component\Security\Core\User\UserInterface;

class DocumentPolicy extends AbstractPolicy
{
    public const string MANAGE   = 'documents.manage';
    public const string DESTROY  = 'documents.destroy';

    public function supports(): array
    {
        return ['document', 'documents',];
    }

    public function dependencies(): array
    {
        return [];
    }

    public function manage(UserInterface $user, mixed $subject): bool
    {
        return $user->can(self::MANAGE);
    }

    public function destroy(UserInterface $user, mixed $subject): bool
    {
        return $user->can(self::DESTROY);
    }
}

use Somnambulist\Bundles\PolicyBundle\Policies\AbstractPolicy;
use Symfony\Component\Security\Core\User\UserInterface;

class FilePolicy extends AbstractPolicy
{
    public const string MANAGE   = 'documents.files.manage';
    public const string DESTROY  = 'documents.documents.destroy';

    public function supports(): array
    {
        return ['file', 'files',];
    }

    public function dependencies(): array
    {
        return [
            '*' => DocumentPolicy::MANAGE,

            self::DESTROY => self::MANAGE,
        ];
    }

    public function manage(UserInterface $user, $subject): bool
    {
        return $user->can(self::MANAGE);
    }

    public function destroy(UserInterface $user, $subject): bool
    {
        return $user->can(self::DESTROY);
    }
}