Use the RBAC service directly in your code when running SuperBackend in middleware mode.
const rbac = globalThis.superbackend.services.rbac;
const result = await rbac.checkRight(userId, orgId, 'backoffice:dashboard:access');
if (result.allowed) {
// User has permission - proceed
console.log('Access granted');
} else {
// Permission denied
console.log('Access denied by:', result.decisionLayer);
// result.decisionLayer can be: 'user', 'role', 'group', 'org', or 'global'
}
const requireRight = (right) => async (req, res, next) => {
const rbac = globalThis.superbackend.services.rbac;
const result = await rbac.checkRight(req.user.id, req.org.id, right);
if (result.allowed) return next();
res.status(403).json({ error: 'Insufficient permissions' });
};
// Usage
app.get('/admin/dashboard',
authenticate, // your auth middleware
requireRight('backoffice:dashboard:access'),
dashboardHandler
);
Check rights via REST API from external services or frontend applications.
POST /api/rbac/check
Content-Type: application/json
Authorization: Bearer <JWT> or Basic Auth
{
"userId": "507f1f77bcf86cd799439011",
"orgId": "507f1f77bcf86cd799439012",
"right": "backoffice:dashboard:access"
}
{
"allowed": true,
"decisionLayer": "role",
"context": {
"userRoles": ["admin", "editor"],
"groupRoles": ["viewer"],
"userGrants": [],
"roleGrants": [
{ "right": "backoffice:dashboard:access", "effect": "allow" }
],
"groupGrants": [],
"orgGrants": [],
"globalGrants": []
}
}
curl -X POST http://localhost:3000/api/rbac/check \
-H "Authorization: Bearer YOUR_JWT" \
-H "Content-Type: application/json" \
-d '{
"userId": "507f1f77bcf86cd799439011",
"orgId": "507f1f77bcf86cd799439012",
"right": "backoffice:dashboard:access"
}'
const response = await fetch('/api/rbac/check', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({
userId: '507f1f77bcf86cd799439011',
orgId: '507f1f77bcf86cd799439012',
right: 'backoffice:dashboard:access'
})
});
const result = await response.json();
if (result.allowed) {
// Proceed with action
}
// Check for wildcard rights
const result = await rbac.checkRight(userId, orgId, 'backoffice:*');
// Matches: backoffice:dashboard:access, backoffice:users:read, etc.
// Check multiple rights (AND logic)
const rights = ['backoffice:dashboard:access', 'backoffice:users:read'];
const results = await Promise.all(
rights.map(right => rbac.checkRight(userId, orgId, right))
);
const hasAll = results.every(r => r.allowed);
const hasAny = results.some(r => r.allowed);
// Global rights (orgId can be null/undefined)
await rbac.checkRight(userId, null, 'system:admin');
// Org-specific rights
await rbac.checkRight(userId, orgId, 'org:settings:edit');
domain:action:resource
Tip: Use the decisionLayer property to understand why access was granted/denied. This is useful for audit trails and debugging.