Files
api/services/nginx/app/routes/userInvoicesRoute.php
T
Jeppe B ab6c3ba5b6 Fix route permission instance calls (#344)
## Root cause

`route_t::hasPermission()` and `requirePermission()` are instance
methods. Route code was invoking them with `self::`; the new XL Vask
hall-scope helper made that call from a genuinely static context,
causing PHP to throw:

`Non-static method routes\\xlvaskUsageLogsRoute::hasPermission() cannot
be called statically`

## Changes

- Invoke route permission methods through `$this` across all 273
executable legacy calls in 45 route classes.
- Make `xlvaskUsageLogsRoute::allowedHallIdsForUser()` an instance
helper and update all 13 callers.
- Preserve the existing all-scope and own-scope hall selection rules.
- Add a token-aware regression test that rejects executable
`self::hasPermission()` and `self::requirePermission()` calls, while
ignoring comments.
- Add focused XL Vask tests for global scanner hall scope and
group-limited own scope.
- Update affected route contract assertions to the instance-call form.

## Verification

- PHP lint: all 53 changed PHP files
- Focused PHPStan: changed XL Vask route and both new regression tests —
clean
- Focused regression slice: 58 passed, 748 assertions
- Full local unit suite: 1,300 passed, 9,442 assertions (1 unrelated
existing warning, 1 environment skip)
- Full local API suite: 285 passed, 11,704 assertions
- Exact-SHA GitHub Tests workflow: all 7 jobs passed (unit, API,
integration, legacy, edge gateway, and supporting checks)
- Independent exact-SHA QA gate: PASS, no findings
- Independent exact-SHA security gate: PASS, no findings
- Independent exact-SHA reviewer gate: PASS, no findings
- Remote comparison: exactly one commit ahead of
`40b104abed7723a7d1b7028190ecda0e7aeef829`; all 53 remote blob hashes
matched the reviewed worktree

## Delivery state

Draft only for human review. No merge or deployment is included. Qodana
is skipped while the PR remains draft and is therefore not represented
as a passed gate.
2026-08-04 16:04:41 +02:00

116 lines
5.0 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use objects\collected_order_invoices_o;
use objects\logs_o;
use traits\route_t;
class userInvoicesRoute
{
use route_t;
public function run(): void
{
$this->get('/user/invoices', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('user_invoices');
$user = (new authentication())->get_user();
if (!$user) {
(new logs_o())->add('user_invoices', 'global', 0, 0, 'USER_INVOICES', 'User not logged in');
$response->error('Invalid session', 400);
}
// Return the invoices owned by the user
$collected_order_invoices = new collected_order_invoices_o();
$result = $collected_order_invoices->listObjectsWithPaginationIfSet(
function ($invoice_collection_array) {
$tmp_invoice = (new collected_order_invoices_o())->select((int)$invoice_collection_array['id']);
return [
...$tmp_invoice->asArray(),
];
},
$collected_order_invoices->forceRestrictFilters(
[
// This makes sure that the user can only see orders from the departments they explicitly have access to
'customer_number' => $user->customer_number->value()
]
)
);
$response->success($result);
},
[
'user_invoices' => 'Get the invoices of the user',
]
);
$this->put('/collected-invoices', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('user_invoices');
$user = (new authentication())->get_user();
if (!$user) {
(new logs_o())->add('user_invoices', 'global', 0, 0, 'USER_INVOICES', 'User not logged in');
$response->error('Invalid session', 400);
}
self::requireParameters(['id']);
self::requireType((int)self::getParameter('id'), self::type_int());
$id = (int)self::getParameter('id');
// Make sure the id is valid
self::requireMinValue($id, 1);
self::requireSameLength($id, self::getParameter('id'));
$is_superuser = $this->hasPermission('superuser');
if (!self::isParametersSet(['po_number']) && !self::isParametersSet(['closed_at'])) {
$response->error('Missing required parameters: po_number, closed_at', 400);
}
if (self::isParametersSet(['closed_at']) && !$is_superuser) {
$response->error('Forbidden: only superusers can update closed_at', 403);
}
// Make sure optional fields are valid
if (self::isParametersSet(['po_number'])) {
self::requireType((string)self::getParameter('po_number'), self::type_string());
self::requireMinLength('po_number', 0);
self::requireMaxLength('po_number', 255);
}
$closed_at = null;
if (self::isParametersSet(['closed_at'])) {
$closed_at = self::getParameter('closed_at');
if ($closed_at !== null && $closed_at !== '') {
self::requireType((string)$closed_at, self::type_string());
self::requireDateFormat((string)$closed_at, self::FORMAT_DATE());
}
}
// Get the invoice
$collected_order_invoices = new collected_order_invoices_o();
$invoice = $collected_order_invoices->select((int)$id);
$invoice->requireSelected();
// Make sure the invoice belongs to the user
if ((int)$invoice->customer_number->value() !== (int)$user->customer_number->value() && !$is_superuser) {
(new logs_o())->add(
'user_invoices',
'global',
0,
0,
'USER_INVOICES',
'User not allowed to access this invoice (invoice_customer=' . (int)$invoice->customer_number->value() . ', user_customer=' . (int)$user->customer_number->value() . ')'
);
$response->error('Forbidden: invoice does not belong to authenticated user', 403);
}
// Update the invoice
if (self::isParametersSet(['po_number'])) {
$invoice->po_number->set((string)self::getParameter('po_number'));
}
if (self::isParametersSet(['closed_at'])) {
$invoice->closed_at->set($closed_at === null || $closed_at === '' ? null : date('Y-m-d 23:59:59', strtotime((string)$closed_at . ' 00:00:01')));
}
// Return success
$response->success($invoice->asArray());
},
[
'user_invoices' => 'Get the invoices of the user',
]
);
}
}