- Define `defaultPermissions` constant in `subuser_grants_o` for managing default subuser permissions. - Adjust `add` method to use `defaultPermissions` instead of a hardcoded array. - Update `/subusers` route to reflect this change.
100 lines
3.6 KiB
PHP
100 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace objects;
|
|
|
|
use classes\db;
|
|
use classes\object_property;
|
|
use Exception;
|
|
use modules\subusers\helpers\subusers_permission_node_key;
|
|
use Random\RandomException;
|
|
use traits\db_object_t;
|
|
|
|
class subuser_grants_o extends db
|
|
{
|
|
use db_object_t;
|
|
|
|
public object_property $billing_customer_number;
|
|
public object_property $subuser;
|
|
public object_property $enabled;
|
|
public object_property $note;
|
|
public object_property $permissions;
|
|
public object_property $created_at;
|
|
public object_property $updated_at;
|
|
public object_property $deleted_at;
|
|
const defaultPermissions = [
|
|
subusers_permission_node_key::VEHICLES_LIST,
|
|
subusers_permission_node_key::SELFSERVE_ADD,
|
|
subusers_permission_node_key::BOOKINGS_LIST,
|
|
subusers_permission_node_key::BOOKINGS_ADD,
|
|
subusers_permission_node_key::BOOKINGS_EDIT,
|
|
subusers_permission_node_key::BOOKINGS_DELETE,
|
|
];
|
|
|
|
|
|
public function structure(): void
|
|
{
|
|
$this->setTable('subuser_grants');
|
|
}
|
|
|
|
public function objectChanged(): void
|
|
{
|
|
// No need to invalidate the cache, since the plate_scans object is not cached
|
|
}
|
|
|
|
public function getObjectProperties(): void
|
|
{
|
|
$this->billing_customer_number = new object_property($this->table, $this->id, 'billing_customer_number', 'int');
|
|
$this->subuser = new object_property($this->table, $this->id, 'subuser', 'int');
|
|
$this->enabled = new object_property($this->table, $this->id, 'enabled', 'bool');
|
|
$this->note = new object_property($this->table, $this->id, 'note', 'string');
|
|
$this->permissions = new object_property($this->table, $this->id, 'permissions', 'json');
|
|
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'timestamp');
|
|
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'timestamp');
|
|
$this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'timestamp');
|
|
}
|
|
|
|
|
|
/**
|
|
* Add a new subuser grant to the database.
|
|
* @param int $billing_customer_number
|
|
* @param int $subuser
|
|
* @param bool $enabled
|
|
* @param string|null $note
|
|
* @param array|null $permissions
|
|
* @return $this
|
|
* @throws Exception If the object creation fails
|
|
*/
|
|
public function add(int $billing_customer_number, int $subuser, bool $enabled, ?string $note, ?array $permissions = self::defaultPermissions): subuser_grants_o
|
|
{
|
|
global $db;
|
|
$tmp = $this->add_object([
|
|
'billing_customer_number' => (int)$billing_customer_number,
|
|
'subuser' => (int)$subuser,
|
|
'enabled' => (bool)$enabled,
|
|
'note' => !empty($note) ? $db->escape_string($note) : null,
|
|
'permissions' => !empty($permissions) ? json_encode($permissions) : json_encode([]),
|
|
]);
|
|
$this->id = (int)$tmp;
|
|
$this->getObjectProperties();
|
|
return $this;
|
|
}
|
|
|
|
public function getGrantsForSubuserAndCustomer(int $subuser_id, int $customer_number): array
|
|
{
|
|
$grants = self::getFieldsWhere([
|
|
'billing_customer_number' => $customer_number,
|
|
'subuser' => $subuser_id,
|
|
'enabled' => 1,
|
|
'deleted_at' => null,
|
|
], ['permissions']);
|
|
// Extract permissions from the grants
|
|
$permissions = [];
|
|
foreach ($grants as $grant) {
|
|
$grant_permissions = json_decode($grant['permissions'], true);
|
|
if (is_array($grant_permissions)) {
|
|
$permissions = array_merge($permissions, $grant_permissions);
|
|
}
|
|
}
|
|
return $permissions;
|
|
}
|
|
} |