PHP code example of dbeurive / rbac

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

    

dbeurive / rbac example snippets


use dbeurive\Rbac\RolesHierarchy;

// Specify the hierarchy of roles using the builder

$hierarchy = new RolesHierarchy("super-admin");
$hierarchy
    ->addSubRole("admin")
        ->addSubRole("admin-bouygues")
            ->addSubRole("user-bouygues")
            ->up()
        ->up()
        ->addSubRole("admin-orange")
            ->addSubRole("user-orange")
            ->up()
        ->up()
    ->up()
    ->addSubRole("other-admin");

// Test a given role.

if ($hierarchy->canAccessResource("super-admin", "admin")) {
    // "super-user" can access resources managed by "admin".
}

$hierarchy = array(
     'role'   => 'super-admin',
     'access' => array(
         array(
             'role'   => 'admin',
             'access' =>  array(
                 array(
                     'role'   => 'admin-bouygues',
                     'access' => array(
                         array(
                             'role'   => 'user-bouygues',
                             'access' => array()
                         )
                     )
                 )
             )
         ),
         array(
             'role'   => 'admin-orange',
             'access' => array(
                 array(
                     'role'   => 'user-orange',
                     'access' => array()
                 )
             )
         ),
         array(
             'role'   => 'other-admin',
             'access' => array()
         )
     )
);

$this->__hierarchyArray = new RolesHierarchy($hierarchy);