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

154 lines
6.4 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use classes\economic;
use classes\invoice_store;
use objects\collected_order_invoices_o;
use objects\economic_module_orders;
use objects\logs_o;
use objects\orders_o;
use objects\users_o;
use traits\route_t;
use app\auth\Scope;
use app\auth\ScopeMiddleware;
class invoicesRoute
{
use route_t;
public function run(): void
{
$this->get('/invoices/draft', function () {
ScopeMiddleware::requireScope(Scope::INVOICE_READ, '/invoices/draft');
// Require the user to be logged in
global $response;
$this->requirePermission('get_invoice_draft');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the invoice id is set
if (!(string)$this->fromRequest('id')) {
$response->error('id parameter is required', 400);
}
// Create economic object
$economic = new economic();
// Get the draft invoice
$result = $economic->invoices->draft->get($this->fromRequest('id'));
if (isset($result->message)) {
$response->error($result->message, $result->httpStatusCode);
}
// Log the incident
(new logs_o())->add('invoices', 'global', 1, $user->id, 'GET_INVOICE', 'Successfully retrieved invoice');
// Return the list of departments
$response->success(
$economic->invoices->draft->get($this->fromRequest('id'))
);
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'LIST_ORDERS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'get_invoice_draft' => 'Get invoice draft'
]
);
$this->post('/invoices/draft/close', function () {
ScopeMiddleware::requireScope(Scope::INVOICE_WRITE, '/invoices/draft/close');
// Require the user to be logged in
global $response;
$this->requirePermission('close_invoice_draft');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the invoice id is set
if (!$this->fromRequest('id')) {
$response->error('id parameter is required', 400);
}
// Create economic object
$economic_module_orders = new economic_module_orders();
// Get the user object from the draft invoice id
/** @var users_o $target_user */
$target_user = $economic_module_orders->getUserFromDraftId($this->fromRequest('id'));
// Get the draft invoice
$target_user->unsetOpenInvoiceDraft();
// Log the incident
(new logs_o())->add('invoices', 'global', 1, $user->id, 'CLOSE_INVOICE', 'Successfully closed invoice');
// Return the list of departments
$response->success(
['message' => 'Invoice closed']
);
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'LIST_ORDERS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'close_invoice_draft' => 'Close invoice draft'
]
);
$this->get('/invoices/pdf', function () {
ScopeMiddleware::requireScope(Scope::INVOICE_READ, '/invoices/pdf');
// Require the user to be logged in
global $response;
$this->requirePermission('get_invoice_pdf');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the invoice id is set
if (!$this->fromRequest('id')) {
$response->error('id parameter is required', 400);
}
// Get the collected order invoice object
$collected_order_invoice = (new collected_order_invoices_o())->select((int)$this->fromRequest('id'));
// Make sure the user owns the invoice
if ((int)$collected_order_invoice->customer_number->value() !== (int)$user->customer_number->value()) {
$response->error('You do not have permission to access this invoice', 403);
}
// Check if the invoice is booked
$is_booked = $collected_order_invoice->isBooked();
if (!$is_booked) {
$response->error('Invoice is not booked yet', 400);
}
$booked_invoice_id = (int)$collected_order_invoice->booked_invoice_id->value();
// Create economic object
$economic = new economic();
// Get the draft invoice
$invoicePathFile = $economic->invoices->pdf->get($booked_invoice_id);
// Add the pdf to the invoice store
$invoice_store = new invoice_store();
$invoice_store->uploadFile('invoice_' . $booked_invoice_id . '.pdf', $invoicePathFile);
// Log the incident
(new logs_o())->add('invoices', 'global', 1, $user->id, 'GET_INVOICE_PDF', 'Successfully retrieved invoice pdf');
// Remove the file
unlink($invoicePathFile);
// Return the download link
$response->success(
['message' => 'Invoice PDF retrieved', 'url' => $invoice_store->getInvoiceDownloadUrl($booked_invoice_id)]
);
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'LIST_ORDERS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'get_invoice_pdf' => 'Get invoice pdf'
]
);
}
}