"use strict";
var AccessRightsNodeService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessRightsNodeService = void 0;
const tslib_1 = require("tslib");
const authorization_1 = require("@loopback/authorization");
const core_1 = require("@loopback/core");
const lodash_1 = require("lodash");
const entity_name_1 = require("../../models/entity-name");
const object_node_service_1 = require("../object-node/object-node.service");
const access_rights_abstract_service_1 = require("./access-rights-abstract.service");
const access_rights_tree_const_1 = require("./access-rights-tree.const");
const access_rights_const_1 = require("./access-rights.const");
const access_rights_service_1 = require("./access-rights.service");
let AccessRightsNodeService = AccessRightsNodeService_1 = class AccessRightsNodeService extends access_rights_abstract_service_1.AccessRightsAbstractService {
    constructor(accessRightsService, objectNodeService) {
        super(access_rights_const_1.ACCESS_RIGHT_PROVIDER, AccessRightsNodeService_1.name, accessRightsService, entity_name_1.EntityName.objectNode);
        this.accessRightsService = accessRightsService;
        this.objectNodeService = objectNodeService;
    }
    async authorizeRead(ctx, context) {
        let node = null;
        if (1 < context.scopes.length) {
            switch (context.scopes[1]) {
                case access_rights_tree_const_1.AccessRightsTreeScope.searchOwner:
                    node = await this.objectNodeService.getOwner(context.invocationContext.args[0], context.invocationContext.args[1], ctx);
                    break;
                case access_rights_tree_const_1.AccessRightsTreeScope.searchNamespace:
                    node = await this.objectNodeService.getNamespace(context.invocationContext.args[0], context.invocationContext.args[1], context.invocationContext.args[2], context.invocationContext.args[3], ctx);
                    break;
                case access_rights_tree_const_1.AccessRightsTreeScope.searchTree:
                    node = await this.objectNodeService.getTree(context.invocationContext.args[0], context.invocationContext.args[1], context.invocationContext.args[2], context.invocationContext.args[3], context.invocationContext.args[4], context.invocationContext.args[5], ctx);
                    break;
                case access_rights_tree_const_1.AccessRightsTreeScope.searchNode:
                    node = await this.objectNodeService.getANodeOfTree(context.invocationContext.args[0], context.invocationContext.args[1], context.invocationContext.args[2], context.invocationContext.args[3], context.invocationContext.args[4], context.invocationContext.args[5], context.invocationContext.args[6], context.invocationContext.args[7], ctx);
                    break;
            }
        }
        else {
            node = await this.objectNodeService.getNode(context.invocationContext.args[0], ctx);
        }
        if (node) {
            return (await this.accessRightsService.hasNodeAccessRights(access_rights_const_1.AccessRightsScope.read, node, ctx))
                ? authorization_1.AuthorizationDecision.ALLOW
                : authorization_1.AuthorizationDecision.DENY;
        }
        return authorization_1.AuthorizationDecision.DENY;
    }
    async authorizeCreate(ctx, context) {
        let node = null;
        if (lodash_1.isString(context.invocationContext.args[0])) {
            node = await this.objectNodeService.getNode(context.invocationContext.args[0], ctx);
        }
        else {
            node = await ctx.nodeContext.parent.getOrSetValue(async () => {
                var _a;
                return this.objectNodeService.searchById((_a = context.invocationContext.args[0]) === null || _a === void 0 ? void 0 : _a.parentNodeId);
            });
        }
        if (node) {
            return (await this.accessRightsService.hasNodeAccessRights(access_rights_const_1.AccessRightsScope.create, node, ctx))
                ? authorization_1.AuthorizationDecision.ALLOW
                : authorization_1.AuthorizationDecision.DENY;
        }
        return authorization_1.AuthorizationDecision.DENY;
    }
    async authorizeUpdate(ctx, context) {
        const node = await this.objectNodeService.getNode(context.invocationContext.args[0], ctx);
        if (node) {
            return (await this.accessRightsService.hasNodeAccessRights(access_rights_const_1.AccessRightsScope.update, node, ctx))
                ? authorization_1.AuthorizationDecision.ALLOW
                : authorization_1.AuthorizationDecision.DENY;
        }
        return authorization_1.AuthorizationDecision.DENY;
    }
    async authorizeDelete(ctx, context) {
        const node = await this.objectNodeService.getNode(context.invocationContext.args[0], ctx);
        if (node) {
            return (await this.accessRightsService.hasNodeAccessRights(access_rights_const_1.AccessRightsScope.delete, node, ctx))
                ? authorization_1.AuthorizationDecision.ALLOW
                : authorization_1.AuthorizationDecision.DENY;
        }
        return authorization_1.AuthorizationDecision.DENY;
    }
    async cleanReturnedEntity(entity, ctx) {
        const objectNode = entity;
        if (!objectNode.entityCtx) {
            objectNode.entityCtx = { entityType: entity_name_1.EntityName.objectNode };
        }
        objectNode.entityCtx.aclCtx = {
            rights: await this.accessRightsService.getNodeAccessRights(objectNode, ctx),
        };
    }
};
AccessRightsNodeService = AccessRightsNodeService_1 = tslib_1.__decorate([
    core_1.injectable({ scope: core_1.BindingScope.SINGLETON }),
    tslib_1.__param(0, core_1.service(access_rights_service_1.AccessRightsService)),
    tslib_1.__param(1, core_1.service(object_node_service_1.ObjectNodeService)),
    tslib_1.__metadata("design:paramtypes", [access_rights_service_1.AccessRightsService,
        object_node_service_1.ObjectNodeService])
], AccessRightsNodeService);
exports.AccessRightsNodeService = AccessRightsNodeService;
//# sourceMappingURL=access-rights-node.service.js.map