fix: [CLI] added some new functionalities

- list roles
- create user
pull/9613/head
iglocska 2024-03-07 13:56:03 +01:00
parent b6d7755e9e
commit f1102decf6
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,55 @@
<?php
/**
* @property User $User
* @property Log $Log
* @property UserLoginProfile $UserLoginProfile
*/
class RoleShell extends AppShell
{
public $uses = ['Role'];
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->addSubcommand('list', [
'help' => __('Get list of the roles.'),
'parser' => [
'arguments' => [
'filter' => ['help' => __('Filter list by name.'), 'required' => false],
],
'options' => [
'json' => ['help' => __('Output as JSON.'), 'boolean' => true],
],
]
]);
return $parser;
}
public function list()
{
$filter = $this->args[0] ?? null;
if ($filter) {
$conditions = ['OR' => [
'Role.name LIKE' => "%$userId%"
]];
} else {
$conditions = [];
}
$roles = $this->Role->find('all', [
'recursive' => -1,
'conditions' => $conditions
]);
if ($this->params['json']) {
$this->out($this->json($roles));
} else {
foreach ($roles as $role) {
$this->out(sprintf(
'%d. %s',
$role['Role']['id'],
$role['Role']['name']
));
}
}
}
}

View File

@ -23,6 +23,20 @@ class UserShell extends AppShell
],
]
]);
$parser->addSubcommand('create', [
'help' => __('Create a new user account.'),
'parser' => [
'arguments' => [
'email' => ['help' => __('E-mail address (also used as the username.'), 'required' => true],
'role_id' => ['help' => __('Role ID of the user. For a list of available roles, use `cake Roles list`.'), 'required' => true],
'org_id' => ['help' => __('Organisation under which the user should be created'), 'required' => true],
'password' => ['help' => __('Enter a password to assign to the user (optional) - if none is set, the user will receive a temporary password.')]
],
'options' => [
'json' => ['help' => __('Output as JSON.'), 'boolean' => true],
],
]
]);
$parser->addSubcommand('init', [
'help' => __('Create default role, organisation and user when not exists.'),
]);
@ -189,6 +203,48 @@ class UserShell extends AppShell
}
}
public function create()
{
if (empty($this->args[0]) || empty($this->args[1]) || empty($this->args[2])) {
$this->err('Invalid input. Usage: `User create [email] [role_id] [org_id] [password:optional]`');
}
$user = [
'email' => $this->args[0],
'role_id' => $this->args[1],
'org_id' => $this->args[2],
'change_pw' => true
];
if (!empty($this->args[3])) {
$user['password'] = $this->args[3];
$user['confirm_password'] = $this->args[3];
$user['change_pw'] = true;
}
$this->User->create();
$result = $this->User->save($user);
// do not fetch sensitive or big values
$schema = $this->User->schema();
unset($schema['authkey']);
unset($schema['password']);
unset($schema['gpgkey']);
unset($schema['certif_public']);
$fields = array_keys($schema);
$fields[] = 'Role.*';
$fields[] = 'Organisation.*';
$user = $this->User->find('first', [
'recursive' => -1,
'fields' => $fields,
'conditions' => ['User.id' => $this->User->id],
'contain' => ['Organisation', 'Role', 'UserSetting'],
]);
if ($this->params['json']) {
$this->out($this->json($user));
} else {
$this->out('User created.');
}
}
public function init()
{
if (!Configure::read('Security.salt')) {