1. Go to this page and download the library: Download jonston/symfony-permission 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/ */
jonston / symfony-permission example snippets
use Doctrine\ORM\Mapping as ORM;
use Jonston\SymfonyPermission\Trait\HasRoles;
use Jonston\SymfonyPermission\Contract\HasRolesInterface;
use Jonston\SymfonyPermission\Entity\Role;
use Jonston\SymfonyPermission\Entity\Permission;
#[ORM\Entity]
class User implements HasRolesInterface
{
use HasRoles;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\ManyToMany(targetEntity: Role::class)]
#[ORM\JoinTable(name: 'user_role')]
protected Collection $roles;
#[ORM\ManyToMany(targetEntity: Permission::class)]
#[ORM\JoinTable(name: 'user_permission')]
protected Collection $permissions;
public function __construct()
{
$this->roles = new \Doctrine\Common\Collections\ArrayCollection();
$this->permissions = new \Doctrine\Common\Collections\ArrayCollection();
}
// ... your logic ...
}
#[ORM\Entity]
class Role implements HasPermissionsInterface
{
use HasPermissions;
// ...existing code...
#[ORM\ManyToMany(targetEntity: Permission::class, inversedBy: 'roles')]
#[ORM\JoinTable(name: 'role_permission')]
protected Collection $permissions;
// ...existing code...
}
$accessControlService->hasPermission($user, 'edit articles'); // true if user or any role has permission
$accessControlService->hasAnyPermission($user, [$permission1, $permission2]);
$accessControlService->hasAllPermissions($user, [$permission1, $permission2]);