"use strict";
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); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SecuritySettingsController = void 0;
const api_types_1 = require("@n8n/api-types");
const config_1 = require("@n8n/config");
const decorators_1 = require("@n8n/decorators");
const permissions_1 = require("@n8n/permissions");
const forbidden_error_1 = require("../errors/response-errors/forbidden.error");
const event_service_1 = require("../events/event.service");
const security_settings_service_1 = require("../services/security-settings.service");
let SecuritySettingsController = class SecuritySettingsController {
    constructor(securitySettingsService, eventService, instanceSettingsLoaderConfig) {
        this.securitySettingsService = securitySettingsService;
        this.eventService = eventService;
        this.instanceSettingsLoaderConfig = instanceSettingsLoaderConfig;
    }
    async getSecuritySettings(_req, _res) {
        const [settings, publishedPersonalWorkflowsCount, sharedPersonalWorkflowsCount, sharedPersonalCredentialsCount,] = await Promise.all([
            this.securitySettingsService.arePersonalSpaceSettingsEnabled(),
            this.securitySettingsService.getPublishedPersonalWorkflowsCount(),
            this.securitySettingsService.getSharedPersonalWorkflowsCount(),
            this.securitySettingsService.getSharedPersonalCredentialsCount(),
        ]);
        return {
            ...settings,
            publishedPersonalWorkflowsCount,
            sharedPersonalWorkflowsCount,
            sharedPersonalCredentialsCount,
            managedByEnv: this.instanceSettingsLoaderConfig.securityPolicyManagedByEnv,
        };
    }
    async updateSecuritySettings(req, _res, dto) {
        if (this.instanceSettingsLoaderConfig.securityPolicyManagedByEnv) {
            throw new forbidden_error_1.ForbiddenError('Security settings are managed via environment variables and cannot be modified through the API');
        }
        const updatedSettings = {};
        if (dto.personalSpacePublishing !== undefined) {
            await this.securitySettingsService.setPersonalSpaceSetting(permissions_1.PERSONAL_SPACE_PUBLISHING_SETTING, dto.personalSpacePublishing);
            updatedSettings.personalSpacePublishing = dto.personalSpacePublishing;
            this.eventService.emit('instance-policies-updated', {
                user: {
                    id: req.user.id,
                    email: req.user.email,
                    firstName: req.user.firstName,
                    lastName: req.user.lastName,
                    role: req.user.role,
                },
                settingName: 'workflow_publishing',
                value: dto.personalSpacePublishing,
            });
        }
        if (dto.personalSpaceSharing !== undefined) {
            await this.securitySettingsService.setPersonalSpaceSetting(permissions_1.PERSONAL_SPACE_SHARING_SETTING, dto.personalSpaceSharing);
            updatedSettings.personalSpaceSharing = dto.personalSpaceSharing;
            this.eventService.emit('instance-policies-updated', {
                user: {
                    id: req.user.id,
                    email: req.user.email,
                    firstName: req.user.firstName,
                    lastName: req.user.lastName,
                    role: req.user.role,
                },
                settingName: 'workflow_sharing',
                value: dto.personalSpaceSharing,
            });
        }
        return updatedSettings;
    }
};
exports.SecuritySettingsController = SecuritySettingsController;
__decorate([
    (0, decorators_1.Licensed)('feat:personalSpacePolicy'),
    (0, decorators_1.GlobalScope)('securitySettings:manage'),
    (0, decorators_1.Get)('/'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object]),
    __metadata("design:returntype", Promise)
], SecuritySettingsController.prototype, "getSecuritySettings", null);
__decorate([
    (0, decorators_1.Licensed)('feat:personalSpacePolicy'),
    (0, decorators_1.GlobalScope)('securitySettings:manage'),
    (0, decorators_1.Post)('/'),
    __param(2, decorators_1.Body),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, api_types_1.UpdateSecuritySettingsDto]),
    __metadata("design:returntype", Promise)
], SecuritySettingsController.prototype, "updateSecuritySettings", null);
exports.SecuritySettingsController = SecuritySettingsController = __decorate([
    (0, decorators_1.RestController)('/settings/security'),
    __metadata("design:paramtypes", [security_settings_service_1.SecuritySettingsService,
        event_service_1.EventService,
        config_1.InstanceSettingsLoaderConfig])
], SecuritySettingsController);
//# sourceMappingURL=security-settings.controller.js.map