Specific permission required to access the endpoint
Express middleware function that validates user permissions
// Require specific permission for content creation
router.post('/content',
authenticateToken,
requirePermission('create:content'),
contentController.create
);
// Separate read and write permissions
router.get('/analytics',
authenticateToken,
requirePermission('read:analytics'),
analyticsController.getAnalytics
);
Permission-Based Authorization Middleware Factory
Creates middleware that enforces granular permission-based access control by validating that the authenticated user's role includes the required permission. This provides more fine-grained authorization than role-based access control alone.
The middleware checks the user's role against the PERMISSIONS mapping to determine if the specific permission is granted. This allows for precise control over individual actions while maintaining the role-based hierarchy.
This middleware is ideal for scenarios where different aspects of functionality need different access levels, such as separating read and write permissions or controlling access to specific features within a role.