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

190 lines
8.4 KiB
PHP

<?php
namespace routes;
use classes\authentication;
use objects\logs_o;
use objects\product_options_o;
use objects\products_o;
use traits\route_t;
class productsRoute
{
use route_t;
public function run(): void
{
$this->get('/products', function () {
function parseProduct($product): array
{
return [
'id' => (int)$product['id'],
'name' => (string)$product['name'],
'description' => (string)$product['description'],
'price' => (int)$product['price'],
'category' => (int)$product['category'],
'piktogram' => (string)$product['piktogram'],
'economic_product_id' => (int)$product['economic_product_id'],
'apply_category_discount' => (int)$product['apply_category_discount'],
'created_at' => (string)$product['created_at'],
'updated_at' => (string)$product['updated_at'],
'addons' => (new product_options_o())->getProductOptions($product['id'])
];
}
// Require the user to be logged in
global $response;
$this->requirePermission('list_products');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the category is set in the request
$data = $_GET ?? [];
// Check if the category is set
if (isset($data['category'])) {
// Log the incident
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products in category ' . $data['category']);
// Return the list of products
$products = (new products_o())->listObjectsByCategory($data['category']);
// Check if the department_id is set
if (isset($data['department_id'])) {
// Apply the departments unique pricing
$products = (new products_o())->applyDepartmentPricing((array)$products, (int)$data['department_id']);
}
$response->success(
$products
);
}
// Log the incident
(new logs_o())->add('products', 'global', 1, $user->id, 'LIST_PRODUCTS', 'Successfully listed products');
// Check if the department_id is set
if (isset($data['department_id'])) {
// Return the list of products
$response->success(
(new products_o())->applyDepartmentPricing((array)(new products_o())->listObjectsWithPaginationIfSet(
function ($product) {
return parseProduct($product);
}
), (int)$data['department_id'])
);
}
// Return the list of products
$response->success(
(new products_o())->listObjectsWithPaginationIfSet(function ($product) {
return parseProduct($product);
})
);
} else {
// Log the incident
(new logs_o())->add('products', 'global', 1, 0, 'LIST_PRODUCTS', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'list_products' => 'List all products'
]
);
$this->post('/products', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('add_product');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Get the request data
$name = $response->getRequestParameter('name');
$description = $response->getRequestParameter('description') ?? null;
$price = $response->getRequestParameter('price');
$category = $response->getRequestParameter('category') ?? null;
$piktogram = $response->getRequestParameter('piktogram') ?? null;
$economicProductId = $response->getRequestParameter('economicProductId') ?? null;
// Check if the required fields are set
self::requireParameters(
[
'name',
'price'
]
);
// Add the product
(new products_o())->add(
(string)$name,
(string)$description ?? '',
(int)$price,
(int)$category ?? '',
(int)$piktogram ?? null,
(int)$economicProductId ?? null
);
// Log the incident
(new logs_o())->add('products', 'global', 1, $user->id, 'ADD_PRODUCT', 'Product name: ' . $name);
// Return a success message
$response->success(['message' => 'Product added successfully']);
} else {
// Log the incident
(new logs_o())->add('products', 'global', 1, 0, 'ADD_PRODUCT', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'add_product' => 'Add a product'
]
);
$this->put('/products', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('edit_product');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Check if the required fields are set
self::requireParameters(['id']);
$id = $response->getRequestParameter('id');
$product = (new products_o())->select((int)$id);
// Check if the product exists
if (!$product->exists()) {
$response->error('Product not found', 404);
}
// Update the fields that are set
if (self::isParametersSet(['name'])) {
$product->name->set($response->getRequestParameter('name'));
}
if (self::isParametersSet(['description'])) {
$product->description->set($response->getRequestParameter('description') ?? '');
}
if (self::isParametersSet(['price'])) {
$product->price->set($response->getRequestParameter('price'));
}
if (self::isParametersSet(['category'])) {
$product->category->set($response->getRequestParameter('category') ?? '');
}
if (self::isParametersSet(['piktogram'])) {
$product->piktogram->set($response->getRequestParameter('piktogram') ?? '');
}
if (self::isParametersSet(['economic_product_id'])) {
$product->economic_product_id->set($response->getRequestParameter('economic_product_id') ?? '');
}
if (self::isParametersSet(['apply_category_discount'])) {
$product->apply_category_discount->set($response->getRequestParameter('apply_category_discount') ? 1 : 0);
}
(new logs_o())->add('products', 'global', 1, $user->id, 'EDIT_PRODUCT', 'Product id: ' . $id);
// Return a success message
$response->success(['message' => 'Product edited successfully']);
} else {
// Log the incident
(new logs_o())->add('products', 'global', 1, 0, 'EDIT_PRODUCT', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 400);
}
},
[
'edit_product' => 'Edit a product'
]
);
}
}