Adds a scope-based access control layer to all 81 existing API routes. Sits alongside existing session-cookie auth (does not replace it). What this PR does: - Audits every existing route and documents required scope per route (see documentation/auth/route-scope-audit.md) - Adds classes/auth/scope.php with 10 scope constants and role→scope defaults - Adds classes/auth/scope_middleware.php with requireScope/requireAnyScope/requireRole - Applies require*() calls to all 81 existing routes - Adds ScopeMiddlewareTest (unit, 178 lines) and RouteScopeTest (integration, 212 lines) Coexistence note: This branch's classes/auth/scope.php is a stub that will be replaced by classes/auth/scope_registry.php (from TRU-145 / PR #396) when that PR merges first. The two have compatible APIs. Refs: TRU-149
149 lines
5.7 KiB
PHP
149 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use classes\response;
|
|
use classes\router;
|
|
use classes\workfeed;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
use app\auth\Scope;
|
|
use app\auth\ScopeMiddleware;
|
|
|
|
class moduleWorkfeedRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
global /** @var response $response */
|
|
/** @var router $router */
|
|
$router, $response;
|
|
|
|
$this->get('/modules/workfeed/employees', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/modules/workfeed/employees');
|
|
global $response;
|
|
$this->requirePermission('modules_workfeed_employees_view');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
|
|
$result = (new workfeed())->listEmployees();
|
|
(new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_EMPLOYEES_LIST', 'Listed Workfeed employees');
|
|
$response->success($result, 200);
|
|
}, [
|
|
'modules_workfeed_employees_view' => 'List Workfeed employees',
|
|
]);
|
|
|
|
$this->get('/modules/workfeed/employees/{id}', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/modules/workfeed/employees/{id}');
|
|
global $response;
|
|
$this->requirePermission('modules_workfeed_employees_view');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
|
|
$result = (new workfeed())->getEmployee((string)$this->fromRoute('id'));
|
|
(new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_EMPLOYEE_GET', 'Fetched Workfeed employee');
|
|
$response->success($result, 200);
|
|
}, [
|
|
'modules_workfeed_employees_view' => 'Get a specific Workfeed employee',
|
|
]);
|
|
|
|
$this->get('/modules/workfeed/shifts', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/modules/workfeed/shifts');
|
|
global $response;
|
|
$this->requirePermission('modules_workfeed_shifts_view');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
|
|
$filters = $this->filterRequestParameters($_GET, [
|
|
'startFrom',
|
|
'startTo',
|
|
'from',
|
|
'to',
|
|
'employeeID',
|
|
'employeeId',
|
|
'released',
|
|
]);
|
|
|
|
if (!isset($filters['startFrom']) && isset($filters['from'])) {
|
|
$filters['startFrom'] = $filters['from'];
|
|
}
|
|
if (!isset($filters['startTo']) && isset($filters['to'])) {
|
|
$filters['startTo'] = $filters['to'];
|
|
}
|
|
if (!isset($filters['employeeID']) && isset($filters['employeeId'])) {
|
|
$filters['employeeID'] = $filters['employeeId'];
|
|
}
|
|
|
|
unset($filters['from'], $filters['to'], $filters['employeeId']);
|
|
|
|
if (!isset($filters['startFrom']) || trim((string)$filters['startFrom']) === '') {
|
|
$response->error('Missing required query parameter: startFrom', 400);
|
|
}
|
|
if (!isset($filters['startTo']) || trim((string)$filters['startTo']) === '') {
|
|
$response->error('Missing required query parameter: startTo', 400);
|
|
}
|
|
|
|
$result = (new workfeed())->listShifts($filters);
|
|
(new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_SHIFTS_LIST', 'Listed Workfeed shifts');
|
|
$response->success($result, 200);
|
|
}, [
|
|
'modules_workfeed_shifts_view' => 'List Workfeed shifts',
|
|
]);
|
|
|
|
$this->get('/modules/workfeed/shifts/{id}', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/modules/workfeed/shifts/{id}');
|
|
global $response;
|
|
$this->requirePermission('modules_workfeed_shifts_view');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
|
|
$result = (new workfeed())->getShift((string)$this->fromRoute('id'));
|
|
(new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_SHIFT_GET', 'Fetched Workfeed shift');
|
|
$response->success($result, 200);
|
|
}, [
|
|
'modules_workfeed_shifts_view' => 'Get a specific Workfeed shift',
|
|
]);
|
|
|
|
$this->get('/modules/workfeed/departments', function () {
|
|
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/modules/workfeed/departments');
|
|
global $response;
|
|
$this->requirePermission('modules_workfeed_departments_view');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
|
|
$result = (new workfeed())->listDepartments();
|
|
(new logs_o())->add('modules_workfeed', 'global', 1, $user->id, 'MODULES_WORKFEED_DEPARTMENTS_LIST', 'Listed Workfeed departments');
|
|
$response->success($result, 200);
|
|
}, [
|
|
'modules_workfeed_departments_view' => 'List Workfeed departments',
|
|
]);
|
|
}
|
|
|
|
private function filterRequestParameters(array $parameters, array $allowedKeys): array
|
|
{
|
|
$allowed = array_flip($allowedKeys);
|
|
$filtered = [];
|
|
|
|
foreach ($parameters as $key => $value) {
|
|
if (isset($allowed[$key]) && $value !== '' && $value !== null) {
|
|
$filtered[$key] = $value;
|
|
}
|
|
}
|
|
|
|
return $filtered;
|
|
}
|
|
}
|