PHP code example of krubio / perfect-rbac

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

    

krubio / perfect-rbac example snippets


use PerfectApp\RBAC\AuthorizationService;
use PerfectApp\RBAC\PermissionsRepository;
use PerfectApp\RBAC\UserRolesRepository;

$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

$permissionsRepository = new PermissionsRepository($pdo);
$userRolesRepository = new UserRolesRepository($pdo);

$authorizationService = new AuthorizationService($userRolesRepository);

/**
 * @param int $userId
 * @param array<mixed> $allowedRoles
 * @return bool
 */
public function isUserRoleAuthorized(int $userId, array $allowedRoles): bool

$userId = 1;
$allowedRoles = ['admin', 'editor'];

if ($authorizationService->isUserRoleAuthorized($userId, $allowedRoles)) {
    echo "User is authorized.";
} else {
    echo "User is not authorized.";
}

/**
 * @param int $userId
 * @param string $eHasPermission(int $userId, string $

$userId = 1;
$ionsRepository->userRoleHasPermission($userId, $ho "User does not have the