Files
api/services/nginx/app/objects/groups_o.php
T
Jepp9350 491f268108 Add role and permission management with enhanced access control
This update introduces functionalities for managing roles, permissions, and access control across departments. Key additions include methods for filtering, restricting, and handling user permissions, as well as new APIs for assigning/removing permissions to/from roles. Access to resources like orders, bookings, and plate scans is now securely tied to department-specific permissions.
2025-02-27 17:54:18 +01:00

129 lines
3.6 KiB
PHP

<?php
namespace objects;
use classes\db;
use classes\object_property;
use Exception;
use traits\db_object_t;
class groups_o extends db
{
use db_object_t;
public object_property $name;
public object_property $description;
public object_property $created_at;
public function structure(): void
{
self::setTable('groups');
}
/**
* Get the group by name
* @throws Exception If an object is not selected
*/
public function asArray(): array
{
self::requireSelected();
return [
'id' => (int)$this->id,
'name' => (string)$this->name->value(),
'description' => (string)$this->description->value(),
'created_at' => (string)$this->created_at->value(),
'permissions' => $this->getPermissions()
];
}
/**
* Get all permissions for a group
* @return array
* @throws Exception If the group was not selected
*/
public function getPermissions(): array
{
self::requireSelected();
return (new groups_permissions_o())->getGroupPermissions($this->id);
}
/**
* Get department ids, that the group has permission (department_access_:group_id) to access
* @return array An array of department ids e.g. [1, 2, 3]
* @throws Exception If the group was not selected
*/
public function getDepartments(): array
{
self::requireSelected();
$permissions = (new groups_permissions_o())->getGroupPermissionsMatching($this->id, '/^department_access_[0-9]+$/');
$departments = [];
foreach ( $permissions as $value ) {
$tmp = explode('_', $value['permission']);
$departments[] = (int)$tmp[2];
}
return $departments;
}
/**
* Add a permission to a group
*
* @param string $permission_id
* @return self $this
* @throws Exception If the permission was not added successfully
*/
public function addPermission(string $permission_id): self
{
self::requireSelected();
(new groups_permissions_o())->add($this->id, $permission_id);
return $this;
}
/**
* Add a group
* @param string $name
* @param string $description
* @return void
* @throws Exception If the object was not created successfully
*/
public function add(string $name, string $description): void
{
$tmp_id = self::add_object([
'name' => $name,
'description' => $description
]);
$this->id = $tmp_id;
self::getObjectProperties();
self::objectChanged();
if (!$this->id) {
throw new Exception('The group was not created successfully.');
}
}
public function getObjectProperties(): void
{
$this->name = new object_property($this->table, $this->id, 'name', 'string', false);
$this->description = new object_property($this->table, $this->id, 'description', 'string', false);
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
self::objectChanged();
}
public function objectChanged(): void
{
//TODO: Add cache invalidation
}
/**
* Remove a permission from a group
*
* @param string $permission_id
* @return self $this
* @throws Exception If the permission was not removed successfully
*/
public function removePermission(string $permission_id): self
{
self::requireSelected();
(new groups_permissions_o())->remove($this->id, $permission_id);
return $this;
}
}