From 3282acbcf126db80981a0d7432b4abf5a2a9ddeb Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 30 Jun 2019 19:09:08 +0200 Subject: [PATCH] :zap: Before credentials get created or updated check if one with same type and name already exist --- packages/cli/src/Server.ts | 29 +++++++++++++++++++ .../src/components/CredentialsInput.vue | 21 ++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 5fc4e5e4cd..38ac064a53 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -55,8 +55,10 @@ import { import { FindManyOptions, + FindOneOptions, LessThan, LessThanOrEqual, + Not, } from 'typeorm'; import * as parseUrl from 'parseurl'; @@ -499,6 +501,19 @@ class App { throw new Error('No encryption key got found to encrypt the credentials!'); } + // Check if credentials with the same name and type exist already + const findQuery = { + where: { + name: incomingData.name, + type: incomingData.type, + }, + } as FindOneOptions; + + const checkResult = await Db.collections.Credentials!.findOne(findQuery); + if (checkResult !== undefined) { + throw new ResponseHelper.ReponseError(`Credentials with the same type and name exist already.`, undefined, 400); + } + // Encrypt the data const credentials = new Credentials(incomingData.name, incomingData.type, incomingData.nodesAccess); credentials.setData(incomingData.data, encryptionKey); @@ -532,6 +547,20 @@ class App { } } + // Check if credentials with the same name and type exist already + const findQuery = { + where: { + id: Not(id), + name: incomingData.name, + type: incomingData.type, + }, + } as FindOneOptions; + + const checkResult = await Db.collections.Credentials!.findOne(findQuery); + if (checkResult !== undefined) { + throw new ResponseHelper.ReponseError(`Credentials with the same type and name exist already.`, undefined, 400); + } + const encryptionKey = await UserSettings.getEncryptionKey(); if (encryptionKey === undefined) { throw new Error('No encryption key got found to encrypt the credentials!'); diff --git a/packages/editor-ui/src/components/CredentialsInput.vue b/packages/editor-ui/src/components/CredentialsInput.vue index 6015a95194..6de56dd42d 100644 --- a/packages/editor-ui/src/components/CredentialsInput.vue +++ b/packages/editor-ui/src/components/CredentialsInput.vue @@ -71,6 +71,8 @@ import Vue from 'vue'; import { restApi } from '@/components/mixins/restApi'; import { nodeHelpers } from '@/components/mixins/nodeHelpers'; +import { showMessage } from '@/components/mixins/showMessage'; + import { ICredentialsDecryptedResponse, IUpdateInformation } from '@/Interface'; import { CredentialInformation, @@ -89,6 +91,7 @@ import mixins from 'vue-typed-mixins'; export default mixins( nodeHelpers, restApi, + showMessage, ).extend({ name: 'CredentialsInput', props: [ @@ -152,7 +155,7 @@ export default mixins( // @ts-ignore this.propertyValue[name] = parameterData.value; }, - async createCredentials () { + async createCredentials (): Promise { const nodesAccess = this.nodesAccess.map((nodeType) => { return { nodeType, @@ -166,7 +169,13 @@ export default mixins( data: this.propertyValue, } as ICredentialsDecrypted; - const result = await this.restApi().createNewCredentials(newCredentials); + let result; + try { + result = await this.restApi().createNewCredentials(newCredentials); + } catch (error) { + this.$showError(error, 'Problem Creating Credentials', 'There was a problem creating the credentials:'); + return; + } // Add also to local store this.$store.commit('addCredentials', result); @@ -202,7 +211,13 @@ export default mixins( data: this.propertyValue, } as ICredentialsDecrypted; - const result = await this.restApi().updateCredentials((this.credentialData as ICredentialsDecryptedResponse).id as string, newCredentials); + let result; + try { + result = await this.restApi().updateCredentials((this.credentialData as ICredentialsDecryptedResponse).id as string, newCredentials); + } catch (error) { + this.$showError(error, 'Problem Updating Credentials', 'There was a problem updating the credentials:'); + return; + } // Update also in local store this.$store.commit('updateCredentials', result);