Implement route-level permission handling.
Added support for defining and registering permissions for API routes. Updated methods across the router and route traits to accommodate permission details, enabling better access control. Enhanced department license plate lookup with session validation and additional endpoints.
This commit is contained in:
@@ -8,6 +8,7 @@ use objects\logs_o;
|
||||
|
||||
trait route_t
|
||||
{
|
||||
protected array $permissions = [];
|
||||
private string $route;
|
||||
|
||||
public function __construct()
|
||||
@@ -103,61 +104,92 @@ trait route_t
|
||||
/**
|
||||
* GET route
|
||||
* @param string $route Example: /home, /home/{id}
|
||||
* @param callable $callback
|
||||
* @param array $permissions
|
||||
* @return void
|
||||
*/
|
||||
public function get(string $route, callable $callback): void
|
||||
public function get(string $route, callable $callback, array $permissions = []): void
|
||||
{
|
||||
$this->registerRoute($route, 'GET', $callback);
|
||||
$this->registerRoute($route, 'GET', $callback, $permissions);
|
||||
}
|
||||
|
||||
private function registerRoute($route, $method, $callback): void
|
||||
private function registerRoute($route, $method, $callback, $permissions = []): void
|
||||
{
|
||||
global $router;
|
||||
$router->add($route, $method, $callback);
|
||||
$router->add($route, $method, $callback, self::registerPermissions($permissions, $route, $method));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register permissions for the route
|
||||
* @param array $permissions The permissions to register
|
||||
* @param null $endpoint
|
||||
* @param null $method
|
||||
* @return array
|
||||
*/
|
||||
public function registerPermissions(array $permissions, $endpoint = null, $method = null): array
|
||||
{
|
||||
foreach ( $permissions as $permission => $description ) {
|
||||
$this->registerPermission($permission, $description, $endpoint, $method);
|
||||
}
|
||||
return $this->permissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a permission
|
||||
* @param string $permission The permission to register (Example: 'modules_motorapi_lookup')
|
||||
* @param string $description The description of the permission (Example: 'Lookup license plate information')
|
||||
* @param null $endpoint The endpoint of the permission (Example: '/department/license-plate/lookup')
|
||||
* @param null $method The method of the permission (Example: 'GET')
|
||||
* @return void
|
||||
*/
|
||||
public function registerPermission(string $permission, string $description = 'No description provided.', $endpoint = null, $method = null): void
|
||||
{
|
||||
$this->permissions[$endpoint ?? $this->route][$method ?? 'UNKNOWN_METHOD'][$permission] = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* POST route
|
||||
* @param string $route Example: /home, /home/{id}
|
||||
*/
|
||||
public function post(string $route, callable $callback): void
|
||||
public function post(string $route, callable $callback, array $permissions = []): void
|
||||
{
|
||||
$this->registerRoute($route, 'POST', $callback);
|
||||
$this->registerRoute($route, 'POST', $callback, $permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT route
|
||||
* @param string $route Example: /home, /home/{id}
|
||||
*/
|
||||
public function put(string $route, callable $callback): void
|
||||
public function put(string $route, callable $callback, array $permissions = []): void
|
||||
{
|
||||
$this->registerRoute($route, 'PUT', $callback);
|
||||
$this->registerRoute($route, 'PUT', $callback, $permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE route
|
||||
* @param string $route Example: /home, /home/{id}
|
||||
*/
|
||||
public function delete(string $route, callable $callback): void
|
||||
public function delete(string $route, callable $callback, array $permissions = []): void
|
||||
{
|
||||
$this->registerRoute($route, 'DELETE', $callback);
|
||||
$this->registerRoute($route, 'DELETE', $callback, $permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* OPTIONS route
|
||||
* @param string $route Example: /home, /home/{id}
|
||||
*/
|
||||
public function options(string $route, callable $callback): void
|
||||
public function options(string $route, callable $callback, array $permissions = []): void
|
||||
{
|
||||
$this->registerRoute($route, 'OPTIONS', $callback);
|
||||
$this->registerRoute($route, 'OPTIONS', $callback, $permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH route
|
||||
* @param string $route Example: /home, /home/{id}
|
||||
*/
|
||||
public function patch(string $route, callable $callback): void
|
||||
public function patch(string $route, callable $callback, array $permissions = []): void
|
||||
{
|
||||
$this->registerRoute($route, 'PATCH', $callback);
|
||||
$this->registerRoute($route, 'PATCH', $callback, $permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user