3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
26 use Friendica\Content\Pager;
27 use Friendica\Core\L10n;
28 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
29 use Friendica\Model\Register;
30 use Friendica\Model\User as UserModel;
31 use Friendica\Util\Temporal;
33 use Seld\CliPrompt\CliPrompt;
36 * tool to manage users of the current node
38 class User extends \Asika\SimpleConsole\Console
40 protected $helpOptions = ['h', 'help', '?'];
51 * @var IManagePersonalConfigValues
55 protected function getHelp()
58 console user - Modify user settings per console commands.
60 bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
61 bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-h|--help|-?] [-v]
62 bin/console user delete [<nickname>] [-y] [-h|--help|-?] [-v]
63 bin/console user allow [<nickname>] [-h|--help|-?] [-v]
64 bin/console user deny [<nickname>] [-h|--help|-?] [-v]
65 bin/console user block [<nickname>] [-h|--help|-?] [-v]
66 bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
67 bin/console user list pending [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
68 bin/console user list removed [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
69 bin/console user list active [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
70 bin/console user list all [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
71 bin/console user search id <UID> [-h|--help|-?] [-v]
72 bin/console user search nick <nick> [-h|--help|-?] [-v]
73 bin/console user search mail <mail> [-h|--help|-?] [-v]
74 bin/console user search guid <GUID> [-h|--help|-?] [-v]
75 bin/console user config list [<nickname>] [<category>] [-h|--help|-?] [-v]
76 bin/console user config get [<nickname>] [<category>] [<key>] [-h|--help|-?] [-v]
77 bin/console user config set [<nickname>] [<category>] [<key>] [<value>] [-h|--help|-?] [-v]
78 bin/console user config delete [<nickname>] [<category>] [<key>] [-h|--help|-?] [-v]
81 Modify user settings per console commands.
84 -h|--help|-? Show help information
85 -v Show more debug information
86 -y Non-interactive mode, assume "yes" as answer to the user deletion prompt
91 public function __construct(App\Mode $appMode, L10n $l10n, IManagePersonalConfigValues $pConfig, array $argv = null)
93 parent::__construct($argv);
95 $this->appMode = $appMode;
97 $this->pConfig = $pConfig;
100 protected function doExecute(): int
102 if ($this->getOption('v')) {
103 $this->out('Class: ' . __CLASS__);
104 $this->out('Arguments: ' . var_export($this->args, true));
105 $this->out('Options: ' . var_export($this->options, true));
108 if (count($this->args) == 0) {
109 $this->out($this->getHelp());
113 if ($this->appMode->isInstall()) {
114 throw new RuntimeException('Database isn\'t ready or populated yet');
117 $command = $this->getArgument(0);
121 return $this->password();
123 return $this->addUser();
125 return $this->pendingUser(true);
127 return $this->pendingUser(false);
129 return $this->blockUser(true);
131 return $this->blockUser(false);
133 return $this->deleteUser();
135 return $this->listUser();
137 return $this->searchUser();
139 return $this->configUser();
141 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
146 * Retrieves the user nick, either as an argument or from a prompt
148 * @param int $arg_index Index of the nick argument in the arguments list
150 * @return string nick of the user
151 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
153 private function getNick($arg_index)
155 $nick = $this->getArgument($arg_index);
158 $this->out($this->l10n->t('Enter user nickname: '));
159 $nick = CliPrompt::prompt();
161 throw new RuntimeException('A nick name must be set.');
169 * Retrieves the user from a nick supplied as an argument or from a prompt
171 * @param int $arg_index Index of the nick argument in the arguments list
173 * @return array|boolean User record with uid field, or false if user is not found
174 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
176 private function getUserByNick($arg_index)
178 $nick = $this->getNick($arg_index);
180 $user = UserModel::getByNickname($nick, ['uid']);
182 throw new RuntimeException($this->l10n->t('User not found'));
189 * Sets a new password
191 * @return int Return code of this command
195 private function password()
197 $user = $this->getUserByNick(1);
199 $password = $this->getArgument(2);
201 if (is_null($password)) {
202 $this->out($this->l10n->t('Enter new password: '), false);
203 $password = CliPrompt::hiddenPrompt(true);
207 $result = UserModel::updatePassword($user['uid'], $password);
209 if (empty($result)) {
210 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
213 $this->out($this->l10n->t('Password changed.'));
214 } catch (\Exception $e) {
215 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
222 * Adds a new user based on given console arguments
224 * @return bool True, if the command was successful
225 * @throws \ErrorException
226 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
227 * @throws \ImagickException
229 private function addUser()
231 $name = $this->getArgument(1);
232 $nick = $this->getArgument(2);
233 $email = $this->getArgument(3);
234 $lang = $this->getArgument(4);
237 $this->out($this->l10n->t('Enter user name: '));
238 $name = CliPrompt::prompt();
240 throw new RuntimeException('A name must be set.');
245 $this->out($this->l10n->t('Enter user nickname: '));
246 $nick = CliPrompt::prompt();
248 throw new RuntimeException('A nick name must be set.');
253 $this->out($this->l10n->t('Enter user email address: '));
254 $email = CliPrompt::prompt();
256 throw new RuntimeException('A email address must be set.');
261 $this->out($this->l10n->t('Enter a language (optional): '));
262 $lang = CliPrompt::prompt();
266 return UserModel::createMinimal($name, $email, $nick);
268 return UserModel::createMinimal($name, $email, $nick, $lang);
273 * Allows or denys a user based on it's nickname
275 * @param bool $allow True, if the pending user is allowed, false if denies
277 * @return bool True, if allow was successful
278 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
280 private function pendingUser(bool $allow = true)
282 $user = $this->getUserByNick(1);
284 $pending = Register::getPendingForUser($user['uid'] ?? 0);
285 if (empty($pending)) {
286 throw new RuntimeException($this->l10n->t('User is not pending.'));
289 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
293 * Blocks/unblocks a user
295 * @param bool $block True, if the given user should get blocked
297 * @return bool True, if the command was successful
300 private function blockUser(bool $block = true)
302 $user = $this->getUserByNick(1);
304 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
310 * @return bool True, if the delete was successful
313 private function deleteUser(): bool
315 $user = $this->getUserByNick(1);
317 if (!empty($user['account_removed'])) {
318 $this->out($this->l10n->t('User has already been marked for deletion.'));
322 if (!$this->getOption('y')) {
323 $this->out($this->l10n->t('Type "yes" to delete %s', $this->getArgument(1)));
324 if (CliPrompt::prompt() !== 'yes') {
325 throw new RuntimeException($this->l10n->t('Deletion aborted.'));
329 return UserModel::remove($user['uid']);
333 * List users of the current node
335 * @return bool True, if the command was successful
337 private function listUser()
339 $subCmd = $this->getArgument(1);
340 $start = $this->getOption(['s', 'start'], 0);
341 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
343 $table = new Console_Table();
347 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
348 $pending = Register::getPending($start, $count);
349 foreach ($pending as $contact) {
355 Temporal::getRelativeDate($contact['created']),
359 $this->out($table->getTable());
364 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
365 $contacts = UserModel::getList($start, $count, $subCmd);
366 foreach ($contacts as $contact) {
372 Temporal::getRelativeDate($contact['created']),
373 Temporal::getRelativeDate($contact['last-activity']),
374 Temporal::getRelativeDate($contact['last-item']),
377 $this->out($table->getTable());
380 $this->out($this->getHelp());
386 * Returns a user based on search parameter
388 * @return bool True, if the command was successful
390 private function searchUser()
404 $subCmd = $this->getArgument(1);
405 $param = $this->getArgument(2);
407 $table = new Console_Table();
408 $table->setHeaders(['UID', 'GUID', 'Name', 'Nick', 'E-Mail', 'Register', 'Login', 'Verified', 'Blocked']);
412 $user = UserModel::getById($param, $fields);
415 $user = UserModel::getByGuid($param, $fields);
418 $user = UserModel::getByEmail($param, $fields);
421 $user = UserModel::getByNickname($param, $fields);
424 $this->out($this->getHelp());
429 $table->addRow($user);
431 $this->out($table->getTable());
437 * Queries and modifies user-specific configuration
439 * @return bool True, if the command was successful
441 private function configUser()
443 $subCmd = $this->getArgument(1);
445 $user = $this->getUserByNick(2);
447 $category = $this->getArgument(3);
449 if (is_null($category)) {
450 $this->out($this->l10n->t('Enter category: '), false);
451 $category = CliPrompt::prompt();
452 if (empty($category)) {
453 throw new RuntimeException('A category must be selected.');
457 $key = $this->getArgument(4);
459 if ($subCmd != 'list' and is_null($key)) {
460 $this->out($this->l10n->t('Enter key: '), false);
461 $key = CliPrompt::prompt();
463 throw new RuntimeException('A key must be selected.');
467 $values = $this->pConfig->load($user['uid'], $category);
471 $table = new Console_Table();
472 $table->setHeaders(['Key', 'Value']);
473 if (array_key_exists($category, $values)) {
474 foreach (array_keys($values[$category]) as $key) {
475 $table->addRow([$key, $values[$category][$key]]);
478 $this->out($table->getTable());
481 if (!array_key_exists($category, $values)) {
482 throw new RuntimeException('Category does not exist');
484 if (!array_key_exists($key, $values[$category])) {
485 throw new RuntimeException('Key does not exist');
488 $this->out($this->pConfig->get($user['uid'], $category, $key));
491 $value = $this->getArgument(5);
493 if (is_null($value)) {
494 $this->out($this->l10n->t('Enter value: '), false);
495 $value = CliPrompt::prompt();
497 throw new RuntimeException('A value must be specified.');
501 if (array_key_exists($category, $values) and
502 array_key_exists($key, $values[$category]) and
503 $values[$category][$key] == $value) {
504 throw new RuntimeException('Value not changed');
507 $this->pConfig->set($user['uid'], $category, $key, $value);
510 if (!array_key_exists($category, $values)) {
511 throw new RuntimeException('Category does not exist');
513 if (!array_key_exists($key, $values[$category])) {
514 throw new RuntimeException('Key does not exist');
517 $this->pConfig->delete($user['uid'], $category, $key);
520 $this->out($this->getHelp());