UNPKG

n8n

Version:

n8n Workflow Automation Tool

208 lines 9.17 kB
"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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CredentialsFinderService = void 0; const db_1 = require("@n8n/db"); const di_1 = require("@n8n/di"); const permissions_1 = require("@n8n/permissions"); const typeorm_1 = require("@n8n/typeorm"); const role_service_1 = require("../services/role.service"); let CredentialsFinderService = class CredentialsFinderService { constructor(sharedCredentialsRepository, credentialsRepository, roleService) { this.sharedCredentialsRepository = sharedCredentialsRepository; this.credentialsRepository = credentialsRepository; this.roleService = roleService; } async fetchGlobalCredentials(trx) { const em = trx ?? this.credentialsRepository.manager; return await em.find(db_1.CredentialsEntity, { where: { isGlobal: true }, relations: { shared: true }, }); } hasGlobalReadOnlyAccess(scopes) { return scopes.length === 1 && scopes[0] === 'credential:read'; } async findGlobalCredentialById(credentialId, relations) { return await this.credentialsRepository.findOne({ where: { id: credentialId, isGlobal: true, }, relations, }); } async findCredentialById(credentialId) { return await this.credentialsRepository.findOne({ where: { id: credentialId } }); } mergeAndDeduplicateCredentials(credentials, globalCredentials, mapGlobalCredential) { const credentialIds = new Set(credentials.map((c) => c.id)); const newGlobalCreds = globalCredentials .filter((gc) => !credentialIds.has(gc.id)) .map(mapGlobalCredential) .filter((mapped) => mapped !== null); return [...credentials, ...newGlobalCreds]; } async findCredentialsForUser(user, scopes) { let where = { isGlobal: false }; if (!(0, permissions_1.hasGlobalScope)(user, scopes, { mode: 'allOf' })) { const [projectRoles, credentialRoles] = await Promise.all([ this.roleService.rolesWithScope('project', scopes), this.roleService.rolesWithScope('credential', scopes), ]); where = { ...where, shared: { role: (0, typeorm_1.In)(credentialRoles), project: { projectRelations: { role: (0, typeorm_1.In)(projectRoles), userId: user.id, }, }, }, }; } const credentials = await this.credentialsRepository.find({ where, relations: { shared: true }, }); if (this.hasGlobalReadOnlyAccess(scopes)) { const globalCredentials = await this.fetchGlobalCredentials(); return [...credentials, ...globalCredentials]; } return credentials; } async findCredentialForUser(credentialsId, user, scopes) { let where = { credentialsId }; if (!(0, permissions_1.hasGlobalScope)(user, scopes, { mode: 'allOf' })) { const [projectRoles, credentialRoles] = await Promise.all([ this.roleService.rolesWithScope('project', scopes), this.roleService.rolesWithScope('credential', scopes), ]); where = { ...where, role: (0, typeorm_1.In)(credentialRoles), project: { projectRelations: { role: (0, typeorm_1.In)(projectRoles), userId: user.id, }, }, }; } const sharedCredential = await this.sharedCredentialsRepository.findOne({ where, relations: { credentials: { shared: { project: true }, }, }, }); if (sharedCredential) { return sharedCredential.credentials; } if (this.hasGlobalReadOnlyAccess(scopes)) { return await this.findGlobalCredentialById(credentialsId, { shared: { project: true }, }); } return null; } async findAllCredentialsForUser(user, scopes, trx, options) { let where = {}; if (!(0, permissions_1.hasGlobalScope)(user, scopes, { mode: 'allOf' })) { const [projectRoles, credentialRoles] = await Promise.all([ this.roleService.rolesWithScope('project', scopes), this.roleService.rolesWithScope('credential', scopes), ]); where = { role: (0, typeorm_1.In)(credentialRoles), project: { projectRelations: { role: (0, typeorm_1.In)(projectRoles), userId: user.id, }, }, }; } const sharedCredential = await this.sharedCredentialsRepository.findCredentialsWithOptions(where, trx); let sharedCredentialsList = sharedCredential.map((sc) => ({ ...sc.credentials, projectId: sc.projectId, })); if (options?.includeGlobalCredentials) { const globalCredentials = await this.fetchGlobalCredentials(trx); sharedCredentialsList = this.mergeAndDeduplicateCredentials(sharedCredentialsList, globalCredentials, (globalCred) => { const ownerSharing = globalCred.shared?.find((s) => s.role === 'credential:owner'); const projectId = ownerSharing?.projectId; if (projectId) { return { ...globalCred, projectId }; } return null; }); } return sharedCredentialsList; } async findCredentialIdsWithScopeForUser(credentialIds, user, scopes) { if (credentialIds.length === 0) return new Set(); let where = { credentialsId: (0, typeorm_1.In)(credentialIds) }; if (!(0, permissions_1.hasGlobalScope)(user, scopes, { mode: 'allOf' })) { const [projectRoles, credentialRoles] = await Promise.all([ this.roleService.rolesWithScope('project', scopes), this.roleService.rolesWithScope('credential', scopes), ]); where = { ...where, role: (0, typeorm_1.In)(credentialRoles), project: { projectRelations: { role: (0, typeorm_1.In)(projectRoles), userId: user.id, }, }, }; } const sharedCredentials = await this.sharedCredentialsRepository.find({ select: { credentialsId: true }, where, }); const result = new Set(sharedCredentials.map((sc) => sc.credentialsId)); if (this.hasGlobalReadOnlyAccess(scopes)) { const globalCreds = await this.credentialsRepository.find({ where: { id: (0, typeorm_1.In)(credentialIds), isGlobal: true }, select: ['id'], }); for (const gc of globalCreds) result.add(gc.id); } return result; } async getCredentialIdsByUserAndRole(userIds, options, trx) { const projectRoles = 'scopes' in options ? await this.roleService.rolesWithScope('project', options.scopes) : options.projectRoles; const credentialRoles = 'scopes' in options ? await this.roleService.rolesWithScope('credential', options.scopes) : options.credentialRoles; const sharings = await this.sharedCredentialsRepository.findCredentialsByRoles(userIds, projectRoles, credentialRoles, trx); return sharings.map((s) => s.credentialsId); } }; exports.CredentialsFinderService = CredentialsFinderService; exports.CredentialsFinderService = CredentialsFinderService = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [db_1.SharedCredentialsRepository, db_1.CredentialsRepository, role_service_1.RoleService]) ], CredentialsFinderService); //# sourceMappingURL=credentials-finder.service.js.map