UNPKG

casbin

Version:

An authorization library that supports access control models like ACL, RBAC, ABAC in Node.JS

179 lines (178 loc) 7.37 kB
"use strict"; // Copyright 2018 The Casbin Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InternalEnforcer = void 0; const coreEnforcer_1 = require("./coreEnforcer"); const model_1 = require("./model"); /** * InternalEnforcer = CoreEnforcer + Internal API. */ class InternalEnforcer extends coreEnforcer_1.CoreEnforcer { /** * addPolicyInternal adds a rule to the current policy. */ addPolicyInternal(sec, ptype, rule) { return __awaiter(this, void 0, void 0, function* () { if (this.model.hasPolicy(sec, ptype, rule)) { return false; } if (this.adapter && this.autoSave) { try { yield this.adapter.addPolicy(sec, ptype, rule); } catch (e) { if (e.message !== 'not implemented') { throw e; } } } if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored this.watcher.update(); } const ok = this.model.addPolicy(sec, ptype, rule); if (sec === 'g' && ok) { yield this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, [rule]); } return ok; }); } // addPolicies adds rules to the current policy. // removePolicies removes rules from the current policy. addPoliciesInternal(sec, ptype, rules) { return __awaiter(this, void 0, void 0, function* () { for (const rule of rules) { if (this.model.hasPolicy(sec, ptype, rule)) { return false; } } const batchAdapter = this.adapter; if (batchAdapter && this.autoSave) { try { yield batchAdapter.addPolicies(sec, ptype, rules); } catch (e) { if (e.message !== 'not implemented') { throw e; } } } if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored this.watcher.update(); } const [ok, effects] = yield this.model.addPolicies(sec, ptype, rules); if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) { yield this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyAdd, ptype, effects); } return ok; }); } /** * removePolicyInternal removes a rule from the current policy. */ removePolicyInternal(sec, ptype, rule) { return __awaiter(this, void 0, void 0, function* () { if (!this.model.hasPolicy(sec, ptype, rule)) { return false; } if (this.adapter && this.autoSave) { try { yield this.adapter.removePolicy(sec, ptype, rule); } catch (e) { if (e.message !== 'not implemented') { throw e; } } } if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored this.watcher.update(); } const ok = yield this.model.removePolicy(sec, ptype, rule); if (sec === 'g' && ok) { yield this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, [rule]); } return ok; }); } // removePolicies removes rules from the current policy. removePoliciesInternal(sec, ptype, rules) { return __awaiter(this, void 0, void 0, function* () { for (const rule of rules) { if (!this.model.hasPolicy(sec, ptype, rule)) { return false; } } const batchAdapter = this.adapter; if (batchAdapter && this.autoSave) { try { yield batchAdapter.removePolicies(sec, ptype, rules); } catch (e) { if (e.message !== 'not implemented') { throw e; } } } if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored this.watcher.update(); } const [ok, effects] = this.model.removePolicies(sec, ptype, rules); if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) { yield this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, effects); } return ok; }); } /** * removeFilteredPolicyInternal removes rules based on field filters from the current policy. */ removeFilteredPolicyInternal(sec, ptype, fieldIndex, fieldValues) { return __awaiter(this, void 0, void 0, function* () { if (this.adapter && this.autoSave) { try { yield this.adapter.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); } catch (e) { if (e.message !== 'not implemented') { throw e; } } } if (this.watcher && this.autoNotifyWatcher) { // error intentionally ignored this.watcher.update(); } const [ok, effects] = this.model.removeFilteredPolicy(sec, ptype, fieldIndex, ...fieldValues); if (sec === 'g' && ok && (effects === null || effects === void 0 ? void 0 : effects.length)) { yield this.buildIncrementalRoleLinks(model_1.PolicyOp.PolicyRemove, ptype, effects); } return ok; }); } } exports.InternalEnforcer = InternalEnforcer;