Files
api/services/nginx/app/modules/subusers/classes/subuser_user_grant.php
T
Jeppe Bundgaard c83b7d49e4 Add permission node system and extend subuser grants functionality
- Introduce a comprehensive permission system for subusers, including permission nodes and types with support for vehicles, bookings, orders, subusers, and self-serve modules.
- Implement `subusers_user`, `subuser_user_grant`, and `subusers_permission_node` classes for managing subuser permissions and grants.
- Extend `subuser_grants_o` with methods to retrieve permissions for subusers linked to customers.
- Add traits and enumerations to streamline permission handling across modules.
- Update subuser session handling to include token-based subuser lookups.
2026-02-11 17:13:25 +01:00

59 lines
2.2 KiB
PHP

<?php
namespace modules\subusers\classes;
use modules\subusers\interfaces\subusers_user_grant_i;
use modules\subusers\traits\subuser_user_grant_t;
use modules\subusers\traits\subusers_user_permissions_t;
use objects\subuser_grants_o;
class subuser_user_grant implements subusers_user_grant_i
{
use subuser_user_grant_t,
subusers_user_permissions_t;
public int $subuser_id;
public int $customer_number;
/**
* @var subusers_permission_node[] $permission_nodes An array of permission nodes that define the permissions granted to the subuser for this customer
*/
protected array $permission_nodes;
public function __construct(int $subuser_id, int $customer_number)
{
$this->subuser_id = $subuser_id;
$this->customer_number = $customer_number;
// Get the grants and enable the permission nodes based on the grants
$this->loadGrants();
}
public function loadGrants(): void
{
$subuser_grants_o = new subuser_grants_o();
$subuser_grants = $subuser_grants_o->getGrantsForSubuserAndCustomer($this->subuser_id, $this->customer_number);
// Loop through the permissions and enable the corresponding permission nodes
foreach ($subuser_grants as $permission) {
$this->enablePermissionNode($permission);
}
}
private function enablePermissionNode(string $permission): void
{
// Get all properties extending the subusers_permission_nodes class
$reflection = new \ReflectionClass($this);
$properties = $reflection->getProperties();
foreach ($properties as $property) {
$propertyType = $property->getType();
if ($propertyType && is_a($propertyType->getName(), subusers_permission_nodes::class, true)) {
$permissionNodes = $property->getValue($this);
if (is_array($permissionNodes)) {
foreach ($permissionNodes as $node) {
if ($node->nodeKey->name === $permission) {
$node->value = true;
return;
}
}
}
}
}
}
}