PHP code example of tourze / rbac-core

1. Go to this page and download the library: Download tourze/rbac-core 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 / rbac-core example snippets




use Tourze\RBAC\Core\Level0\User;
use Tourze\RBAC\Core\Level0\Role;
use Tourze\RBAC\Core\Level0\Permission;

// Implement the interfaces in your application
class MyUser implements User
{
    public function getRoles(): array
    {
        // Return user's roles
    }
    
    public function getSessions(): array
    {
        // Return user's sessions
    }
}

class MyRole implements Role
{
    public function getName(): string
    {
        return 'admin';
    }
    
    public function getTitle(): string
    {
        return 'Administrator';
    }
}

class MyPermission implements Permission
{
    public function getName(): string
    {
        return 'user.create';
    }
    
    public function getTitle(): string
    {
        return 'Create User';
    }
    
    public function getParentPermission(): ?Permission
    {
        return null;
    }
}



use Tourze\RBAC\Core\Level1\HierarchicalRole;

class MyHierarchicalRole implements HierarchicalRole
{
    public function getName(): string
    {
        return 'manager';
    }
    
    public function getTitle(): string
    {
        return 'Manager';
    }
    
    public function getParentRoles(): array
    {
        // Return parent roles for inheritance
        return [];
    }
}



use Tourze\RBAC\Core\Level2\MutexRole;
use Tourze\RBAC\Core\Level2\PreconditionRole;

// Mutually exclusive roles
class MyMutexRole implements MutexRole
{
    public function getMutexRoles(): array
    {
        // Return roles that cannot be assigned together
        return [];
    }
}

// Prerequisite roles
class MyPreconditionRole implements PreconditionRole
{
    public function getPreconditionRole(): ?Role
    {
        // Return role that must be assigned first
        return null;
    }
}



use Tourze\RBAC\Core\Rule\Rule;
use Tourze\RBAC\Core\Rule\RuleRole;

class MyRule implements Rule
{
    public function getName(): string
    {
        return 'business_hour_rule';
    }
    
    public function getTitle(): string
    {
        return 'Business Hour Access Rule';
    }
    
    public function evaluate(array $context): bool
    {
        // Custom rule logic
        $currentHour = date('H');
        return $currentHour >= 9 && $currentHour <= 17;
    }
}