GAC’s entity and role system provides a flexible way to manage permissions and restrictions across different types of users and organizational structures. The system supports multiple entity types, role-based access control (RBAC), and a sophisticated priority mechanism.
Attempting to load permissions or restrictions without setting an entity first will throw an exception.
// Validation (GAC.php:141-143)if (empty($this->entityType) || empty($this->entityId)) { throw new \Exception('Entity type and ID must be set before loading data.');}
Entities can be assigned multiple roles, each with a specific priority value:
// Get roles query (DatabaseAdapter.php:49-59)SELECT b.id, b.code, a.priorityFROM `gac_role_entity` AS aINNER JOIN `gac_role` AS b ON a.role_id = b.idWHERE a.entity_type = :entity_type AND a.entity_id = :entity_id AND a.is_disabled = '0' AND b.is_disabled = '0' AND a.deleted_at IS NULL AND b.deleted_at IS NULLORDER BY a.priority ASC
Permissions and restrictions assigned directly to the entity always take precedence.
// Personal priority (GAC.php:382-383)if ($record['from_entity_type'] !== '0') { $record['priority'] = -1; // Personal permissions first}
2
Role-Based (Priority 0+)
Permissions inherited from roles use the role’s assigned priority value.
// Role priority (GAC.php:384-386)else { // Use role priority from assignment $record['priority'] = $roleData['priority'][$record['from_entity_id']] ?? 100;}
Once a permission is assigned for a module, subsequent matches are ignored:
// First match logic (GAC.php:428)if (!array_key_exists($moduleData['code'], $response)) { // This is the first permission for this module - use it $response[$moduleData['code']] = [ 'i' => $permission['id'], 'd' => $moduleData['is_developing'], 'f' => $permission['feature'], 'l' => $permission['level'] ];}// Else: skip this permission, module already has one
$gac = new GAC();$gac->setDatabase($pdo);$gac->setCache();// For a user$gac->setEntity('user', 12345);$permissions = $gac->getPermissions();// For a client$gac->setEntity('client', 789);$clientPermissions = $gac->getPermissions();
// Purge cache for specific users$gac->purgePermissionsBy('user', [12345, 67890]);// Purge cache for a client$gac->purgeRestrictionsBy('client', [789]);// Purge all entities with a specific role$gac->purgePermissionsBy('role', [101]);// Purge everything$gac->purgePermissionsBy('global');
Grant temporary elevated access via personal permissions:
// User normally has Staff role (priority 30)// Temporarily grant Admin permissions as personal (priority -1)// Personal permissions override role-based access