Skip to main content

Overview

The Permissions class manages a collection of module permissions for an entity. It provides methods to check permission existence, retrieve individual permissions, and manage the permission list.

Constructor

Creates a new Permissions instance with a permission list.
public function __construct(array $list)
list
array
required
Array of permissions indexed by module code. Each permission contains:
  • i (int): Permission ID
  • d (string): Module developing status (‘0’ or ‘1’)
  • f (array): Feature codes (e.g., [‘0’, ‘1’, ‘2’] for create, read, update)
  • l (int): Permission level
Typically, you don’t instantiate this directly. Use GAC::getPermissions() instead.
// Retrieved from GAC
$permissions = $gac->setEntity('user', 123)->getPermissions();

Methods

has()

Checks if permission exists for a specific module.
public function has(string $moduleCode): bool
moduleCode
string
required
The unique code identifying the module (e.g., 'invoices', 'products', 'users')
return
bool
Returns true if the entity has permission to access the module, false otherwise
if ($permissions->has('invoices')) {
    // User has permission to access invoices module
    echo "Access granted to invoices";
} else {
    echo "Access denied";
}

get()

Retrieves a Permission instance for a specific module.
public function get(string $moduleCode): Permission|null
moduleCode
string
required
The unique code identifying the module
return
Permission|null
Returns a Permission instance with permission details, or null if no permission exists
Throws: Exception if permission data is invalid
$invoicePermission = $permissions->get('invoices');

if ($invoicePermission !== null) {
    // Check specific features
    if ($invoicePermission->hasFeature('create')) {
        echo "Can create invoices";
    }
    
    // Get permission level
    $level = $invoicePermission->getLevel();
    echo "Permission level: " . $level;
}

getList()

Returns the complete permission list.
public function getList(): array
return
array
Array of all permissions indexed by module code
$allPermissions = $permissions->getList();

foreach ($allPermissions as $moduleCode => $permData) {
    echo "Module: $moduleCode\n";
    echo "Permission ID: {$permData['i']}\n";
    echo "Level: {$permData['l']}\n";
}

setList()

Replaces the entire permission list.
public function setList(array $list): Permissions
list
array
required
New permission array to replace the current list
return
Permissions
Returns the Permissions instance for method chaining
$newPermissions = [
    'products' => [
        'i' => 5,
        'd' => '0',
        'f' => ['1', '2'], // read, update
        'l' => 2
    ]
];

$permissions->setList($newPermissions);

Permission Data Structure

Each permission in the list follows this structure:
i
int
Permission record ID from the database
d
string
Module development status: '0' (stable) or '1' (developing)
f
array
Array of feature codes:
  • '0': create
  • '1': read
  • '2': update
  • '3': delete
  • '4': trash
  • '5': dev
l
int
Permission level (numeric value indicating access tier)

Usage Examples

Basic Permission Check

$gac = new GAC();
$gac->setDatabase($pdo)
    ->setEntity('user', 456);

$permissions = $gac->getPermissions();

// Simple existence check
if (!$permissions->has('admin_panel')) {
    throw new Exception('Access denied');
}

Detailed Permission Inspection

// Get all modules user can access
$allPermissions = $permissions->getList();
$accessibleModules = array_keys($allPermissions);

echo "User has access to: " . implode(', ', $accessibleModules);

// Check each module's features
foreach ($accessibleModules as $module) {
    $permission = $permissions->get($module);
    
    echo "\nModule: $module";
    echo "\n  Can create: " . ($permission->hasFeature('create') ? 'Yes' : 'No');
    echo "\n  Can read: " . ($permission->hasFeature('read') ? 'Yes' : 'No');
    echo "\n  Can update: " . ($permission->hasFeature('update') ? 'Yes' : 'No');
    echo "\n  Can delete: " . ($permission->hasFeature('delete') ? 'Yes' : 'No');
    echo "\n  Level: " . $permission->getLevel();
}

Conditional UI Rendering

// Show UI elements based on permissions
$permissions = $gac->setEntity('user', $userId)->getPermissions();

if ($permissions->has('reports')) {
    $reportPerm = $permissions->get('reports');
    
    // Show reports menu
    echo '<a href="/reports">Reports</a>';
    
    // Show create button only if user can create
    if ($reportPerm->hasFeature('create')) {
        echo '<button>New Report</button>';
    }
    
    // Show export button for higher permission levels
    if ($reportPerm->getLevel() >= 3) {
        echo '<button>Export All</button>';
    }
}

Permission Caching Pattern

// Get from cache (default behavior)
$permissions = $gac->getPermissions(true);

// Force refresh from database
$permissions = $gac->getPermissions(false);

// After updating permissions in database, clear cache
// ... update permissions in DB ...
$gac->clearCache();

// Next call will fetch fresh data
$permissions = $gac->getPermissions();

See Also

Build docs developers (and LLMs) love