diff --git a/app/Console/Command/UserShell.php b/app/Console/Command/UserShell.php index d65a0409fe0..410fcb8e379 100644 --- a/app/Console/Command/UserShell.php +++ b/app/Console/Command/UserShell.php @@ -16,7 +16,7 @@ public function getOptionParser() 'help' => __('Get list of user accounts.'), 'parser' => [ 'arguments' => [ - 'userId' => ['help' => __('User ID or e-mail address.'), 'required' => true], + 'userId' => ['help' => __('User ID or e-mail address to filter.'), 'required' => false], ], 'options' => [ 'json' => ['help' => __('Output as JSON.'), 'boolean' => true], @@ -82,6 +82,15 @@ public function getOptionParser() ], ], ]); + $parser->addSubcommand('change_role', [ + 'help' => __('Change user role.'), + 'parser' => [ + 'arguments' => [ + 'userId' => ['help' => __('User ID or e-mail address.'), 'required' => true], + 'new_role' => ['help' => __('Role ID or Role name.'), 'required' => true], + ] + ], + ]); $parser->addSubcommand('change_authkey', [ 'help' => __('Change authkey. When advanced authkeys are enabled, old authkeys will be disabled.'), 'parser' => [ @@ -443,6 +452,35 @@ public function change_authkey() } } + public function change_role() + { + list($userId, $newRole) = $this->args; + $user = $this->getUser($userId); + + if (is_numeric($newRole)) { + $conditions = ['Role.id' => $newRole]; + } else { + $conditions = ['Role.name' => $newRole]; + } + + $newRoleFromDb = $this->User->Role->find('first', [ + 'conditions' => $conditions, + 'fields' => ['Role.id'], + ]); + + if (empty($newRoleFromDb)) { + $this->error("Role `$newRole` not found."); + } + + if ($newRoleFromDb['Role']['id'] == $user['role_id']) { + $this->error("Role `$newRole` is already assigned to {$user['email']}."); + } + + $this->User->updateField($user, 'role_id', $newRoleFromDb['Role']['id']); + + $this->out("Role changed from `{$user['role_id']}` to `{$newRoleFromDb['Role']['id']}`."); + } + public function user_ips() { list($userId) = $this->args; @@ -575,7 +613,7 @@ public function expire_authkeys_without_ip_allowlist() } /** - * @param string|int $userId + * @param string|int $userId User ID or User e-mail * @return array */ private function getUser($userId)