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

Create reset command for user management #2698

Merged
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
63 changes: 63 additions & 0 deletions packages/cli/commands/user-management/reset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import Command from '@oclif/command';
import { Not } from 'typeorm';
import { LoggerProxy } from '../../../workflow/dist/src';
import { Db } from '../../src';
import { getLogger } from '../../src/Logger';

export class Reset extends Command {
static description = '\nResets the database to the default user state';

async run() {
const logger = getLogger();
LoggerProxy.init(logger);
await Db.init();

try {
const globalRole = await Db.collections.Role!.findOne({
name: 'owner',
scope: 'global',
});

const instanceOwner = await Db.collections.User!.findOneOrFail({ globalRole });
krynble marked this conversation as resolved.
Show resolved Hide resolved

// switch all workflows ownership to owner
await Db.collections.SharedWorkflow!.update(
{ user: Not(instanceOwner) },
{ user: instanceOwner },
);

// switch all credentials ownership to owner
await Db.collections.SharedCredentials!.update(
{ user: Not(instanceOwner) },
{ user: instanceOwner },
);

// delete all users from users table except owner
await Db.collections.User!.delete({ id: Not(instanceOwner.id) });

// reset user to being a shell
await Db.collections.User!.save(
Object.assign(instanceOwner, {
firstName: null,
krynble marked this conversation as resolved.
Show resolved Hide resolved
lastName: null,
email: null,
password: null,
resetPasswordToken: null,
}),
);

// update settings table
await Db.collections.Settings!.update({ key: 'userManagement.hasOwner' }, { value: 'false' });
} catch (error) {
console.error('Error resetting database. See log messages for details.');
logger.error(error.message);
this.exit(1);
}

this.exit();
}
}
2 changes: 1 addition & 1 deletion packages/nodes-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@
"basic-auth": "^2.0.1",
"change-case": "^4.1.1",
"cheerio": "1.0.0-rc.6",
"chokidar": "^3.5.2",
"chokidar": "3.5.2",
krynble marked this conversation as resolved.
Show resolved Hide resolved
"cron": "~1.7.2",
"eventsource": "^1.0.7",
"fast-glob": "^3.2.5",
Expand Down