PHP code example of tomsgu / permission-bundle

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

    

tomsgu / permission-bundle example snippets


return [
    // ...
    Tomsgu\PermissionBundle\TomsguPermissionBundle::class => ['all' => true],
];

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Tomsgu\PermissionBundle\Entity\Permission as BasePermission;

#[ORM\Entity]
#[ORM\Table(name: 'permission')]
class Permission extends BasePermission
{
    #[ORM\Id]
    #[ORM\Column(type: 'integer')]
    #[ORM\GeneratedValue]
    protected int $id;

    public function getId(): int
    {
        return $this->id;
    }
}

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Tomsgu\PermissionBundle\Model\UserPermissionInterface;

#[ORM\Entity]
class User implements UserPermissionInterface
{
    #[ORM\ManyToMany(targetEntity: Permission::class)]
    #[ORM\JoinTable(name: 'users_permissions')]
    protected array $permissions;

    public function getId(): int
    {
        return $this->id;
    }

    public function getPermissions(): array
    {
        return $this->permissions;
    }
}

use Tomsgu\PermissionBundle\Model\UserManagerInterface;

class PostController
{
    public function __construct(private UserManagerInterface $userManager) {}

    public function edit(User $user): void
    {
        if ($this->userManager->hasPermission($user, 'EDIT_POST')) {
            // ...
        }
    }
}