Files
api/services/nginx/app/routes/customerCodeDepartmentRoute.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

93 lines
3.8 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use objects\logs_o;
use objects\users_o;
use traits\route_t;
use app\auth\Scope;
use app\auth\ScopeMiddleware;
class customerCodeDepartmentRoute
{
use route_t;
public function run(): void
{
$this->get('/admin/customer/code', function () {
ScopeMiddleware::requireScope(Scope::CUSTOMER_READ, '/admin/customer/code');
// Require the user to be logged in
global $response;
$this->requirePermission('get_customer_code');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Get the query parameters from the URL
$data = $_GET;
// Check if the required fields are set
if (!isset($data['customer_number']) && !isset($data['user_id'])) {
$response->error('User ID or Customer Number is required', 400);
}
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, $user->id, 'GET_CUSTOMER_CODE', 'Successfully retrieved customer code');
// Check if the user exists
if (!(new users_o())->automaticGetTargetUserFromRequest()->exists()) {
$response->error('Customer not found', 400);
}
// Return the customer code
$response->success(
['code' => (new users_o())->automaticGetTargetUserFromRequest()->getCode()]
);
} else {
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, 0, 'GET_CUSTOMER_CODE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'get_customer_code' => 'Get customer code'
]
);
$this->post('/admin/customer/code', function () {
ScopeMiddleware::requireScope(Scope::CUSTOMER_WRITE, '/admin/customer/code');
// Require the user to be logged in
global $response;
$this->requirePermission('add_customer_code');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Get the post data
$data = json_decode(file_get_contents('php://input'), true);
// Check if the required fields are set
if (!isset($data['customer_number']) && !isset($data['user_id']) && !isset($data['code'])) {
$response->error('User ID, Customer Number, and Code are required', 400);
}
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, $user->id, 'ADD_CUSTOMER_CODE', 'Successfully added customer code');
// Check if the user exists
if (!(new users_o())->automaticGetTargetUserFromRequest()->exists()) {
$response->error('Customer not found', 400);
}
// Add the customer code
(new users_o())->automaticGetTargetUserFromRequest()->setCode($data['code']);
// Return a success message
$response->success(['message' => 'Customer code added']);
} else {
// Log the incident
(new logs_o())->add('customer_codes', 'global', 1, 0, 'ADD_CUSTOMER_CODE', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'add_customer_code' => 'Add a customer code'
]
);
}
}