Files
api/services/nginx/app/routes/departmentSelfserveQuestionsRoute.php
T
OpenClaw 51a87655d6 feat(auth): add scope-based access control to all existing routes (TRU-149)
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
2026-08-17 11:43:13 +00:00

253 lines
12 KiB
PHP

<?php
/**
* Route for department self-serve questions
*/
namespace routes;
use classes\authentication;
use modules\selfserve\classes\selfserve_config_versioning;
use classes\response;
use objects\department_selfserve_questions_o;
use objects\logs_o;
use traits\route_t;
use app\auth\Scope;
use app\auth\ScopeMiddleware;
class departmentSelfserveQuestionsRoute
{
use route_t;
public function run(): void
{
/**
* List department self-serve questions
*/
$this->get('/department/selfserve/questions', function () {
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/department/selfserve/questions');
global $response;
$this->requirePermission('list_department_selfserve_questions');
$user = (new authentication())->get_user();
if ($user) {
(new logs_o())->add('department_selfserve_questions', 'global', 1, $user->id, 'LIST_QUESTIONS', 'User listed department self-serve questions');
$questions_o = new department_selfserve_questions_o();
$authorized_department_ids = $user->getGroup()->getDepartments();
$has_view_all_permission = $this->hasPermission('view_all_department_selfserve_questions');
// If an ID is provided, return that specific question
if (self::isParametersSet(['id'])) {
$questions_o->select((int)self::getParameter('id'));
if ($questions_o->exists()) {
if (!$this->canAccessDepartment($authorized_department_ids, (int)$questions_o->department->value())) {
if (!$has_view_all_permission) {
$this->forbidDepartmentAccess((int)$questions_o->department->value(), ['view_all_department_selfserve_questions']);
}
}
$response->success($questions_o->asArray());
} else {
$response->error('Question not found', 404);
}
}
$filters = [];
if (self::isParametersSet(['department'])) {
$requested_department = (int)self::getParameter('department');
if (!$this->canAccessDepartment($authorized_department_ids, $requested_department) && !$has_view_all_permission) {
$this->forbidDepartmentAccess($requested_department, ['view_all_department_selfserve_questions']);
}
$filters['department'] = $requested_department;
} else {
if (!$has_view_all_permission) {
if (empty($authorized_department_ids)) {
$authorized_department_ids = [0];
} else {
$authorized_department_ids[] = 0;
}
$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');
}
$response->success(
$questions_o->setSearchableFields(['id', 'department', 'lane', 'product', 'question', 'description', 'deleted_at'])
->listObjectsWithPaginationIfSet(function ($question) {
$q = new department_selfserve_questions_o();
$q->select((int)$question['id']);
return $q->asArray();
}, $questions_o->forceRestrictFilters($filters))
);
} else {
$response->error('Invalid session', 400);
}
}, [
'list_department_selfserve_questions' => 'List all department self-serve questions',
'view_all_department_selfserve_questions' => 'View questions from all departments'
]);
/**
* Add a department self-serve question
*/
$this->post('/department/selfserve/questions', function () {
ScopeMiddleware::requireScope(Scope::CUSTOMER_WRITE, '/department/selfserve/questions');
global $response;
$this->requirePermission('add_department_selfserve_questions');
$user = (new authentication())->get_user();
if ($user) {
$department = $response->isRequestParameterSet('department') ? (int)$response->getRequestParameter('department') : 0;
$lane = $response->isRequestParameterSet('lane') ? (int)$response->getRequestParameter('lane') : 0;
$product = $response->isRequestParameterSet('product') ? (int)$response->getRequestParameter('product') : 0;
$question = (string)$response->getRequestParameter('question');
$description = (string)$response->getRequestParameter('description');
$condition_id = null;
if ($response->isRequestParameterSet('condition_id')) {
$condition_id_param = $response->getRequestParameter('condition_id');
if (!is_null($condition_id_param) && $condition_id_param !== '' && $condition_id_param !== 'null') {
$condition_id = (int)$condition_id_param;
}
}
$order_priority = (int)($response->getRequestParameter('order_priority') ?? 0);
if (!$question || !$description) {
$response->error('Missing required fields: question and description', 400);
}
$authorized_department_ids = $user->getGroup()->getDepartments();
if (!$this->canAccessDepartment($authorized_department_ids, $department)) {
$this->forbidDepartmentAccess($department);
}
try {
$question_o = (new department_selfserve_questions_o())->add(
$department,
$lane,
$product,
$question,
$description,
$condition_id,
$order_priority
);
(new selfserve_config_versioning())->syncDraftFromLegacyForDepartment($department);
(new logs_o())->add('department_selfserve_questions', 'global', 1, $user->id, 'ADD_QUESTION', 'User added a department self-serve question: ' . $question);
$response->success($question_o->asArray());
} catch (\Exception $e) {
$response->error($e->getMessage(), 500);
}
} else {
$response->error('Invalid session', 400);
}
}, [
'add_department_selfserve_questions' => 'Add a department self-serve question'
]);
/**
* Update a department self-serve question
*/
$this->put('/department/selfserve/questions', function () {
ScopeMiddleware::requireScope(Scope::CUSTOMER_WRITE, '/department/selfserve/questions');
global $response;
$this->requirePermission('edit_department_selfserve_questions');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters(['id']);
$id = (int)self::getParameter('id');
$question_o = new department_selfserve_questions_o();
$question_o->select($id);
if (!$question_o->exists()) {
$response->error('Question not found', 404);
}
$originalDepartment = (int)$question_o->department->value();
$authorized_department_ids = $user->getGroup()->getDepartments();
if (!$this->canAccessDepartment($authorized_department_ids, (int)$question_o->department->value())) {
$this->forbidDepartmentAccess((int)$question_o->department->value());
}
if (self::isParametersSet(['department'])) {
$new_department = (int)self::getParameter('department');
if (!$this->canAccessDepartment($authorized_department_ids, $new_department)) {
$this->forbidDepartmentAccess($new_department);
}
$question_o->department->set($new_department);
}
if (self::isParametersSet(['lane'])) {
$question_o->lane->set((int)self::getParameter('lane'));
}
if (self::isParametersSet(['product'])) {
$question_o->product->set((int)self::getParameter('product'));
}
if (self::isParametersSet(['question'])) {
$question_o->question->set((string)self::getParameter('question'));
}
if (self::isParametersSet(['description'])) {
$question_o->description->set((string)self::getParameter('description'));
}
if (self::isParametersSet(['condition_id'])) {
$condition_id = self::getParameter('condition_id');
$question_o->condition_id->set($condition_id === null || $condition_id === '' || $condition_id === 'null' ? null : (int)$condition_id);
}
if (self::isParametersSet(['order_priority'])) {
$question_o->order_priority->set((int)self::getParameter('order_priority'));
}
(new selfserve_config_versioning())->syncDraftFromLegacyForDepartment($originalDepartment);
(new selfserve_config_versioning())->syncDraftFromLegacyForDepartment((int)$question_o->department->value());
(new logs_o())->add('department_selfserve_questions', 'global', 1, $user->id, 'EDIT_QUESTION', 'User updated department self-serve question ID: ' . $id);
$response->success($question_o->asArray());
} else {
$response->error('Invalid session', 400);
}
}, [
'edit_department_selfserve_questions' => 'Edit a department self-serve question'
]);
/**
* Delete a department self-serve question
*/
$this->delete('/department/selfserve/questions', function () {
ScopeMiddleware::requireScope(Scope::CUSTOMER_WRITE, '/department/selfserve/questions');
global $response;
$this->requirePermission('delete_department_selfserve_questions');
$user = (new authentication())->get_user();
if ($user) {
self::requireParameters(['id']);
$id = (int)self::getParameter('id');
$question_o = new department_selfserve_questions_o();
$question_o->select($id);
if (!$question_o->exists()) {
$response->error('Question not found', 404);
}
$authorized_department_ids = $user->getGroup()->getDepartments();
if (!$this->canAccessDepartment($authorized_department_ids, (int)$question_o->department->value())) {
$this->forbidDepartmentAccess((int)$question_o->department->value());
}
$question_o->delete();
(new selfserve_config_versioning())->syncDraftFromLegacyForDepartment((int)$question_o->department->value());
(new logs_o())->add('department_selfserve_questions', 'global', 1, $user->id, 'DELETE_QUESTION', 'User deleted department self-serve question ID: ' . $id);
$response->success('Question deleted');
} else {
$response->error('Invalid session', 400);
}
}, [
'delete_department_selfserve_questions' => 'Delete a department self-serve question'
]);
}
private function canAccessDepartment(array $authorizedDepartmentIds, int $departmentId): bool
{
return $departmentId === 0 || in_array($departmentId, $authorizedDepartmentIds, true);
}
}