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

Refactor misc endpoints for user management #2707

Merged
47 changes: 46 additions & 1 deletion packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable import/no-cycle */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Credentials } from 'n8n-core';

import {
Expand All @@ -17,7 +18,8 @@ import {
WorkflowExecuteMode,
} from 'n8n-workflow';

import { CredentialsOverwrites, CredentialTypes, Db, ICredentialsDb } from '.';
import { CredentialsOverwrites, CredentialTypes, Db, ICredentialsDb, WhereClause } from '.';
import { User } from './databases/entities/User';

const mockNodeTypes: INodeTypes = {
nodeTypes: {},
Expand Down Expand Up @@ -270,3 +272,46 @@ export class CredentialsHelper extends ICredentialsHelper {
await Db.collections.Credentials!.update(findQuery, newCredentialsData);
}
}

/**
* Build a `where` clause for a `find()` or `findOne()` operation
* in the `shared_workflow` or `shared_credentials` tables.
*/
export function whereClause({
user,
entityType,
entityId = '',
}: {
user: User;
entityType: 'workflow' | 'credentials';
entityId?: string;
}): WhereClause {
const where: WhereClause = entityId ? { [entityType]: { id: entityId } } : {};

if (user.globalRole.name !== 'owner') {
where.user = { id: user.id };
}

return where;
}

/**
* Get a credential if it has been shared with a user.
*/
export async function getCredentialForUser(
credentialId: string,
user: User,
): Promise<ICredentialsDb | null> {
const sharedCredential = await Db.collections.SharedCredentials!.findOne({
relations: ['credentials'],
where: whereClause({
user,
entityType: 'credentials',
entityId: credentialId,
}),
});

if (!sharedCredential) return null;

return sharedCredential.credentials as ICredentialsDb;
}
15 changes: 1 addition & 14 deletions packages/cli/src/PersonalizationSurvey.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { readFileSync, writeFile } from 'fs';
import { promisify } from 'util';
import { readFileSync } from 'fs';
import { UserSettings } from 'n8n-core';

import * as config from '../config';
// eslint-disable-next-line import/no-cycle
import { Db, IPersonalizationSurvey, IPersonalizationSurveyAnswers } from '.';

const fsWriteFile = promisify(writeFile);

const PERSONALIZATION_SURVEY_FILENAME = 'personalizationSurvey.json';

function loadSurveyFromDisk(): IPersonalizationSurveyAnswers | undefined {
Expand All @@ -23,16 +20,6 @@ function loadSurveyFromDisk(): IPersonalizationSurveyAnswers | undefined {
}
}

export async function writeSurveyToDisk(
surveyAnswers: IPersonalizationSurveyAnswers,
): Promise<void> {
const userSettingsPath = UserSettings.getUserN8nFolderPath();
await fsWriteFile(
`${userSettingsPath}/${PERSONALIZATION_SURVEY_FILENAME}`,
JSON.stringify(surveyAnswers, null, '\t'),
);
}

export async function preparePersonalizationSurvey(): Promise<IPersonalizationSurvey> {
const survey: IPersonalizationSurvey = {
shouldShow: false,
Expand Down
Loading