Fix jwt-token

This commit is contained in:
2025-09-15 06:38:23 +02:00
parent 60f8867cd2
commit 2851a2e9c8
4 changed files with 71 additions and 2 deletions

View File

@@ -117,6 +117,44 @@ const hasPermission = (userRole, permission) => {
return ROLES[userRole].includes(permission);
};
/**
* Compatibility function for tests - converts resource.action format to permission
* @param {string} userRole - The user's role
* @param {string} resource - The resource (e.g., 'devices', 'users')
* @param {string} action - The action (e.g., 'read', 'create', 'update', 'delete')
* @returns {boolean} - True if user has permission
*/
const checkPermission = (userRole, resource, action) => {
// Map common actions to our permission system
const actionMap = {
'read': 'view',
'create': 'create',
'update': 'edit',
'delete': 'delete',
'manage': 'manage'
};
// Special cases for resource mapping
const resourceMap = {
'devices': 'devices',
'users': 'users',
'detections': 'detections',
'alerts': 'alerts',
'dashboard': 'dashboard',
'branding': 'branding',
'security': 'security',
'ip_restrictions': 'security',
'audit_logs': 'security',
'ui_customization': 'branding'
};
const mappedResource = resourceMap[resource] || resource;
const mappedAction = actionMap[action] || action;
const permission = `${mappedResource}.${mappedAction}`;
return hasPermission(userRole, permission);
};
/**
* Check if a user has any of the specified permissions
* @param {string} userRole - The user's role
@@ -222,6 +260,7 @@ module.exports = {
PERMISSIONS,
ROLES,
hasPermission,
checkPermission,
hasAnyPermission,
hasAllPermissions,
getPermissions,