Add Stripe terminal and department variables API endpoints
This update introduces new endpoints for managing Stripe terminal readers, locations, and department-specific configurations. It also adds support for creating, updating, and retrieving department variables along with enhanced validation, logging, and permission checks. These updates improve integration and expand functionality for Stripe and department-related operations.
This commit is contained in:
@@ -97,12 +97,14 @@ class authentication implements authentication_i
|
||||
{
|
||||
// Get the token from the headers
|
||||
$headers = getallheaders();
|
||||
if (!isset($headers['Authorization'])) {
|
||||
if (!isset($headers['Authorization']) && !isset($_GET['token'])) {
|
||||
return false;
|
||||
}
|
||||
$token = $headers['Authorization'];
|
||||
// Strip the Bearer prefix
|
||||
$token = str_replace('Bearer ', '', $token);
|
||||
$token = $_GET['token'] ?? $headers['Authorization'];
|
||||
// Strip the Bearer prefix (If the token is from the headers)
|
||||
if (isset($headers['Authorization'])) {
|
||||
$token = str_replace('Bearer ', '', $token);
|
||||
}
|
||||
// Get the token from the database
|
||||
$token = (new plate_scanners_o())->getPlateScannerByApiKey($token);
|
||||
// Check if the token exists
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace stripe\endpoints;
|
||||
use Exception;
|
||||
use Stripe\Collection;
|
||||
use Stripe\Exception\ApiErrorException;
|
||||
use Stripe\Terminal\Location;
|
||||
use Stripe\Terminal\Reader;
|
||||
use traits\stripe_endpoint_t;
|
||||
|
||||
class stripe_endpoint_readers
|
||||
@@ -22,5 +24,135 @@ class stripe_endpoint_readers
|
||||
return self::getClient()->terminal->readers->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all readers for a location
|
||||
* @throws ApiErrorException If the location is not set
|
||||
* @throws Exception If the location is not set
|
||||
*/
|
||||
public function listLocation(string $location_id): Collection
|
||||
{
|
||||
return self::getClient()->terminal->readers->all([
|
||||
'location' => $location_id
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a reader
|
||||
* @param string $label
|
||||
* @param string $location
|
||||
* @return Reader
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function register(string $label, string $location): Reader
|
||||
{
|
||||
return self::getClient()->terminal->readers->create([
|
||||
'label' => $label,
|
||||
'location' => $location
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all locations
|
||||
* @return Collection
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function listLocations(): Collection
|
||||
{
|
||||
return self::getClient()->terminal->locations->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a location
|
||||
* @param string $location_id
|
||||
* @return Location
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function retrieveLocation(string $location_id): Location
|
||||
{
|
||||
return self::getClient()->terminal->locations->retrieve($location_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a reader
|
||||
* @param string $reader_id
|
||||
* @return Reader
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function retrieve(string $reader_id): Reader
|
||||
{
|
||||
return self::getClient()->terminal->readers->retrieve($reader_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a location
|
||||
* @param string $label
|
||||
* @return Location
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function registerLocation(string $label): Location
|
||||
{
|
||||
return self::getClient()->terminal->locations->create([
|
||||
'label' => $label
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a location
|
||||
* @param string $location_id
|
||||
* @param string $label
|
||||
* @return Location
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function updateLocation(string $location_id, string $label): Location
|
||||
{
|
||||
return self::getClient()->terminal->locations->update($location_id, [
|
||||
'label' => $label
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a reader
|
||||
* @param string $reader_id
|
||||
* @param string $label
|
||||
* @return Reader
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(string $reader_id, string $label): Reader
|
||||
{
|
||||
return self::getClient()->terminal->readers->update($reader_id, [
|
||||
'label' => $label
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a location
|
||||
* @param string $location_id
|
||||
* @return Location
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function deleteLocation(string $location_id): Location
|
||||
{
|
||||
return self::getClient()->terminal->locations->delete($location_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a reader
|
||||
* @param string $reader_id
|
||||
* @return Reader
|
||||
* @throws ApiErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function delete(string $reader_id): Reader
|
||||
{
|
||||
return self::getClient()->terminal->readers->delete($reader_id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use Exception;
|
||||
use traits\db_object_t;
|
||||
|
||||
class department_variables_o extends db
|
||||
{
|
||||
use db_object_t;
|
||||
|
||||
public int $department_id;
|
||||
|
||||
public object_property $variable;
|
||||
public object_property $value;
|
||||
public object_property $created_at;
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('department_variables');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a department variable
|
||||
* @param string $variable The variable name
|
||||
* @param string $value The variable value
|
||||
* @return void
|
||||
*
|
||||
* @throws Exception If the object was not created successfully
|
||||
*/
|
||||
public function set(string $variable, mixed $value): void
|
||||
{
|
||||
self::requireDepartmentId();
|
||||
// Check if the variable already exists
|
||||
$isAlreadyDefinedId = self::getFieldsWhere([
|
||||
'department_id' => $this->department_id,
|
||||
'variable' => $variable
|
||||
], ['id']);
|
||||
// Update the variable if it already exists
|
||||
if ($isAlreadyDefinedId) {
|
||||
$this->id = $isAlreadyDefinedId[0]['id'];
|
||||
$this->getObjectProperties();
|
||||
$this->value->set($value);
|
||||
return;
|
||||
}
|
||||
// Add the variable
|
||||
$tmp_id = self::add_object([
|
||||
'department_id' => $this->department_id,
|
||||
'variable' => $variable,
|
||||
'value' => $value
|
||||
]);
|
||||
$this->id = $tmp_id;
|
||||
self::getObjectProperties();
|
||||
self::objectChanged();
|
||||
if (!$this->id) {
|
||||
throw new Exception('The variable was not created successfully.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Require the department ID to be set, and throw an exception if it is not
|
||||
* @throws Exception If the department ID is not set.
|
||||
*/
|
||||
public function requireDepartmentId(): void
|
||||
{
|
||||
if (!$this->department_id) {
|
||||
throw new Exception('The department ID is not set.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the department variable by ID
|
||||
* @throws Exception If the department ID is not set
|
||||
*/
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
self::requireDepartmentId();
|
||||
$this->variable = new object_property($this->table, $this->id, 'variable', 'string', false);
|
||||
$this->value = new object_property($this->table, $this->id, 'value', 'string', false);
|
||||
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
{
|
||||
//TODO: Add cache invalidation
|
||||
}
|
||||
|
||||
/**
|
||||
* List all department variables
|
||||
* @throws Exception If the department ID is not set
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
self::requireDepartmentId();
|
||||
$variables = self::getFieldsWhere([
|
||||
'department_id' => $this->department_id
|
||||
], ['id']);
|
||||
$result = [];
|
||||
foreach ( $variables as $variable ) {
|
||||
$tmp = new department_variables_o();
|
||||
$tmp->select($variable['id']);
|
||||
$result[] = $tmp->asArray();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the department variable by name
|
||||
* @throws Exception If the object was not selected
|
||||
*/
|
||||
public function asArray(): array
|
||||
{
|
||||
self::requireSelected();
|
||||
return [
|
||||
'id' => (int)$this->id,
|
||||
'department_id' => (int)$this->department_id,
|
||||
'variable' => (string)$this->variable->value(),
|
||||
'value' => (string)$this->value->value(),
|
||||
'created_at' => (string)$this->created_at->value(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a department variable
|
||||
* @param string $variable The variable name
|
||||
* @return null If the variable is not set
|
||||
* @return mixed The variable value
|
||||
* @throws Exception If the department ID is not set
|
||||
*/
|
||||
public function getVariable(string $variable): mixed
|
||||
{
|
||||
self::requireDepartmentId();
|
||||
$variable = self::getFieldsWhere([
|
||||
'department_id' => $this->department_id,
|
||||
'variable' => $variable
|
||||
], ['value']);
|
||||
if (!$variable) {
|
||||
return null;
|
||||
}
|
||||
return $variable[0]['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a department variable is set
|
||||
* @param string $variable The variable name
|
||||
* @return bool If the variable is set
|
||||
* @throws Exception If the department ID is not set
|
||||
*/
|
||||
public function isSet(string $variable): bool
|
||||
{
|
||||
self::requireDepartmentId();
|
||||
$variable = self::getFieldsWhere([
|
||||
'department_id' => $this->department_id,
|
||||
'variable' => $variable
|
||||
], ['id']);
|
||||
return (bool)count($variable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nullify a department variable
|
||||
* @param string $variable The variable name
|
||||
* @return void If the variable was nullified
|
||||
* @throws Exception If the object was not selected
|
||||
* @throws Exception If the department ID is not set
|
||||
*/
|
||||
public function nullify(string $variable): void
|
||||
{
|
||||
self::requireDepartmentId();
|
||||
$variable = self::getFieldsWhere([
|
||||
'department_id' => $this->department_id,
|
||||
'variable' => $variable
|
||||
], ['id']);
|
||||
if ($variable) {
|
||||
$tmp = new department_variables_o();
|
||||
$tmp->select($variable[0]['id']);
|
||||
$tmp->requireSelected();
|
||||
$tmp->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a department
|
||||
* @param int $id The department ID
|
||||
* @return self
|
||||
*/
|
||||
public function selectDepartment(int $id): self
|
||||
{
|
||||
$this->department_id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,8 @@ namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use classes\stripe;
|
||||
use Exception;
|
||||
use traits\db_object_t;
|
||||
|
||||
class departments_o extends db
|
||||
@@ -14,6 +16,7 @@ class departments_o extends db
|
||||
public object_property $description;
|
||||
public object_property $economic_department_id; // The id of the department in the economic system (Can be null)
|
||||
public object_property $slack_webhook; // The slack webhook for the department
|
||||
public department_variables_o $variables; // The department variables object
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
@@ -26,6 +29,12 @@ class departments_o extends db
|
||||
redis->clear_departments();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a department
|
||||
* @param string $name
|
||||
* @param string $description
|
||||
* @throws Exception If the object was not created successfully
|
||||
*/
|
||||
public function create(string $name, string $description): void
|
||||
{
|
||||
global $db;
|
||||
@@ -46,12 +55,18 @@ class departments_o extends db
|
||||
redis->clear_departments();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the object properties of the department
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
self::requireSelected();
|
||||
$this->name = new object_property($this->table, $this->id, 'name', 'string', true);
|
||||
$this->description = new object_property($this->table, $this->id, 'description', 'string', false);
|
||||
$this->economic_department_id = new object_property($this->table, $this->id, 'economic_department_id', 'int', false);
|
||||
$this->slack_webhook = new object_property($this->table, $this->id, 'slack_webhook', 'string', false);
|
||||
$this->variables = (new department_variables_o())->selectDepartment($this->id);
|
||||
}
|
||||
|
||||
public function edit(int $id, string $name, string $description, int $economic_department_id): void
|
||||
@@ -193,11 +208,67 @@ class departments_o extends db
|
||||
if (!empty($name)) {
|
||||
redis->cache_department_name($department, $name);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$name = 'Unable to get department name: ' . $department;
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
return redis->get_department_name($department) ?? 'Unable to get department name: ' . $department;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Stripe terminal location department variable
|
||||
* @note This is a link to the department_variables_o::getVariable(stripe_terminal_location) method
|
||||
* @return string The Stripe terminal location id
|
||||
* @throws Exception If the department variable is not set
|
||||
* @throws Exception If the department is not selected
|
||||
*/
|
||||
public function getStripeTerminalLocation(): string
|
||||
{
|
||||
self::requireSelected();
|
||||
if (!$this->variables->isSet('stripe_terminal_location')) {
|
||||
throw new Exception('The Stripe terminal location is not set for the department.');
|
||||
}
|
||||
return $this->variables->getVariable('stripe_terminal_location');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the Stripe terminal location variable is set
|
||||
* @note This is a link to the department_variables_o::isSet(stripe_terminal_location) method
|
||||
* @return bool true If the Stripe terminal location is set
|
||||
* @throws Exception If the department is not selected
|
||||
*/
|
||||
public function isStripeTerminalLocationSet(): bool
|
||||
{
|
||||
self::requireSelected();
|
||||
return $this->variables->isSet('stripe_terminal_location');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Stripe terminal location department variable
|
||||
* @note This is a link to the department_variables_o::set(stripe_terminal_location) method
|
||||
* @param string $location The Stripe terminal location id
|
||||
* @throws Exception If the department is not selected
|
||||
* @throws Exception If the department variable is not updated successfully
|
||||
*/
|
||||
public function setStripeTerminalLocation(string $location): void
|
||||
{
|
||||
self::requireSelected();
|
||||
$this->variables->set('stripe_terminal_location', $location);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Stripe terminal readers
|
||||
* @note This is a link to the stripe_endpoint_readers::listLocation method
|
||||
* @throws Exception If the department is not selected
|
||||
* @throws Exception If the Stripe terminal location is not set for the department
|
||||
*/
|
||||
public function getStripeTerminalReaders(): \Stripe\Collection
|
||||
{
|
||||
self::requireSelected();
|
||||
if (!$this->variables->isSet('stripe_terminal_location')) {
|
||||
throw new Exception('The Stripe terminal location is not set for the department.');
|
||||
}
|
||||
return (new stripe())->readers->listLocation($this->variables->getVariable('stripe_terminal_location'));
|
||||
}
|
||||
}
|
||||
@@ -284,5 +284,9 @@ class departmentsRoute
|
||||
'delete_department_category' => 'Delete a department category'
|
||||
]
|
||||
);
|
||||
|
||||
self::get('/departments/order/recommended', function () {
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ use classes\email;
|
||||
use classes\response;
|
||||
use classes\router;
|
||||
use classes\stripe;
|
||||
use objects\departments_o;
|
||||
use objects\logs_o;
|
||||
use objects\orders_o;
|
||||
use objects\stripe_module_customers_o;
|
||||
@@ -139,22 +140,150 @@ class moduleStripeRoute
|
||||
'modules_stripe_invoice_send' => 'Send invoice'
|
||||
]
|
||||
);
|
||||
|
||||
self::get('/modules/stripe/terminals', function () {
|
||||
|
||||
self::get('/modules/stripe/terminal/readers', function () {
|
||||
global $response;
|
||||
self::requirePermission('modules_stripe_terminals_list');
|
||||
self::requirePermission('modules_stripe_terminal_readers_list');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the terminals list');
|
||||
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the readers list');
|
||||
$result = (new stripe())->readers->list();
|
||||
$response->success((object)$result);
|
||||
} else {
|
||||
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the terminals list without a valid session');
|
||||
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the readers list without a valid session');
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'modules_stripe_terminals_list' => 'List all Stripe payment terminals'
|
||||
'modules_stripe_terminal_readers_list' => 'List all Stripe terminal readers'
|
||||
]
|
||||
);
|
||||
|
||||
self::get('/modules/stripe/terminal/locations', function () {
|
||||
global $response;
|
||||
self::requirePermission('modules_stripe_terminal_locations_list');
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the terminal locations list');
|
||||
$result = (new stripe())->readers->listLocations();
|
||||
$response->success((object)$result);
|
||||
} else {
|
||||
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the terminal locations list without a valid session');
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'modules_stripe_terminal_locations_list' => 'List all Stripe terminal locations'
|
||||
]
|
||||
);
|
||||
|
||||
self::get('/modules/stripe/department/terminal/location',
|
||||
function () {
|
||||
global $response;
|
||||
self::requirePermission('modules_stripe_department_terminal_location_list');
|
||||
self::requireParameters(['id']);
|
||||
self::requireType((int)self::fromRequest('id'), self::TYPE_INT());
|
||||
// Require the id to be above 0
|
||||
if ((int)self::fromRequest('id') < 1) {
|
||||
$response->error('Invalid department ID', 400);
|
||||
}
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
// Make sure the user has access to the department
|
||||
self::requireDepartmentAccess((int)self::fromRequest('id'));
|
||||
// Make sure the department exists
|
||||
$department = (new departments_o())->select((int)self::fromRequest('id'));
|
||||
$department->requireSelected();
|
||||
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the department terminal location');
|
||||
// Get the department terminal location
|
||||
$isSet = $department->isStripeTerminalLocationSet();
|
||||
// Return the result if the department terminal location is set, otherwise return null
|
||||
$response->success([
|
||||
'id' => (
|
||||
$isSet
|
||||
? $department->getStripeTerminalLocation()
|
||||
: null
|
||||
)
|
||||
]);
|
||||
} else {
|
||||
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the department terminal location without a valid session');
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'modules_stripe_department_terminal_location_list' => 'List all department terminal location',
|
||||
'department_access_:id' => 'Access department. - :id'
|
||||
]
|
||||
);
|
||||
|
||||
self::post('/modules/stripe/department/terminal/location',
|
||||
function () {
|
||||
global $response;
|
||||
self::requirePermission('modules_stripe_department_terminal_location_set');
|
||||
self::requireParameters(['id', 'location']);
|
||||
self::requireType((int)self::fromRequest('id'), self::TYPE_INT());
|
||||
self::requireType((string)self::fromRequest('location'), 'string');
|
||||
self::requireMinLength('location', 1);
|
||||
self::requireMaxLength('location', 255);
|
||||
// Require the id to be above 0
|
||||
if ((int)self::fromRequest('id') < 1) {
|
||||
$response->error('Invalid department ID', 400);
|
||||
}
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
// Make sure the user has access to the department
|
||||
self::requireDepartmentAccess((int)self::fromRequest('id'));
|
||||
// Make sure the department exists
|
||||
$department = (new departments_o())->select((int)self::fromRequest('id'));
|
||||
$department->requireSelected();
|
||||
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User set the department terminal location');
|
||||
// Set the department terminal location
|
||||
$department->setStripeTerminalLocation((string)self::fromRequest('location'));
|
||||
// Return the result
|
||||
$response->success([
|
||||
'id' => $department->getStripeTerminalLocation()
|
||||
]);
|
||||
} else {
|
||||
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to set the department terminal location without a valid session');
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'modules_stripe_department_terminal_location_set' => 'Set department terminal location',
|
||||
'department_access_:id' => 'Access department. - :id'
|
||||
]
|
||||
);
|
||||
|
||||
self::get('/modules/stripe/department/terminal/readers',
|
||||
function () {
|
||||
global $response;
|
||||
self::requirePermission('modules_stripe_department_terminal_readers_list');
|
||||
self::requireParameters(['id']);
|
||||
self::requireType((int)self::fromRequest('id'), self::TYPE_INT());
|
||||
// Require the id to be above 0
|
||||
if ((int)self::fromRequest('id') < 1) {
|
||||
$response->error('Invalid department ID', 400);
|
||||
}
|
||||
$user = (new authentication())->get_user();
|
||||
if ($user) {
|
||||
// Make sure the user has access to the department
|
||||
self::requireDepartmentAccess((int)self::fromRequest('id'));
|
||||
// Make sure the department exists
|
||||
$department = (new departments_o())->select((int)self::fromRequest('id'));
|
||||
$department->requireSelected();
|
||||
(new logs_o())->add('modules_stripe', 'global', 1, 0, 'MODULES_STRIPE', 'User accessed the department terminal readers');
|
||||
// Get the department terminal readers
|
||||
$readers = $department->getStripeTerminalReaders();
|
||||
// Return the result
|
||||
$response->success($readers);
|
||||
} else {
|
||||
(new logs_o())->add('modules_stripe', 'global', 0, 0, 'MODULES_STRIPE', 'User tried to access the department terminal readers without a valid session');
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'modules_stripe_department_terminal_readers_list' => 'List all department terminal readers',
|
||||
'department_access_:id' => 'Access department. - :id'
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace routes;
|
||||
|
||||
use classes\authentication;
|
||||
use objects\departments_o;
|
||||
use objects\logs_o;
|
||||
use objects\plate_scanners_o;
|
||||
use traits\route_t;
|
||||
@@ -125,5 +126,54 @@ class plateScannersRoute
|
||||
'edit_number_plate_scanner' => 'Edit a number plate scanner'
|
||||
]
|
||||
);
|
||||
|
||||
self::get('/department/numberplatescanners', function () {
|
||||
// Require the user to be logged in
|
||||
global $response;
|
||||
self::requirePermission('list_department_number_plate_scanners');
|
||||
// Get the user object
|
||||
$user = (new authentication())->get_user();
|
||||
// Check if the request was successful
|
||||
if ($user) {
|
||||
// Get the department ID
|
||||
self::requireParameters(['id']);
|
||||
self::requireType((int)self::getParameter('id'), self::type_int());
|
||||
self::requireParameterIntPositive((int)self::getParameter('id'), 'id');
|
||||
// Check if the user has access to the department
|
||||
self::requireDepartmentAccess((int)self::getParameter('id'));
|
||||
// Check if the department exists
|
||||
$department = (new departments_o())->select((int)self::getParameter('id'));
|
||||
$department->requireSelected();
|
||||
// Log the incident
|
||||
(new logs_o())->add('numberplatescanners', 'global', 1, $user->id, 'LIST_DEPARTMENT_NUMBER_PLATE_SCANNERS', 'Successfully listed department number plate scanners');
|
||||
// Get the department scanners.
|
||||
$result = (new plate_scanners_o())
|
||||
->getFieldsWhere([
|
||||
'department_id' => (int)self::getParameter('id')
|
||||
], [
|
||||
'id',
|
||||
'name',
|
||||
'notes'
|
||||
]);
|
||||
// Parse the result
|
||||
foreach ( $result as $key => $value ) {
|
||||
$result[$key]['id'] = (int)$value['id'];
|
||||
}
|
||||
// Return the list of plate scanners
|
||||
$response->success(
|
||||
$result
|
||||
);
|
||||
} else {
|
||||
// Log the incident
|
||||
(new logs_o())->add('numberplatescanners', 'global', 1, 0, 'LIST_DEPARTMENT_NUMBER_PLATE_SCANNERS', 'No user found, or invalid session');
|
||||
// Return an error
|
||||
$response->error('Invalid session', 400);
|
||||
}
|
||||
},
|
||||
[
|
||||
'list_department_number_plate_scanners' => 'List all number plate scanners in a department',
|
||||
'department_access_:id' => 'Access department :id'
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -94,5 +94,31 @@ class plateScansRoute
|
||||
}, [
|
||||
'list_number_plate_scans' => 'List all number plate scans'
|
||||
]);
|
||||
|
||||
self::get('/numberplatescans/post', function () {
|
||||
/**
|
||||
* This is here because the post method is not supported by the software some of the cameras are running on.
|
||||
*/
|
||||
global $response;
|
||||
self::requirePlateScannerAuth();
|
||||
$plate_scanner = (new authentication())->get_plate_scanner();
|
||||
self::requireParameters(['plate', 'token']);
|
||||
// Validate the plate
|
||||
self::requireType('plate', 'string');
|
||||
self::requireMinLength('plate', 1);
|
||||
self::requireMaxLength('plate', 10);
|
||||
// Validate the token
|
||||
self::requireType('token', 'string');
|
||||
self::requireMinLength('token', 1);
|
||||
self::requireMaxLength('token', 100);
|
||||
// Add the number plate scanner
|
||||
(new plate_scans_o())->add($plate_scanner->id, self::getParameter('plate'));
|
||||
// Log the incident
|
||||
(new logs_o())->add('numberplatescans', $plate_scanner->department_id->value(), 1, 0, 'ADD_NUMBER_PLATE_SCANS', 'Successfully added a number plate scan: ' . self::getParameter('plate'));
|
||||
// Return a success message
|
||||
$response->success(['message' => 'License plate scan recorded.', 'plate' => self::getParameter('plate'), 'scanner' => $plate_scanner->name->value()], 201);
|
||||
}, [
|
||||
'add_number_plate_scan' => 'Add a number plate scan'
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,57 @@ trait route_t
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Require department access
|
||||
* @note This checks if the user has access to the department by checking if the user has the permission department_access_{department} (_{permission} if provided)
|
||||
* @param string $department
|
||||
* @param string|null $permission
|
||||
*/
|
||||
public function requireDepartmentAccess(string $department, string|null $permission = null): void
|
||||
{
|
||||
self::requirePermission('department_access_' . $department . ($permission ? '_' . $permission : ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Require permission
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
public function requirePermission(string $permission): bool
|
||||
{
|
||||
global $response;
|
||||
// Check if the users authorization token has the required permission
|
||||
try {
|
||||
$user = (new authentication())->get_user();
|
||||
// If there is no user, return an error
|
||||
if (!$user) {
|
||||
(new logs_o())->add('global', 'global', 1, 0, 'AUTHENTICATION_FAILED', 'Authentication failed. Invalid, or missing token');
|
||||
$response->error('Authentication failed. Invalid or missing token.', 401);
|
||||
}
|
||||
if (!$user->hasPermission($permission)) {
|
||||
(new logs_o())->add('global', 'global', 1, $user->id, 'PERMISSION_DENIED', 'Permission denied. Missing permission: ' . $permission);
|
||||
$response->error('Permission denied. Missing permission: ' . $permission . ' for user: ' . $user->id . ' In group: ' . $user->group_id->value(), 403);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$response->error($e->getMessage(), 400);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require parameter to be a positive integer
|
||||
* @param int $value The value to check
|
||||
* @param string|null $parameter The name of the parameter
|
||||
* @return void
|
||||
*/
|
||||
public function requireParameterIntPositive(int $value, string $parameter = null): void
|
||||
{
|
||||
global $response;
|
||||
if ($value <= 0) {
|
||||
$response->error('Parameter ' . $parameter . ' must be a positive integer', 400);
|
||||
}
|
||||
}
|
||||
|
||||
public function requireMinLength(string $parameter, int $length): void
|
||||
{
|
||||
global $response;
|
||||
@@ -204,32 +255,6 @@ trait route_t
|
||||
return $params[$index] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require permission
|
||||
* @param string $permission
|
||||
* @return bool
|
||||
*/
|
||||
public function requirePermission(string $permission): bool
|
||||
{
|
||||
global $response;
|
||||
// Check if the users authorization token has the required permission
|
||||
try {
|
||||
$user = (new authentication())->get_user();
|
||||
// If there is no user, return an error
|
||||
if (!$user) {
|
||||
(new logs_o())->add('global', 'global', 1, 0, 'AUTHENTICATION_FAILED', 'Authentication failed. Invalid, or missing token');
|
||||
$response->error('Authentication failed. Invalid or missing token.', 401);
|
||||
}
|
||||
if (!$user->hasPermission($permission)) {
|
||||
(new logs_o())->add('global', 'global', 1, $user->id, 'PERMISSION_DENIED', 'Permission denied. Missing permission: ' . $permission);
|
||||
$response->error('Permission denied. Missing permission: ' . $permission . ' for user: ' . $user->id . ' In group: ' . $user->group_id->value(), 403);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$response->error($e->getMessage(), 400);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require plate scanner authentication
|
||||
* @return bool
|
||||
|
||||
Reference in New Issue
Block a user