Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix foreign credentials being shown for new nodes #4622

Merged
merged 3 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/editor-ui/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,13 @@ export interface INodeMetadata {
parametersLastUpdatedAt?: number;
}

export interface IUsedCredential {
id: string;
name: string;
credentialType: string;
currentUserHasAccess: boolean;
}

export interface WorkflowsState {
activeExecutions: IExecutionsCurrentSummaryExtended[];
activeWorkflows: string[];
Expand All @@ -915,6 +922,7 @@ export interface WorkflowsState {
finishedExecutionsCount: number;
nodeMetadata: NodeMetadataMap;
subWorkflowExecutionError: Error | null;
usedCredentials: Record<string, IUsedCredential>;
workflow: IWorkflowDb;
workflowExecutionData: IExecutionResponse | null;
workflowExecutionPairedItemMappings: {[itemId: string]: Set<string>};
Expand Down
4 changes: 2 additions & 2 deletions packages/editor-ui/src/components/NodeDetailsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ export default mixins(
},
hasForeignCredential(): boolean {
const credentials = (this.activeNode || {}).credentials;
const foreignCredentials = this.credentialsStore.foreignCredentialsById;
const usedCredentials = this.workflowsStore.usedCredentials;

let hasForeignCredential = false;
if (credentials && this.settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.WorkflowSharing)) {
Object.values(credentials).forEach((credential) => {
if (credential.id && foreignCredentials[credential.id] && !foreignCredentials[credential.id].currentUserHasAccess) {
if (credential.id && usedCredentials[credential.id] && !usedCredentials[credential.id].currentUserHasAccess) {
hasForeignCredential = true;
}
});
Expand Down
9 changes: 3 additions & 6 deletions packages/editor-ui/src/stores/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
getCredentialById() {
return (id: string): ICredentialsResponse => this.credentials[id];
},
foreignCredentialsById(): ICredentialMap {
return Object.fromEntries(Object.entries(this.credentials).filter(([_, credential]) => credential.hasOwnProperty('currentUserHasAccess')));
},
getCredentialByIdAndType() {
return (id: string, type: string): ICredentialsResponse | undefined => {
const credential = this.credentials[id];
Expand Down Expand Up @@ -226,7 +223,7 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {

return credential;
},
async deleteCredential({ id }: {id: string}): void {
async deleteCredential({ id }: {id: string}) {
const rootStore = useRootStore();
const deleted = await deleteCredential(rootStore.getRestApiContext, id);
if (deleted) {
Expand Down Expand Up @@ -263,10 +260,10 @@ export const useCredentialsStore = defineStore(STORES.CREDENTIALS, {
},

// Enterprise edition actions
setCredentialOwnedBy(payload: { credentialId: string, ownedBy: Partial<IUser> }): void {
setCredentialOwnedBy(payload: { credentialId: string, ownedBy: Partial<IUser> }) {
Vue.set(this.credentials[payload.credentialId], 'ownedBy', payload.ownedBy);
},
async setCredentialSharedWith(payload: { sharedWith: IUser[]; credentialId: string; }): void {
async setCredentialSharedWith(payload: { sharedWith: IUser[]; credentialId: string; }) {
if(useSettingsStore().isEnterpriseFeatureEnabled(EnterpriseEditionFeature.Sharing)) {
await setCredentialSharedWith(
useRootStore().getRestApiContext,
Expand Down
11 changes: 11 additions & 0 deletions packages/editor-ui/src/stores/workflows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_NEW_WORKFLOW_NAME, DUPLICATE_POSTFFIX, MAX_WORKFLOW_NAME_LENGTH, PLACEHOLDER_EMPTY_WORKFLOW_ID, STORES } from "@/constants";
import {
ICredentialMap,
IExecutionResponse,
IExecutionsCurrentSummaryExtended,
IExecutionsSummary,
Expand All @@ -9,6 +10,7 @@ import {
IPushDataExecutionFinished,
IPushDataNodeExecuteAfter,
IUpdateInformation,
IUsedCredential,
IWorkflowDb,
IWorkflowsMap,
WorkflowsState,
Expand All @@ -25,6 +27,7 @@ import { isJsonKeyObject } from "@/utils";
import { stringSizeInBytes } from "@/components/helpers";
import { useNDVStore } from "./ndv";
import { useNodeTypesStore } from "./nodeTypes";
import {ICredentialsDb} from "n8n";

export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
state: (): WorkflowsState => ({
Expand All @@ -41,6 +44,7 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
pinData: {},
hash: '',
},
usedCredentials: {},
activeWorkflows: [],
activeExecutions: [],
currentWorkflowExecutions: [],
Expand Down Expand Up @@ -220,6 +224,13 @@ export const useWorkflowsStore = defineStore(STORES.WORKFLOWS, {
this.workflow.id = id === 'new' ? PLACEHOLDER_EMPTY_WORKFLOW_ID : id;
},

setUsedCredentials(data: IUsedCredential[]) {
this.usedCredentials = data.reduce<{ [name: string]: IUsedCredential }>((accu, credential) => {
accu[credential.id!] = credential;
return accu;
}, {});
},

setWorkflowName(data: { newName: string, setStateDirty: boolean }): void {
if (data.setStateDirty === true) {
const uiStore = useUIStore();
Expand Down
6 changes: 3 additions & 3 deletions packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ export default mixins(
});

if (data.usedCredentials) {
this.credentialsStore.addCredentials(data.usedCredentials);
this.workflowsStore.setUsedCredentials(data.usedCredentials);
}

const tags = (data.tags || []) as ITag[];
Expand Down Expand Up @@ -2403,10 +2403,10 @@ export default mixins(
}

if (newNodeData.credentials && this.settingsStore.isEnterpriseFeatureEnabled(EnterpriseEditionFeature.WorkflowSharing)) {
const foreignCredentials = this.credentialsStore.foreignCredentialsById;
const usedCredentials = this.workflowsStore.usedCredentials;
newNodeData.credentials = Object.fromEntries(
Object.entries(newNodeData.credentials).filter(([_, credential]) => {
return credential.id && (!foreignCredentials[credential.id] || foreignCredentials[credential.id]?.currentUserHasAccess);
return credential.id && (!usedCredentials[credential.id] || usedCredentials[credential.id]?.currentUserHasAccess);
}),
);
}
Expand Down