Add vehicle type handling in customer_vehicles_o, including vehicle type retrieval, setting, and creation in XLVask. Extend route logic to support vehicle type updates and auto-start on LPR configurations, incorporating enhanced validation and permission checks. Refactor and streamline helper classes for consistency and functionality expansion.

This commit is contained in:
Jepp9350
2025-06-19 15:01:22 +02:00
parent 75e3efcb89
commit 29aa63bb08
7 changed files with 328 additions and 29 deletions
+174 -1
View File
@@ -27,6 +27,37 @@ class vehiclesRoute
if ($user) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'Successfully listed own vehicles');
// Check if the id parameter is set
if ($this->isParametersSet(['id'])) {
// Get the id parameter
$id = (int)$this->getParameter('id');
$this->requireType($id, self::type_int());
$this->requireMinValue($id, 1);
$this->requireMaxValue($id, 9999999999);
// Get the vehicle object
$vehicle = (new customer_vehicles_o())->select($id);
// Check if the vehicle exists
if (!$vehicle->exists()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to list the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to list other users vehicles
if (!$user->hasPermission('list_vehicles_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'LIST_OWN_VEHICLES', 'User tried to list a vehicle from another user');
// Return an error
$response->error('You are not allowed to list vehicles from other users', 403);
}
}
// Return the vehicle as an array
$response->success(
[...$vehicle->asArray()]
);
}
// Return the list of the user's vehicles
$vehicles_o = new customer_vehicles_o();
// Check if the user is allowed to list other user's vehicles
@@ -212,7 +243,7 @@ class vehiclesRoute
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to edit the vehicle
if ($vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to edit other users vehicles
if (!$user->hasPermission('edit_vehicle_other')) {
// Log the incident
@@ -418,6 +449,148 @@ class vehiclesRoute
]
);
$this->post('/vehicles/set-auto-start-on-lpr', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('set_auto_start_on_lpr');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_AUTO_START_ON_LPR', 'User set auto start on LPR');
// Get the request data
self::requireParameters([
'id',
'active',
]);
$id = (int)self::getParameter('id');
self::requireType($id, self::type_int());
self::requireMinValue($id, 1);
self::requireMaxValue($id, 9999999999);
// Validate the autoStartOnLpr (active) parameter
$autoStartOnLpr = (bool)self::getParameter('active');
self::requireType($autoStartOnLpr, self::type_bool());
// Get the vehicle object
$vehicle = (new customer_vehicles_o())->select($id);
// Check if the vehicle exists
if (!$vehicle->exists()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_AUTO_START_ON_LPR', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to edit the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to edit other users vehicles
if (!$user->hasPermission('edit_vehicle_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_AUTO_START_ON_LPR', 'User tried to set auto start on LPR from another user');
// Return an error
$response->error('You are not allowed to edit vehicles from other users', 403);
}
}
// Set the auto start on LPR
$vehicle->setAutoStartOnLpr(
$autoStartOnLpr
);
// Return the vehicle as an array
$response->success(
[...$vehicle->asArray()]
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'SET_AUTO_START_ON_LPR', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'set_auto_start_on_lpr' => 'Set auto start on LPR',
'set_auto_start_on_lpr_other' => 'Set auto start on LPR for another user\'s vehicle',
]
);
$this->post('/vehicles/set-vehicle-type-id', function () {
// Require the user to be logged in
global $response;
$this->requirePermission('set_vehicle_type_id');
// Get the user object
$user = (new authentication())->get_user();
// Check if the request was successful
if ($user) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'User set vehicle type ID');
// Get the request data
self::requireParameters([
'id',
'vehicleTypeId',
]);
$id = (int)self::getParameter('id');
self::requireType($id, self::type_int());
self::requireMinValue($id, 1);
self::requireMaxValue($id, 9999999999);
// Validate the vehicleTypeId
$vehicleTypeId = (string)self::getParameter('vehicleTypeId');
self::requireType($vehicleTypeId, self::type_string());
self::requireMinLength('vehicleTypeId', 1);
self::requireMaxLength('vehicleTypeId', 50);
// Get the vehicle object
$vehicle = (new customer_vehicles_o())->select($id);
// Check if the vehicle exists
if (!$vehicle->exists()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'Vehicle not found');
// Return an error
$response->error('Vehicle not found', 404);
}
// Check if the user is allowed to edit the vehicle
if ((int)$vehicle->customer_id->value() !== (int)$user->customer_number->value()) {
// Check if the user has permission to edit other users vehicles
if (!$user->hasPermission('edit_vehicle_other')) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'User tried to set vehicle type ID from another user');
// Return an error
$response->error('You are not allowed to edit vehicles from other users', 403);
}
}
// Get the customer object
$customer = (new users_o())->getUserByCustomerNumber((int)$vehicle->customer_id->value());
// Check if the vehicle is registered in the XL Vask system
if (!$vehicle->hasXLVask()) {
// Check if the customer has an XL Vask customer account
if (!$customer->hasXLVaskCustomerAccount()) {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, $user->id, 'SET_VEHICLE_TYPE_ID', 'User tried to set vehicle type ID on a vehicle that is not registered in the XL Vask system, without a customer account');
// Return an error
$response->error('Vehicle is not registered in the XL Vask system', 400);
} else {
$vehicle->createXLVaskVehicle($vehicleTypeId);
}
} else {
// Set the vehicle type ID
$vehicle->setVehicleTypeId(
$vehicleTypeId
);
}
// Return the vehicle as an array
$response->success(
[...$vehicle->asArray()]
);
} else {
// Log the incident
(new logs_o())->add('vehicles', 'global', 1, 0, 'SET_VEHICLE_TYPE_ID', 'No user found, or invalid session');
// Return an error
$response->error('Invalid session', 401);
}
},
[
'set_vehicle_type_id' => 'Set vehicle type ID',
'set_vehicle_type_id_other' => 'Set vehicle type ID for another user\'s vehicle',
]
);
$this->get('/superuser/users-with-vehicle-subscriptions', function () {
// Require the user to be logged in
global $response;