3 * @copyright Copyright (C) 2010-2021, 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\IPConfig;
29 use Friendica\Database\Database;
30 use Friendica\Model\Register;
31 use Friendica\Model\User as UserModel;
32 use Friendica\Util\Temporal;
34 use Seld\CliPrompt\CliPrompt;
37 * tool to manage users of the current node
39 class User extends \Asika\SimpleConsole\Console
41 protected $helpOptions = ['h', 'help', '?'];
56 protected function getHelp()
59 console user - Modify user settings per console commands.
61 bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
62 bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-h|--help|-?] [-v]
63 bin/console user delete [<nickname>] [-y] [-h|--help|-?] [-v]
64 bin/console user allow [<nickname>] [-h|--help|-?] [-v]
65 bin/console user deny [<nickname>] [-h|--help|-?] [-v]
66 bin/console user block [<nickname>] [-h|--help|-?] [-v]
67 bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
68 bin/console user list pending [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
69 bin/console user list removed [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
70 bin/console user list active [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
71 bin/console user list all [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
72 bin/console user search id <UID> [-h|--help|-?] [-v]
73 bin/console user search nick <nick> [-h|--help|-?] [-v]
74 bin/console user search mail <mail> [-h|--help|-?] [-v]
75 bin/console user search guid <GUID> [-h|--help|-?] [-v]
76 bin/console user config list [<nickname>] [<category>] [-h|--help|-?] [-v]
77 bin/console user config get [<nickname>] [<category>] [<key>] [-h|--help|-?] [-v]
78 bin/console user config set [<nickname>] [<category>] [<key>] [<value>] [-h|--help|-?] [-v]
79 bin/console user config delete [<nickname>] [<category>] [<key>] [-h|--help|-?] [-v]
82 Modify user settings per console commands.
85 -h|--help|-? Show help information
86 -v Show more debug information
87 -y Non-interactive mode, assume "yes" as answer to the user deletion prompt
92 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, IPConfig $pConfig, array $argv = null)
94 parent::__construct($argv);
96 $this->appMode = $appMode;
98 $this->pConfig = $pConfig;
101 protected function doExecute()
103 if ($this->getOption('v')) {
104 $this->out('Class: ' . __CLASS__);
105 $this->out('Arguments: ' . var_export($this->args, true));
106 $this->out('Options: ' . var_export($this->options, true));
109 if (count($this->args) == 0) {
110 $this->out($this->getHelp());
114 if ($this->appMode->isInstall()) {
115 throw new RuntimeException('Database isn\'t ready or populated yet');
118 $command = $this->getArgument(0);
122 return $this->password();
124 return $this->addUser();
126 return $this->pendingUser(true);
128 return $this->pendingUser(false);
130 return $this->blockUser(true);
132 return $this->blockUser(false);
134 return $this->deleteUser();
136 return $this->listUser();
138 return $this->searchUser();
140 return $this->configUser();
142 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
147 * Retrieves the user nick, either as an argument or from a prompt
149 * @param int $arg_index Index of the nick argument in the arguments list
151 * @return string nick of the user
152 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
154 private function getNick($arg_index)
156 $nick = $this->getArgument($arg_index);
159 $this->out($this->l10n->t('Enter user nickname: '));
160 $nick = CliPrompt::prompt();
162 throw new RuntimeException('A nick name must be set.');
170 * Retrieves the user from a nick supplied as an argument or from a prompt
172 * @param int $arg_index Index of the nick argument in the arguments list
174 * @return array|boolean User record with uid field, or false if user is not found
175 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
177 private function getUserByNick($arg_index)
179 $nick = $this->getNick($arg_index);
181 $user = UserModel::getByNickname($nick, ['uid']);
183 throw new RuntimeException($this->l10n->t('User not found'));
190 * Sets a new password
192 * @return int Return code of this command
196 private function password()
198 $user = $this->getUserByNick(1);
200 $password = $this->getArgument(2);
202 if (is_null($password)) {
203 $this->out($this->l10n->t('Enter new password: '), false);
204 $password = CliPrompt::hiddenPrompt(true);
208 $result = UserModel::updatePassword($user['uid'], $password);
211 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
214 $this->out($this->l10n->t('Password changed.'));
215 } catch (\Exception $e) {
216 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
223 * Adds a new user based on given console arguments
225 * @return bool True, if the command was successful
226 * @throws \ErrorException
227 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
228 * @throws \ImagickException
230 private function addUser()
232 $name = $this->getArgument(1);
233 $nick = $this->getArgument(2);
234 $email = $this->getArgument(3);
235 $lang = $this->getArgument(4);
238 $this->out($this->l10n->t('Enter user name: '));
239 $name = CliPrompt::prompt();
241 throw new RuntimeException('A name must be set.');
246 $this->out($this->l10n->t('Enter user nickname: '));
247 $nick = CliPrompt::prompt();
249 throw new RuntimeException('A nick name must be set.');
254 $this->out($this->l10n->t('Enter user email address: '));
255 $email = CliPrompt::prompt();
257 throw new RuntimeException('A email address must be set.');
262 $this->out($this->l10n->t('Enter a language (optional): '));
263 $lang = CliPrompt::prompt();
267 return UserModel::createMinimal($name, $email, $nick);
269 return UserModel::createMinimal($name, $email, $nick, $lang);
274 * Allows or denys a user based on it's nickname
276 * @param bool $allow True, if the pending user is allowed, false if denies
278 * @return bool True, if allow was successful
279 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
281 private function pendingUser(bool $allow = true)
283 $user = $this->getUserByNick(1);
285 $pending = Register::getPendingForUser($user['uid'] ?? 0);
286 if (empty($pending)) {
287 throw new RuntimeException($this->l10n->t('User is not pending.'));
290 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
294 * Blocks/unblocks a user
296 * @param bool $block True, if the given user should get blocked
298 * @return bool True, if the command was successful
301 private function blockUser(bool $block = true)
303 $user = $this->getUserByNick(1);
305 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
311 * @return bool True, if the delete was successful
314 private function deleteUser()
316 $user = $this->getUserByNick(1);
318 if (!empty($user['account_removed'])) {
319 $this->out($this->l10n->t('User has already been marked for deletion.'));
323 if (!$this->getOption('y')) {
324 $this->out($this->l10n->t('Type "yes" to delete %s', $this->getArgument(1)));
325 if (CliPrompt::prompt() !== 'yes') {
326 throw new RuntimeException($this->l10n->t('Deletion aborted.'));
330 return UserModel::remove($user['uid']);
334 * List users of the current node
336 * @return bool True, if the command was successful
338 private function listUser()
340 $subCmd = $this->getArgument(1);
341 $start = $this->getOption(['s', 'start'], 0);
342 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
344 $table = new Console_Table();
348 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
349 $pending = Register::getPending($start, $count);
350 foreach ($pending as $contact) {
356 Temporal::getRelativeDate($contact['created']),
360 $this->out($table->getTable());
365 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
366 $contacts = UserModel::getList($start, $count, $subCmd);
367 foreach ($contacts as $contact) {
373 Temporal::getRelativeDate($contact['created']),
374 Temporal::getRelativeDate($contact['login_date']),
375 Temporal::getRelativeDate($contact['last-item']),
378 $this->out($table->getTable());
381 $this->out($this->getHelp());
387 * Returns a user based on search parameter
389 * @return bool True, if the command was successful
391 private function searchUser()
405 $subCmd = $this->getArgument(1);
406 $param = $this->getArgument(2);
408 $table = new Console_Table();
409 $table->setHeaders(['UID', 'GUID', 'Name', 'Nick', 'E-Mail', 'Register', 'Login', 'Verified', 'Blocked']);
413 $user = UserModel::getById($param, $fields);
416 $user = UserModel::getByGuid($param, $fields);
419 $user = UserModel::getByEmail($param, $fields);
422 $user = UserModel::getByNickname($param, $fields);
425 $this->out($this->getHelp());
430 $table->addRow($user);
432 $this->out($table->getTable());
438 * Queries and modifies user-specific configuration
440 * @return bool True, if the command was successful
442 private function configUser()
444 $subCmd = $this->getArgument(1);
446 $user = $this->getUserByNick(2);
448 $category = $this->getArgument(3);
450 if (is_null($category)) {
451 $this->out($this->l10n->t('Enter category: '), false);
452 $category = CliPrompt::prompt();
453 if (empty($category)) {
454 throw new RuntimeException('A category must be selected.');
458 $key = $this->getArgument(4);
460 if ($subCmd != 'list' and is_null($key)) {
461 $this->out($this->l10n->t('Enter key: '), false);
462 $key = CliPrompt::prompt();
464 throw new RuntimeException('A key must be selected.');
468 $values = $this->pConfig->load($user['uid'], $category);
472 $table = new Console_Table();
473 $table->setHeaders(['Key', 'Value']);
474 if (array_key_exists($category, $values)) {
475 foreach (array_keys($values[$category]) as $key) {
476 $table->addRow([$key, $values[$category][$key]]);
479 $this->out($table->getTable());
482 if (!array_key_exists($category, $values)) {
483 throw new RuntimeException('Category does not exist');
485 if (!array_key_exists($key, $values[$category])) {
486 throw new RuntimeException('Key does not exist');
489 $this->out($this->pConfig->get($user['uid'], $category, $key));
492 $value = $this->getArgument(5);
494 if (is_null($value)) {
495 $this->out($this->l10n->t('Enter value: '), false);
496 $value = CliPrompt::prompt();
498 throw new RuntimeException('A value must be specified.');
502 if (array_key_exists($category, $values) and
503 array_key_exists($key, $values[$category]) and
504 $values[$category][$key] == $value) {
505 throw new RuntimeException('Value not changed');
508 $this->pConfig->set($user['uid'], $category, $key, $value);
511 if (!array_key_exists($category, $values)) {
512 throw new RuntimeException('Category does not exist');
514 if (!array_key_exists($key, $values[$category])) {
515 throw new RuntimeException('Key does not exist');
518 $this->pConfig->delete($user['uid'], $category, $key);
521 $this->out($this->getHelp());