"use strict";
var AccessRightsTypeService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessRightsTypeService = void 0;
const tslib_1 = require("tslib");
const authorization_1 = require("@loopback/authorization");
const core_1 = require("@loopback/core");
const acl_ctx_model_1 = require("./../../models/acl-ctx.model");
const entity_name_1 = require("./../../models/entity-name");
const object_type_service_1 = require("./../object-type.service");
const access_rights_abstract_service_1 = require("./access-rights-abstract.service");
const access_rights_const_1 = require("./access-rights.const");
const access_rights_service_1 = require("./access-rights.service");
let AccessRightsTypeService = AccessRightsTypeService_1 = class AccessRightsTypeService extends access_rights_abstract_service_1.AccessRightsAbstractService {
    constructor(accessRightsService, objectTypeService) {
        super(access_rights_const_1.ACCESS_RIGHT_PROVIDER, AccessRightsTypeService_1.name, accessRightsService, entity_name_1.EntityName.objectType);
        this.accessRightsService = accessRightsService;
        this.objectTypeService = objectTypeService;
    }
    async cleanReturnedEntity(entity, ctx) {
        const user = entity;
        if (!user.entityCtx) {
            user.entityCtx = { entityType: entity_name_1.EntityName.objectType };
        }
        if (!user.entityCtx.aclCtx) {
            user.entityCtx.aclCtx = new acl_ctx_model_1.AclCtx();
        }
        user.entityCtx.aclCtx.rights.read = true;
        // TODO: check admin and registered objects during boot for CRUD
        user.entityCtx.aclCtx.rights.create =
            ctx.accessRightsContext.rootRights.value.create;
        user.entityCtx.aclCtx.rights.update =
            !user.applicationType && ctx.accessRightsContext.rootRights.value.update;
        user.entityCtx.aclCtx.rights.delete =
            !user.applicationType && ctx.accessRightsContext.rootRights.value.delete;
        // TODO: remove subTypes for non admin;
    }
    async authorizeRead(ctx, context) {
        return authorization_1.AuthorizationDecision.ALLOW;
    }
    async authorizeCreate(ctx, context) {
        return ctx.accessRightsContext.rootRights.value.create
            ? authorization_1.AuthorizationDecision.ALLOW
            : authorization_1.AuthorizationDecision.DENY;
    }
    async authorizeUpdate(ctx, context) {
        return ctx.accessRightsContext.rootRights.value.update &&
            !(await this.objectTypeService.searchById(context.invocationContext.args[0])).applicationType
            ? authorization_1.AuthorizationDecision.ALLOW
            : authorization_1.AuthorizationDecision.DENY;
    }
    async authorizeDelete(ctx, context) {
        return ctx.accessRightsContext.rootRights.value.delete &&
            !(await this.objectTypeService.searchById(context.invocationContext.args[0])).applicationType
            ? authorization_1.AuthorizationDecision.ALLOW
            : authorization_1.AuthorizationDecision.DENY;
    }
};
AccessRightsTypeService = AccessRightsTypeService_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_type_service_1.ObjectTypeService)),
    tslib_1.__metadata("design:paramtypes", [access_rights_service_1.AccessRightsService,
        object_type_service_1.ObjectTypeService])
], AccessRightsTypeService);
exports.AccessRightsTypeService = AccessRightsTypeService;
//# sourceMappingURL=access-rights-type.service.js.map