GWM : role based api restiction

This commit is contained in:
Gowtham M 2026-01-08 17:20:13 +05:30
parent e3da05d107
commit 99af6cb97e
2 changed files with 16 additions and 2 deletions

View File

@ -28,7 +28,7 @@ $routes->group('api', ['filter' => 'appSignature'], function ($routes) {
//api's with token
$routes->group('api', ['filter' => ["jwtAuth","appSignature"] ], function ($routes) {
$routes->group('api', ['filter' => ["jwtAuth:1,2,3,agent","appSignature"] ], function ($routes) {
//logout
$routes->get('auth/logout', 'StaffAuthController::logout');

View File

@ -25,9 +25,23 @@ class JwtAuthFilter implements FilterInterface
return service('response')->setJSON(['status' => 401, 'message' => 'Invalid or expired token'])->setStatusCode(401);
}
// ⚙️ Extract date info from token
// Extract date info from token
$userId = $decodedToken['data']->id ?? null;
$lastLogin = $decodedToken['data']->last_login_datetime ?? null;
$role = $decodedToken['data']->role_id ?? 'agent';
// Role-based restriction (if $arguments are passed)
if ($arguments && isset($role)) {
$allowedRoles = array_map('strtolower', array_map('trim', $arguments));
$userRole = strtolower(trim($role));
if (!in_array($userRole, $allowedRoles)) {
return service('response')->setJSON([
'status' => 401,
'message' => 'Access denied. You do not have permission.'
])->setStatusCode(401);
}
}
if ($userId && $lastLogin) {