Implement departmentSelfserveTasksRoute with CRUD operations and department-level access control
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
<?php
|
||||
/**
|
||||
* Route for department self-serve tasks
|
||||
*/
|
||||
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use classes\response;
|
||||
use objects\department_selfserve_tasks_o;
|
||||
use objects\logs_o;
|
||||
use traits\route_t;
|
||||
|
||||
class departmentSelfserveTasksRoute
|
||||
{
|
||||
use route_t;
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
/**
|
||||
* List department self-serve tasks
|
||||
*/
|
||||
$this->get('/department/selfserve/tasks', function () {
|
||||
global $response;
|
||||
$this->requirePermission('list_department_selfserve_tasks');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
(new logs_o())->add('department_selfserve_tasks', 'global', 1, $user->id, 'LIST_TASKS', 'User listed department self-serve tasks');
|
||||
|
||||
$tasks_o = new department_selfserve_tasks_o();
|
||||
$authorized_department_ids = $user->getGroup()->getDepartments();
|
||||
|
||||
// If an ID is provided, return that specific task
|
||||
if (self::isParametersSet(['id'])) {
|
||||
$tasks_o->select((int)self::getParameter('id'));
|
||||
if ($tasks_o->exists()) {
|
||||
if (!in_array((int)$tasks_o->department->value(), $authorized_department_ids)) {
|
||||
$response->error('You do not have access to this department', 403);
|
||||
}
|
||||
$response->success($tasks_o->asArray());
|
||||
} else {
|
||||
$response->error('Task not found', 404);
|
||||
}
|
||||
}
|
||||
|
||||
$filters = [];
|
||||
if (self::isParametersSet(['department'])) {
|
||||
$requested_department = (int)self::getParameter('department');
|
||||
if (!in_array($requested_department, $authorized_department_ids)) {
|
||||
$response->error('You do not have access to this department', 403);
|
||||
}
|
||||
$filters['department'] = $requested_department;
|
||||
} else {
|
||||
$filters['department'] = $authorized_department_ids;
|
||||
}
|
||||
|
||||
if (self::isParametersSet(['lane'])) {
|
||||
$filters['lane'] = (int)self::getParameter('lane');
|
||||
}
|
||||
|
||||
if (self::isParametersSet(['product'])) {
|
||||
$filters['product'] = (int)self::getParameter('product');
|
||||
}
|
||||
|
||||
if (self::isParametersSet(['question'])) {
|
||||
$filters['question'] = (int)self::getParameter('question');
|
||||
}
|
||||
|
||||
$response->success(
|
||||
$tasks_o->setSearchableFields(['id', 'department', 'lane', 'product', 'question', 'task', 'description'])
|
||||
->listObjectsWithPaginationIfSet(function ($task) {
|
||||
$t = new department_selfserve_tasks_o();
|
||||
$t->select((int)$task['id']);
|
||||
return $t->asArray();
|
||||
}, $tasks_o->forceRestrictFilters($filters))
|
||||
);
|
||||
} else {
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
}, [
|
||||
'list_department_selfserve_tasks' => 'List all department self-serve tasks'
|
||||
]);
|
||||
|
||||
/**
|
||||
* Add a department self-serve task
|
||||
*/
|
||||
$this->post('/department/selfserve/tasks', function () {
|
||||
global $response;
|
||||
$this->requirePermission('add_department_selfserve_tasks');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
$department = (int)$response->getRequestParameter('department');
|
||||
$lane = (int)$response->getRequestParameter('lane');
|
||||
$product = (int)$response->getRequestParameter('product');
|
||||
$question = $response->isRequestParameterSet('question') ? (int)$response->getRequestParameter('question') : null;
|
||||
$task = (string)$response->getRequestParameter('task');
|
||||
$description = (string)$response->getRequestParameter('description');
|
||||
$order_priority = (int)($response->getRequestParameter('order_priority') ?? 0);
|
||||
|
||||
if (!$department || !$lane || !$product || !$task || !$description) {
|
||||
$response->error('Missing required fields', 400);
|
||||
}
|
||||
|
||||
$authorized_department_ids = $user->getGroup()->getDepartments();
|
||||
if (!in_array($department, $authorized_department_ids)) {
|
||||
$response->error('You do not have access to this department', 403);
|
||||
}
|
||||
|
||||
try {
|
||||
$task_o = (new department_selfserve_tasks_o())->add(
|
||||
$department,
|
||||
$lane,
|
||||
$product,
|
||||
$question,
|
||||
$task,
|
||||
$description,
|
||||
$order_priority
|
||||
);
|
||||
(new logs_o())->add('department_selfserve_tasks', 'global', 1, $user->id, 'ADD_TASK', 'User added a department self-serve task: ' . $task);
|
||||
$response->success($task_o->asArray());
|
||||
} catch (\Exception $e) {
|
||||
$response->error($e->getMessage(), 500);
|
||||
}
|
||||
} else {
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
}, [
|
||||
'add_department_selfserve_tasks' => 'Add a department self-serve task'
|
||||
]);
|
||||
|
||||
/**
|
||||
* Update a department self-serve task
|
||||
*/
|
||||
$this->put('/department/selfserve/tasks', function () {
|
||||
global $response;
|
||||
$this->requirePermission('edit_department_selfserve_tasks');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
self::requireParameters(['id']);
|
||||
$id = (int)self::getParameter('id');
|
||||
$task_o = new department_selfserve_tasks_o();
|
||||
$task_o->select($id);
|
||||
|
||||
if (!$task_o->exists()) {
|
||||
$response->error('Task not found', 404);
|
||||
}
|
||||
|
||||
$authorized_department_ids = $user->getGroup()->getDepartments();
|
||||
if (!in_array((int)$task_o->department->value(), $authorized_department_ids)) {
|
||||
$response->error('You do not have access to this department', 403);
|
||||
}
|
||||
|
||||
if (self::isParametersSet(['department'])) {
|
||||
$new_department = (int)self::getParameter('department');
|
||||
if (!in_array($new_department, $authorized_department_ids)) {
|
||||
$response->error('You do not have access to the new department', 403);
|
||||
}
|
||||
$task_o->department->set($new_department);
|
||||
}
|
||||
if (self::isParametersSet(['lane'])) {
|
||||
$task_o->lane->set((int)self::getParameter('lane'));
|
||||
}
|
||||
if (self::isParametersSet(['product'])) {
|
||||
$task_o->product->set((int)self::getParameter('product'));
|
||||
}
|
||||
if (self::isParametersSet(['question'])) {
|
||||
$task_o->question->set(self::getParameter('question') === null ? null : (int)self::getParameter('question'));
|
||||
}
|
||||
if (self::isParametersSet(['task'])) {
|
||||
$task_o->task->set((string)self::getParameter('task'));
|
||||
}
|
||||
if (self::isParametersSet(['description'])) {
|
||||
$task_o->description->set((string)self::getParameter('description'));
|
||||
}
|
||||
if (self::isParametersSet(['order_priority'])) {
|
||||
$task_o->order_priority->set((int)self::getParameter('order_priority'));
|
||||
}
|
||||
|
||||
(new logs_o())->add('department_selfserve_tasks', 'global', 1, $user->id, 'EDIT_TASK', 'User updated department self-serve task ID: ' . $id);
|
||||
$response->success($task_o->asArray());
|
||||
} else {
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
}, [
|
||||
'edit_department_selfserve_tasks' => 'Edit a department self-serve task'
|
||||
]);
|
||||
|
||||
/**
|
||||
* Delete a department self-serve task
|
||||
*/
|
||||
$this->delete('/department/selfserve/tasks', function () {
|
||||
global $response;
|
||||
$this->requirePermission('delete_department_selfserve_tasks');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
self::requireParameters(['id']);
|
||||
$id = (int)self::getParameter('id');
|
||||
$task_o = new department_selfserve_tasks_o();
|
||||
$task_o->select($id);
|
||||
|
||||
if (!$task_o->exists()) {
|
||||
$response->error('Task not found', 404);
|
||||
}
|
||||
|
||||
$authorized_department_ids = $user->getGroup()->getDepartments();
|
||||
if (!in_array((int)$task_o->department->value(), $authorized_department_ids)) {
|
||||
$response->error('You do not have access to this department', 403);
|
||||
}
|
||||
|
||||
$task_o->delete();
|
||||
(new logs_o())->add('department_selfserve_tasks', 'global', 1, $user->id, 'DELETE_TASK', 'User deleted department self-serve task ID: ' . $id);
|
||||
$response->success('Task deleted');
|
||||
} else {
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
}, [
|
||||
'delete_department_selfserve_tasks' => 'Delete a department self-serve task'
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user