mirror of
https://github.com/directus/directus.git
synced 2026-01-24 07:07:58 -05:00
Add role create cli command
This commit is contained in:
14
api/src/cli/commands/roles/create.ts
Normal file
14
api/src/cli/commands/roles/create.ts
Normal file
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
14
api/src/cli/commands/users/create.ts
Normal file
14
api/src/cli/commands/users/create.ts
Normal file
@@ -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();
|
||||
}
|
||||
@@ -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 <email>', `user's email`)
|
||||
.option('-p, --password <password>', `user's password`)
|
||||
.action(userCreate);
|
||||
.option('--email <value>', `user's email`)
|
||||
.option('--password <value>', `user's password`)
|
||||
.option('--role <value>', `user's role`)
|
||||
.action(usersCreate);
|
||||
|
||||
const rolesCommand = program.command('roles');
|
||||
rolesCommand
|
||||
.command('create')
|
||||
.storeOptionsAsProperties(false)
|
||||
.passCommandToAction(false)
|
||||
.description('Create a new role')
|
||||
.option('--name <value>', `name for the role`)
|
||||
.option('--admin', `whether or not the role has admin access`)
|
||||
.action(rolesCreate);
|
||||
|
||||
program.parse(process.argv);
|
||||
|
||||
Reference in New Issue
Block a user