From bed7f67203b130de3fc0fd574e153a10101ae4fc Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Fri, 18 Sep 2020 11:49:36 -0400 Subject: [PATCH] Add role create cli command --- api/src/cli/commands/roles/create.ts | 14 ++++++++++++++ api/src/cli/commands/user/create.ts | 23 ----------------------- api/src/cli/commands/users/create.ts | 14 ++++++++++++++ api/src/cli/index.ts | 25 ++++++++++++++++++------- 4 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 api/src/cli/commands/roles/create.ts delete mode 100644 api/src/cli/commands/user/create.ts create mode 100644 api/src/cli/commands/users/create.ts diff --git a/api/src/cli/commands/roles/create.ts b/api/src/cli/commands/roles/create.ts new file mode 100644 index 0000000000..c7947bbcfd --- /dev/null +++ b/api/src/cli/commands/roles/create.ts @@ -0,0 +1,14 @@ +export default async function rolesCreate({ name, admin }: any) { + const database = require('../../../database/index').default; + const RolesService = require('../../../services/roles').default; + + if (!name) { + console.error('Name is required'); + process.exit(1); + } + + const service = new RolesService(); + const id = await service.create({ name, admin, status: 'active' }); + console.log(id); + database.destroy(); +} diff --git a/api/src/cli/commands/user/create.ts b/api/src/cli/commands/user/create.ts deleted file mode 100644 index 3fe79e9d90..0000000000 --- a/api/src/cli/commands/user/create.ts +++ /dev/null @@ -1,23 +0,0 @@ -import argon2 from 'argon2'; -import { v4 as uuidV4 } from 'uuid'; - -export default async function userCreate({ email, password }: any) { - if (!email || !password) { - console.error('Email and password are required'); - process.exit(1); - } - - const database = require('../../../database').default; - - const existingUser = await database.select('id').from('directus_users').where({ email }).first(); - - if (existingUser) { - console.error(`User with email "${email}" already exists`); - process.exit(1); - } - - const hashedPassword = await argon2.hash(password); - await database.insert({ id: uuidV4(), email, password: hashedPassword }).into('directus_users'); - - database.destroy(); -} diff --git a/api/src/cli/commands/users/create.ts b/api/src/cli/commands/users/create.ts new file mode 100644 index 0000000000..9e0960f305 --- /dev/null +++ b/api/src/cli/commands/users/create.ts @@ -0,0 +1,14 @@ +export default async function usersCreate({ email, password, role }: any) { + const database = require('../../../database/index').default; + const UsersService = require('../../../services/users').default; + + if (!email || !password || !role) { + console.error('Email, password, role are required'); + process.exit(1); + } + + const service = new UsersService(); + const id = await service.create({ email, password, role }); + console.log(id); + database.destroy(); +} diff --git a/api/src/cli/index.ts b/api/src/cli/index.ts index 57d0a67e22..a7c20c52f6 100644 --- a/api/src/cli/index.ts +++ b/api/src/cli/index.ts @@ -8,7 +8,8 @@ import start from './commands/start'; import init from './commands/init'; import dbInstall from './commands/database/install'; import dbMigrate from './commands/database/migrate'; -import userCreate from './commands/user/create'; +import usersCreate from './commands/users/create'; +import rolesCreate from './commands/roles/create'; program.name('directus').usage('[command] [options]'); program.version(pkg.version, '-v, --version'); @@ -22,13 +23,23 @@ dbCommand.command('migrate:latest').description('Upgrade the database').action(( dbCommand.command('migrate:up').description('Upgrade the database').action(() => dbMigrate('up')); dbCommand.command('migrate:down').description('Downgrade the database').action(() => dbMigrate('down')); -const userCommand = program.command('user'); - -userCommand +const usersCommand = program.command('users'); +usersCommand .command('create') .description('Create a new user') - .option('-e, --email ', `user's email`) - .option('-p, --password ', `user's password`) - .action(userCreate); + .option('--email ', `user's email`) + .option('--password ', `user's password`) + .option('--role ', `user's role`) + .action(usersCreate); + +const rolesCommand = program.command('roles'); +rolesCommand +.command('create') + .storeOptionsAsProperties(false) + .passCommandToAction(false) + .description('Create a new role') + .option('--name ', `name for the role`) + .option('--admin', `whether or not the role has admin access`) + .action(rolesCreate); program.parse(process.argv);