UNPKG

@merncloud/permission-resolver

Version:

Reusable RBAC + ABAC permission resolver for Node.js

30 lines (29 loc) 1.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PermissionResolver = void 0; const evaluateCondition_1 = require("./evaluateCondition"); class PermissionResolver { constructor(rules = []) { this.rules = rules; } addRule(rule) { this.rules.push(rule); } getRules() { return this.rules; } can(subject, resource, action) { const matchingRules = this.rules.filter((rule) => rule.resource === resource.type && rule.action === action); for (const rule of matchingRules) { const roleAllowed = !rule.allowedRoles || rule.allowedRoles.some((role) => subject.roles.includes(role)); const attributesAllowed = !rule.attributeCondition || (0, evaluateCondition_1.evaluateCondition)(rule.attributeCondition, subject, resource); if (roleAllowed && attributesAllowed) { return true; } } return false; } } exports.PermissionResolver = PermissionResolver;