134 lines
6.3 KiB
PHP
134 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\logs_o;
|
|
use objects\users_o;
|
|
use traits\route_t;
|
|
|
|
class usersRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->get('/users', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('list_users');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, $user->id, 'LIST_USERS', 'Successfully listed users');
|
|
// Return the list of users
|
|
$response->success(
|
|
(new users_o())->listObjects()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, 0, 'LIST_USERS', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->get('/users/customer', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('get_user_from_customer_number');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Check if the customer number is valid
|
|
if (!(new users_o())->automaticGetTargetUserFromRequest()->exists()) {
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_FROM_CUSTOMER_NUMBER', 'Customer not found, not imported');
|
|
$response->error('Customer not found', 400);
|
|
}
|
|
// Check
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, $user->id, 'GET_USER_FROM_CUSTOMER_NUMBER', 'Successfully retrieved user from customer number');
|
|
// Return the list of users
|
|
$response->success(
|
|
(new users_o())->automaticGetTargetUserFromRequest()->getCustomerEcocomicData()->asArray()
|
|
);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, 0, 'GET_USER_FROM_CUSTOMER_NUMBER', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->post('/users', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('add_user');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['customer_number'])) { $response->error('Customer number is required', 400); }
|
|
if (!isset($data['password'])) { $response->error('Password is required', 400); }
|
|
if (!isset($data['role'])) { $response->error('Role is required', 400); }
|
|
// Add the user
|
|
(new users_o())->add($data['customer_number'], $data['password'], (int)$data['role']);
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, $user->id, 'ADD_USER', 'Successfully added a user');
|
|
// Return a success message
|
|
$response->success(['message' => 'User added']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, 0, 'ADD_USER', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
|
|
$this->put('/users', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
$this->requirePermission('edit_user');
|
|
// Get the user object
|
|
$user = (new authentication())->get_user();
|
|
// Check if the request was successful
|
|
if ($user) {
|
|
// Get the post data
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
// Check if the required fields are set
|
|
if (!isset($data['id'])) { $response->error('ID is required', 400); }
|
|
if (!isset($data['customer_number'])) { $response->error('Customer number is required', 400); }
|
|
// Check if a new role is set, if not, set it to null to prevent it from being updated
|
|
if (!isset($data['role']) || $data['role'] === 'null' || $data['role'] === '') { $data['role'] = null; }
|
|
// If the role is set, require the edit_user_role permission
|
|
if ($data['role']) {
|
|
$this->requirePermission('edit_user_role');
|
|
}
|
|
// Check if a new password is set, if not, set it to null to prevent it from being updated
|
|
if (!isset($data['password']) || $data['password'] === 'null' || $data['password'] === '') { $data['password'] = null; }
|
|
// If the password is set, require the edit_user_password permission
|
|
if ($data['password']) {
|
|
$this->requirePermission('edit_user_password');
|
|
}
|
|
// Edit the user
|
|
(new users_o())->edit((int)$data['id'], (string)$data['customer_number'], $data['role'], $data['password']);
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, $user->id, 'EDIT_USER', 'Successfully edited a user with ID: ' . $data['id']);
|
|
// Return a success message
|
|
$response->success(['message' => 'User edited']);
|
|
} else {
|
|
// Log the incident
|
|
(new logs_o())->add('users', 'global', 1, 0, 'EDIT_USER', 'No user found, or invalid session');
|
|
// Return an error
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
});
|
|
}
|
|
} |