PHP code example of ggbb / symfony-user-permission

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

    

ggbb / symfony-user-permission example snippets


class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface, UserLoaderInterface
{
    use UserRoleUserLoaderTrait;
    ...
}

class User implements UserInterface, UserRoleFieldInterface
{
    use GetRolesMethodTrait;
    ...
}

namespace App\Entity;

#[ORM\Entity(repositoryClass: UserRoleRepository::class)]
class UserRole implements UserRoleInterface
{
    use RoleFieldTrait;
    use RolePermissionFieldTrait;
    ...
}

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class YourController extends AbstractController
{
    public function yourAction(): Response
    {
        if ($this->isGranted('ROLE_ADMIN')) {
            // ...
        }

        $object = ...;
        if ($this->isGranted('EDIT', $object)) {
            // ...
        }

        return new Response('...');
    }
}

#[Patch(
    security: "is_granted('PostPermission::EDIT') or is_granted('PostPermission::MY_EDIT', object.getAddedByUser())",
)]
class Post
{
    // ...
}



namespace App\Permission;

use Ggbb\SymfonyUserPermissionBundle\Permission\AbstractPermission;

class PostPermission extends AbstractPermission
{
    public const VIEW = 'PostPermission::VIEW';
    public const ADD = 'PostPermission::ADD';
    public const EDIT = 'PostPermission::EDIT';
    public const MY_EDIT = 'PostPermission::MY_EDIT';
    public const DELETE = 'PostPermission::DELETE';


    public function getPermissions(): array
    {
        return [
            self::VIEW => [
                'title' => 'Просмотр всех объектов',
            ],
            self::ADD => [
                'title' => 'Добавить объект',
            ],
            self::EDIT => [
                'title' => 'Отредактировать все объекты',
            ],
            self::DELETE => [
                'title' => 'Удалить все объекты',
            ],
        ];
    }

    public function getName(): string
    {
        return 'Объекты';
    }
}