@minimaltech/node-infra
Version:
Minimal Technology NodeJS Infrastructure - Loopback 4 Framework
99 lines • 5.37 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var EnforcerService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnforcerService = void 0;
const datasources_1 = require("../../../base/datasources");
const helpers_1 = require("../../../helpers");
const utilities_1 = require("../../../utilities");
const core_1 = require("@loopback/core");
const casbin_1 = require("casbin");
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
const node_fs_1 = __importDefault(require("node:fs"));
const adapters_1 = require("../adapters");
const common_1 = require("../common");
let EnforcerService = EnforcerService_1 = class EnforcerService {
constructor(options, dataSource) {
this.options = options;
this.dataSource = dataSource;
this.logger = helpers_1.LoggerFactory.getLogger([EnforcerService_1.name]);
this.logger.info('[getEnforcer] Initialize enforcer with options: %j', this.options);
}
getEnforcer() {
if (this.enforcer) {
return Promise.resolve(this.enforcer);
}
this.logger.debug('[getEnforcer] Enforcer Options: %j', this.options);
const { confPath, adapterType = common_1.CasbinAdapterTypes.POSTGRES, adapter, useCache } = this.options;
if (!confPath || (0, isEmpty_1.default)(confPath)) {
this.logger.error('[getEnforcer] Invalid configure path | confPath: %s', confPath);
throw (0, utilities_1.getError)({
statusCode: 500,
message: `[getEnforcer] Invalid enforcer configuration path | confPath: ${confPath}`,
});
}
if (!node_fs_1.default.existsSync(confPath)) {
this.logger.error('[getEnforcer] Please check again configure path | confPath: %s', confPath);
throw (0, utilities_1.getError)({
statusCode: 500,
message: `[getEnforcer] Enforcer configuration path is not existed | confPath: ${confPath}`,
});
}
this.logger.info('[getEnforcer] Creating new Enforcer with configure path: %s | dataSource: %s', confPath, this.dataSource.name);
const casbinAdapter = adapter !== null && adapter !== void 0 ? adapter : adapters_1.CasbinAdapterBuilder.getInstance().build({
type: adapterType,
dataSource: this.dataSource,
});
if (useCache) {
return (0, casbin_1.newCachedEnforcer)(confPath, casbinAdapter);
}
this.logger.debug('[getEnforcer] Created new enforcer | Configure path: %s', confPath);
return (0, casbin_1.newEnforcer)(confPath, casbinAdapter);
}
// -----------------------------------------------------------------------------------------
getTypeEnforcer(id) {
return __awaiter(this, void 0, void 0, function* () {
const enforcer = yield this.getEnforcer();
if (!enforcer) {
return null;
}
const filterValue = {
principalType: 'User',
principalValue: id,
};
yield enforcer.loadFilteredPolicy(filterValue);
return enforcer;
});
}
};
exports.EnforcerService = EnforcerService;
exports.EnforcerService = EnforcerService = EnforcerService_1 = __decorate([
(0, core_1.injectable)({ scope: core_1.BindingScope.SINGLETON }),
__param(0, (0, core_1.inject)(common_1.AuthorizerKeys.CONFIGURE_OPTIONS)),
__param(1, (0, core_1.inject)(common_1.AuthorizerKeys.AUTHORIZE_DATASOURCE)),
__metadata("design:paramtypes", [Object, datasources_1.BaseDataSource])
], EnforcerService);
//# sourceMappingURL=enforcer.service.js.map