Add role cloning functionality and adjust permission methods.
Introduced a method to clone groups with their permissions and added a corresponding API endpoint for role cloning. Adjusted existing permission methods to ensure consistent behavior and naming, swapping the implementation of add/remove permissions. Improved permission fetching logic for cleaner data handling.
This commit is contained in:
@@ -66,19 +66,37 @@ class groups_o extends db
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a permission to a group
|
||||
* Remove a permission from a group
|
||||
*
|
||||
* @param string $permission_id
|
||||
* @return self $this
|
||||
* @throws Exception If the permission was not added successfully
|
||||
* @throws Exception If the permission was not removed successfully
|
||||
*/
|
||||
public function addPermission(string $permission_id): self
|
||||
public function removePermission(string $permission_id): self
|
||||
{
|
||||
self::requireSelected();
|
||||
(new groups_permissions_o())->add($this->id, $permission_id);
|
||||
(new groups_permissions_o())->remove($this->id, $permission_id);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clone a group with all its permissions
|
||||
* @param string $name
|
||||
* @param string $description
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function clone(string $name, string $description): void
|
||||
{
|
||||
self::requireSelected();
|
||||
$group = new groups_o();
|
||||
$group->add($name, $description);
|
||||
$permissions = (new groups_permissions_o())->getGroupPermissions($this->id);
|
||||
foreach ( $permissions as $permission ) {
|
||||
$group->addPermission($permission['permission']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a group
|
||||
* @param string $name
|
||||
@@ -114,16 +132,16 @@ class groups_o extends db
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a permission from a group
|
||||
* Add a permission to a group
|
||||
*
|
||||
* @param string $permission_id
|
||||
* @return self $this
|
||||
* @throws Exception If the permission was not removed successfully
|
||||
* @throws Exception If the permission was not added successfully
|
||||
*/
|
||||
public function removePermission(string $permission_id): self
|
||||
public function addPermission(string $permission_id): self
|
||||
{
|
||||
self::requireSelected();
|
||||
(new groups_permissions_o())->remove($this->id, $permission_id);
|
||||
(new groups_permissions_o())->add($this->id, $permission_id);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -114,9 +114,14 @@ class groups_permissions_o extends db
|
||||
*/
|
||||
public function getGroupPermissions(int $group_id): array
|
||||
{
|
||||
return self::getFieldsWhere([
|
||||
$raw_permissions = self::getFieldsWhere([
|
||||
'group_id' => $group_id
|
||||
], ['permission']);
|
||||
$permissions = [];
|
||||
foreach ( $raw_permissions as $permission ) {
|
||||
$permissions[] = $permission['permission'];
|
||||
}
|
||||
return $permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -157,5 +157,29 @@ class rolesRoute
|
||||
'delete_role_permission' => 'Remove a permission from a role'
|
||||
]
|
||||
);
|
||||
|
||||
self::post('/roles/clone', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
self::requirePermission('clone_role');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
self::requireParameters(['id']);
|
||||
self::requireType((int)self::getParameter('id'), self::type_int());
|
||||
$group = new groups_o();
|
||||
$group->select((int)self::getParameter('id'));
|
||||
$group->requireSelected();
|
||||
$group->clone($group->name->value() . ' - Klon', (string)$group->description->value());
|
||||
(new logs_o())->add('roles', 'global', 1, $user->id, 'ROLES', 'User cloned a role');
|
||||
$response->success($group->asArray());
|
||||
} else {
|
||||
(new logs_o())->add('roles', 'global', 0, 0, 'ROLES', 'User tried to clone a role without a valid session');
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'clone_role' => 'Clone a role'
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user