UNPKG

n8n

Version:

n8n Workflow Automation Tool

108 lines 5.31 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 di_1 = require("@n8n/di"); const typeorm_1 = require("@n8n/typeorm"); const credentials_repository_1 = require("../databases/repositories/credentials.repository"); const shared_credentials_repository_1 = require("../databases/repositories/shared-credentials.repository"); const role_service_1 = require("../services/role.service"); let CredentialsFinderService = class CredentialsFinderService { constructor(sharedCredentialsRepository, roleService, credentialsRepository) { this.sharedCredentialsRepository = sharedCredentialsRepository; this.roleService = roleService; this.credentialsRepository = credentialsRepository; } async findCredentialsForUser(user, scopes) { let where = {}; if (!user.hasGlobalScope(scopes, { mode: 'allOf' })) { const projectRoles = this.roleService.rolesWithScope('project', scopes); const credentialRoles = 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, }, }, }, }; } return await this.credentialsRepository.find({ where, relations: { shared: true } }); } async findCredentialForUser(credentialsId, user, scopes) { let where = { credentialsId }; if (!user.hasGlobalScope(scopes, { mode: 'allOf' })) { const projectRoles = this.roleService.rolesWithScope('project', scopes); const credentialRoles = 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: { projectRelations: { user: true } } }, }, }, }); if (!sharedCredential) return null; return sharedCredential.credentials; } async findAllCredentialsForUser(user, scopes, trx) { let where = {}; if (!user.hasGlobalScope(scopes, { mode: 'allOf' })) { const projectRoles = this.roleService.rolesWithScope('project', scopes); const credentialRoles = 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); return sharedCredential.map((sc) => ({ ...sc.credentials, projectId: sc.projectId })); } async getCredentialIdsByUserAndRole(userIds, options, trx) { const projectRoles = 'scopes' in options ? this.roleService.rolesWithScope('project', options.scopes) : options.projectRoles; const credentialRoles = 'scopes' in options ? 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", [shared_credentials_repository_1.SharedCredentialsRepository, role_service_1.RoleService, credentials_repository_1.CredentialsRepository]) ], CredentialsFinderService); //# sourceMappingURL=credentials-finder.service.js.map