Add cli passwd command (#4398)

* User passwd cli command

* Add command to list
This commit is contained in:
Adrian Dimitrov
2021-03-23 16:39:11 +02:00
committed by GitHub
parent 2f163a91b4
commit a18f9d68d5
2 changed files with 39 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import argon2 from 'argon2';
import { getSchema } from '../../../utils/get-schema';
export default async function usersPasswd({ email, password }: any) {
const { default: database } = require('../../../database/index');
const { UsersService } = require('../../../services/users');
if (!email || !password) {
console.error('Email and password are required');
process.exit(1);
}
try {
const passwordHashed = await argon2.hash(password);
const schema = await getSchema();
const service = new UsersService({ schema, knex: database });
const user = await service.knex.select('id').from('directus_users').where({ email }).first();
if (user) {
await service.knex('directus_users').update({ password: passwordHashed }).where({ id: user.id });
console.log(`Password is updated for user ${user.id}`);
} else {
console.log('No such user by this email');
}
await database.destroy();
process.exit(user ? 0 : 1);
} catch (err) {
console.error(err);
process.exit(1);
}
}

View File

@@ -9,6 +9,7 @@ import init from './commands/init';
import dbInstall from './commands/database/install';
import dbMigrate from './commands/database/migrate';
import usersCreate from './commands/users/create';
import usersPasswd from './commands/users/passwd';
import rolesCreate from './commands/roles/create';
import count from './commands/count';
import bootstrap from './commands/bootstrap';
@@ -42,6 +43,12 @@ usersCommand
.option('--password <value>', `user's password`)
.option('--role <value>', `user's role`)
.action(usersCreate);
usersCommand
.command('passwd')
.description('Set user password')
.option('--email <value>', `user's email`)
.option('--password <value>', `user's new password`)
.action(usersPasswd);
const rolesCommand = program.command('roles');
rolesCommand