games_app/backend/dist-test-faq/rbac/permissionResolver.js

69 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
/**
* Resolves whether a user holds a required permission, including legacy coarse-key aliases.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.userHasPermission = userHasPermission;
exports.userHasAllPermissions = userHasAllPermissions;
exports.userHasAnyPermission = userHasAnyPermission;
exports.mergePermissionSets = mergePermissionSets;
const permissionCatalog_1 = require("./permissionCatalog");
/** Map legacy coarse key → granular keys it implies. */
const LEGACY_IMPLIES = new Map();
for (const def of permissionCatalog_1.PERMISSION_CATALOG) {
if (!def.legacyGrants?.length)
continue;
for (const legacy of def.legacyGrants) {
if (!LEGACY_IMPLIES.has(legacy))
LEGACY_IMPLIES.set(legacy, new Set());
LEGACY_IMPLIES.get(legacy).add(def.key);
}
// Also: owning the granular key satisfies checks against legacy coarse keys when all granules covered
}
function expandPermissions(userPerms) {
const expanded = new Set(userPerms);
for (const p of userPerms) {
const implied = LEGACY_IMPLIES.get(p);
if (implied)
implied.forEach((k) => expanded.add(k));
}
return expanded;
}
/** True if [userPerms] satisfies [required] (direct match or legacy alias). */
function userHasPermission(userPerms, required) {
if (userPerms.includes(required))
return true;
const expanded = expandPermissions(userPerms);
if (expanded.has(required))
return true;
// User has granular keys that together satisfy a legacy coarse requirement
const impliedByLegacy = LEGACY_IMPLIES.get(required);
if (impliedByLegacy) {
for (const granular of impliedByLegacy) {
if (userPerms.includes(granular) || expanded.has(granular))
return true;
}
}
return false;
}
/** True if user has every listed permission. */
function userHasAllPermissions(userPerms, required) {
return required.every((p) => userHasPermission(userPerms, p));
}
/** True if user has at least one listed permission. */
function userHasAnyPermission(userPerms, required) {
return required.some((p) => userHasPermission(userPerms, p));
}
/**
* Effective permissions = role grants ∪ individual allows − individual denies.
* Deny always wins over inherited and direct allow.
*/
function mergePermissionSets(inherited, directAllow, directDeny) {
const set = new Set(inherited);
for (const key of directAllow)
set.add(key);
for (const key of directDeny)
set.delete(key);
return [...set].sort();
}
//# sourceMappingURL=permissionResolver.js.map