PHP code example of tourze / biz-role-bundle

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

    

tourze / biz-role-bundle example snippets


return [
    // ...
    Tourze\BizRoleBundle\BizRoleBundle::class => ['all' => true],
];

use Tourze\BizRoleBundle\Entity\BizRole;

$role = new BizRole();
$role->setName('ROLE_MANAGER');
$role->setTitle('Manager');
$role->setPermissions(['user.view', 'user.edit', 'report.view']);
$role->setHierarchicalRoles(['ROLE_OPERATOR']); // Inherits from ROLE_OPERATOR
$role->setValid(true);

$entityManager->persist($role);
$entityManager->flush();

use Tourze\BizRoleBundle\Entity\RoleEntityPermission;

$permission = new RoleEntityPermission();
$permission->setRole($role);
$permission->setEntityClass('App\Entity\Order');
$permission->setStatement('o.department = :dept'); // WHERE clause
$permission->setRemark('Access to department orders only');
$permission->setValid(true);

$entityManager->persist($permission);
$entityManager->flush();

use Tourze\BizRoleBundle\Controller\Admin\BizRoleCrudController;
use Tourze\BizRoleBundle\Controller\Admin\RoleEntityPermissionCrudController;

public function configureMenuItems(): iterable
{
    yield MenuItem::linkToCrud('Roles', 'fa fa-user-shield', BizRole::class)
        ->setController(BizRoleCrudController::class);
    
    yield MenuItem::linkToCrud('Data Permissions', 'fa fa-lock', RoleEntityPermission::class)
        ->setController(RoleEntityPermissionCrudController::class);
}

// In a controller
$this->denyAccessUnlessGranted('user.edit');

// In Twig
{% if is_granted('user.view') %}
    <!-- Show user data -->
{% endif %}

$managerRole = new BizRole();
$managerRole->setName('ROLE_MANAGER');
$managerRole->setHierarchicalRoles(['ROLE_USER']);
// Manager inherits all USER permissions plus additional ones

$permission = new RoleEntityPermission();
$permission->setEntityClass('App\Entity\Document');
$permission->setStatement('d.owner_id = :current_user_id OR d.is_public = 1');

class DocumentVoter extends Voter
{
    protected function supports(string $attribute, mixed $subject): bool
    {
        return $attribute === 'VIEW_DOCUMENT' && $subject instanceof Document;
    }
    
    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
    {
        // Custom permission logic using role data permissions
    }
}
bash
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate