@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
76 lines (73 loc) • 3.5 kB
JavaScript
;
var tslib_es6 = require('../../../../../external/tslib/tslib.es6.js');
var cache_class = require('./cache.class.js');
var attachmentSourcesToken_constant = require('../../../../../constant/class/authorization/policy/attachment-sources-token.constant.js');
var common = require('@nestjs/common');
var logger_utility = require('../../../../../utility/logger.utility.js');
const iamAttachmentResolverLogger = logger_utility.LoggerUtility.getLogger("ApiAuthorizationIamAttachmentResolver");
exports.ApiAuthorizationIamAttachmentResolver = class ApiAuthorizationIamAttachmentResolver {
cache;
sources;
constructor(cache, sources = []) {
this.cache = cache;
this.sources = sources;
}
clear() {
this.cache.clear();
}
async resolve(principal) {
const cachedAttachments = this.cache.get(principal);
if (cachedAttachments) {
iamAttachmentResolverLogger.verbose(`Using cached IAM attachments for principal "${principal.id}" (${cachedAttachments.attachments.length} attachments, ${cachedAttachments.boundaries.length} boundaries).`);
return cachedAttachments;
}
const attachments = new Map();
const boundaries = new Map();
for (const source of this.sources) {
const resolvedAttachments = await source.getAttachments(principal);
for (const attachment of resolvedAttachments.attachments) {
attachments.set(this.createAttachmentKey(attachment), attachment);
}
for (const boundary of resolvedAttachments.boundaries) {
boundaries.set(this.createAttachmentKey(boundary), boundary);
}
}
const result = {
attachments: [...attachments.values()],
boundaries: [...boundaries.values()],
};
this.cache.set(principal, result);
iamAttachmentResolverLogger.verbose(`Resolved ${result.attachments.length} IAM attachments and ${result.boundaries.length} boundaries for principal "${principal.id}" from ${this.sources.length} sources.`);
return result;
}
createAttachmentKey(attachment) {
return JSON.stringify({
metadata: this.normalizeAttachmentValue(attachment.metadata ?? {}),
policyId: attachment.policyId,
principalId: attachment.principalId,
principalType: attachment.principalType,
});
}
normalizeAttachmentValue(value) {
if (Array.isArray(value)) {
return value.map((item) => this.normalizeAttachmentValue(item));
}
if (!value || typeof value !== "object") {
return value;
}
const normalizedValue = {};
const recordValue = value;
const sortedKeys = Object.keys(recordValue).toSorted((left, right) => left.localeCompare(right));
for (const key of sortedKeys) {
normalizedValue[key] = this.normalizeAttachmentValue(recordValue[key]);
}
return normalizedValue;
}
};
exports.ApiAuthorizationIamAttachmentResolver = tslib_es6.__decorate([
common.Injectable(),
tslib_es6.__param(1, common.Inject(attachmentSourcesToken_constant.AUTHORIZATION_POLICY_ATTACHMENT_SOURCES_TOKEN)),
tslib_es6.__param(1, common.Optional()),
tslib_es6.__metadata("design:paramtypes", [cache_class.ApiAuthorizationIamAttachmentCache, Array])
], exports.ApiAuthorizationIamAttachmentResolver);
//# sourceMappingURL=resolver.class.js.map