Files
api/services/nginx/app/routes/userOrdersRoute.php
T
Jepp9350 d28cb172a0 Add permission definitions to route handlers
This update introduces explicit permission definitions for various route handlers across multiple routes. These changes enhance clarity and allow for more granular control over route access based on defined permissions. The updates ensure better manageability and scalability of endpoint permissions.
2025-02-20 14:33:42 +01:00

89 lines
3.4 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use objects\logs_o;
use objects\orders_o;
use traits\route_t;
class userOrdersRoute
{
use route_t;
public function run(): void
{
$this->get('/user/orders', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('list_own_orders');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('orders', 'global', 1, $user->id, 'LIST_OWN_ORDERS', 'Successfully listed own orders');
// Return the list of the user's orders
$response->success(
(new orders_o())->getCustomerOrdersPaginated(
$user->customer_number->value(),
($this->fromRequest('page') ?? 1),
($this->fromRequest('limit') ?? 10),
['id' => 'DESC'],
$this->fromRequest('search') === null ? '' : $this->fromRequest('search'),
$this->fromRequest('filters') === null ? [] :
$response->parseFilters($this->fromRequest('filters')) ?? []
)
);
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'LIST_OWN_ORDERS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_own_orders' => 'List own orders'
]
);
$this->get('/user/order', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('fetch_own_order');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
$data = $_GET;
if (!isset($data['id'])) {
$response->error('id parameter is required', 400);
}
// Make sure the user exists
if (!$user->exists()) {
$response->error('User not found', 400);
}
// Make sure the user is allowed to fetch the order
if (!$user->hasAccessToOrder($data['id'])) {
$response->error('You are not allowed to fetch this order', 400);
}
// Log the incident
(new logs_o())->add('orders', 'global', 1, $user->id, 'FETCH_OWN_ORDER', 'Successfully fetched own order');
// Return the list of the user's orders
$response->success(
(new orders_o())->getOrderById($data['id'])->includeIncludes()->asArray()
);
} else {
// Log the incident
(new logs_o())->add('orders', 'global', 1, 0, 'FETCH_OWN_ORDER', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'fetch_own_order' => 'Fetch own order'
]
);
}
}