Add notifications module with routes, types, and helpers

Introduced a complete notifications module, including classes for managing notifications (`notifications_o`), traits for handling types and routing, and API routes to list, add, and delete notifications. Added input validation, permission handling, and JSON data processing capabilities.
This commit is contained in:
Jepp9350
2025-03-10 13:11:44 +01:00
parent fdb536ea3e
commit 48ad281baa
6 changed files with 519 additions and 1 deletions
+41
View File
@@ -31,6 +31,15 @@ trait route_t
{
// Get the type of the value
$value_type = gettype($value);
// If the type is Array, or Object, json_decode the value to check if it is a valid JSON
if ($type === 'array' || $type === 'object') {
$value = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
global $response;
$response->error('Invalid JSON. Value: ' . $value, 400);
}
$value_type = gettype($value);
}
// Check if the value is of the required type
if ($value_type !== $type) {
global $response;
@@ -57,6 +66,16 @@ trait route_t
return true;
}
/**
* TYPE_ARRAY
* @note This is used to check if the value is an array, this is used in routes to validate input
* @return string
*/
public function TYPE_ARRAY(): string
{
return 'array';
}
/**
* TYPE_NULL
* @note This is used to check if the value is null, this is used in routes to validate input
@@ -156,6 +175,28 @@ trait route_t
return true;
}
/**
* Check if the user has a permission
* @param string $permission
* @return bool
*/
public function hasPermission(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);
}
return $user->hasPermission($permission);
} catch (\Exception $e) {
$response->error($e->getMessage(), 400);
}
}
/**
* Require parameter to be a positive integer
* @param int $value The value to check