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', '?'];
60 protected function getHelp()
63 console user - Modify user settings per console commands.
65 bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
66 bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-h|--help|-?] [-v]
67 bin/console user delete [<nickname>] [-y] [-h|--help|-?] [-v]
68 bin/console user allow [<nickname>] [-h|--help|-?] [-v]
69 bin/console user deny [<nickname>] [-h|--help|-?] [-v]
70 bin/console user block [<nickname>] [-h|--help|-?] [-v]
71 bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
72 bin/console user list pending [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
73 bin/console user list removed [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
74 bin/console user list active [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
75 bin/console user list all [-s|--start=0] [-c|--count=50] [-h|--help|-?] [-v]
76 bin/console user search id <UID> [-h|--help|-?] [-v]
77 bin/console user search nick <nick> [-h|--help|-?] [-v]
78 bin/console user search mail <mail> [-h|--help|-?] [-v]
79 bin/console user search guid <GUID> [-h|--help|-?] [-v]
80 bin/console user config list [<nickname>] [<category>] [-h|--help|-?] [-v]
81 bin/console user config get [<nickname>] [<category>] [<key>] [-h|--help|-?] [-v]
82 bin/console user config set [<nickname>] [<category>] [<key>] [<value>] [-h|--help|-?] [-v]
83 bin/console user config delete [<nickname>] [<category>] [<key>] [-h|--help|-?] [-v]
86 Modify user settings per console commands.
89 -h|--help|-? Show help information
90 -v Show more debug information
91 -y Non-interactive mode, assume "yes" as answer to the user deletion prompt
96 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, IPConfig $pConfig, array $argv = null)
98 parent::__construct($argv);
100 $this->appMode = $appMode;
103 $this->pConfig = $pConfig;
106 protected function doExecute()
108 if ($this->getOption('v')) {
109 $this->out('Class: ' . __CLASS__);
110 $this->out('Arguments: ' . var_export($this->args, true));
111 $this->out('Options: ' . var_export($this->options, true));
114 if (count($this->args) == 0) {
115 $this->out($this->getHelp());
119 if ($this->appMode->isInstall()) {
120 throw new RuntimeException('Database isn\'t ready or populated yet');
123 $command = $this->getArgument(0);
127 return $this->password();
129 return $this->addUser();
131 return $this->pendingUser(true);
133 return $this->pendingUser(false);
135 return $this->blockUser(true);
137 return $this->blockUser(false);
139 return $this->deleteUser();
141 return $this->listUser();
143 return $this->searchUser();
145 return $this->configUser();
147 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
152 * Retrieves the user nick, either as an argument or from a prompt
154 * @param int $arg_index Index of the nick argument in the arguments list
156 * @return string nick of the user
157 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
159 private function getNick($arg_index)
161 $nick = $this->getArgument($arg_index);
164 $this->out($this->l10n->t('Enter user nickname: '));
165 $nick = CliPrompt::prompt();
167 throw new RuntimeException('A nick name must be set.');
175 * Retrieves the user from a nick supplied as an argument or from a prompt
177 * @param int $arg_index Index of the nick argument in the arguments list
179 * @return mixed user data or dba failure result
180 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
182 private function getUserByNick($arg_index)
184 $nick = $this->getNick($arg_index);
186 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
187 if (!$this->dba->isResult($user)) {
188 throw new RuntimeException($this->l10n->t('User not found'));
195 * Sets a new password
197 * @return int Return code of this command
201 private function password()
203 $user = $this->getUserByNick(1);
205 $password = $this->getArgument(2);
207 if (is_null($password)) {
208 $this->out($this->l10n->t('Enter new password: '), false);
209 $password = CliPrompt::hiddenPrompt(true);
213 $result = UserModel::updatePassword($user['uid'], $password);
215 if (!$this->dba->isResult($result)) {
216 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
219 $this->out($this->l10n->t('Password changed.'));
220 } catch (\Exception $e) {
221 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
228 * Adds a new user based on given console arguments
230 * @return bool True, if the command was successful
231 * @throws \ErrorException
232 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
233 * @throws \ImagickException
235 private function addUser()
237 $name = $this->getArgument(1);
238 $nick = $this->getArgument(2);
239 $email = $this->getArgument(3);
240 $lang = $this->getArgument(4);
243 $this->out($this->l10n->t('Enter user name: '));
244 $name = CliPrompt::prompt();
246 throw new RuntimeException('A name must be set.');
251 $this->out($this->l10n->t('Enter user nickname: '));
252 $nick = CliPrompt::prompt();
254 throw new RuntimeException('A nick name must be set.');
259 $this->out($this->l10n->t('Enter user email address: '));
260 $email = CliPrompt::prompt();
262 throw new RuntimeException('A email address must be set.');
267 $this->out($this->l10n->t('Enter a language (optional): '));
268 $lang = CliPrompt::prompt();
272 return UserModel::createMinimal($name, $email, $nick);
274 return UserModel::createMinimal($name, $email, $nick, $lang);
279 * Allows or denys a user based on it's nickname
281 * @param bool $allow True, if the pending user is allowed, false if denies
283 * @return bool True, if allow was successful
284 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
286 private function pendingUser(bool $allow = true)
288 $user = $this->getUserByNick(1);
290 $pending = Register::getPendingForUser($user['uid'] ?? 0);
291 if (empty($pending)) {
292 throw new RuntimeException($this->l10n->t('User is not pending.'));
295 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
299 * Blocks/unblocks a user
301 * @param bool $block True, if the given user should get blocked
303 * @return bool True, if the command was successful
306 private function blockUser(bool $block = true)
308 $user = $this->getUserByNick(1);
310 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
316 * @return bool True, if the delete was successful
319 private function deleteUser()
321 $user = $this->getUserByNick(1);
323 if (!empty($user['account_removed'])) {
324 $this->out($this->l10n->t('User has already been marked for deletion.'));
328 if (!$this->getOption('y')) {
329 $this->out($this->l10n->t('Type "yes" to delete %s', $this->getArgument(1)));
330 if (CliPrompt::prompt() !== 'yes') {
331 throw new RuntimeException($this->l10n->t('Deletion aborted.'));
335 return UserModel::remove($user['uid']);
339 * List users of the current node
341 * @return bool True, if the command was successful
343 private function listUser()
345 $subCmd = $this->getArgument(1);
346 $start = $this->getOption(['s', 'start'], 0);
347 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
349 $table = new Console_Table();
353 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
354 $pending = Register::getPending($start, $count);
355 foreach ($pending as $contact) {
361 Temporal::getRelativeDate($contact['created']),
365 $this->out($table->getTable());
370 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
371 $contacts = UserModel::getList($start, $count, $subCmd);
372 foreach ($contacts as $contact) {
378 Temporal::getRelativeDate($contact['created']),
379 Temporal::getRelativeDate($contact['login_date']),
380 Temporal::getRelativeDate($contact['last-item']),
383 $this->out($table->getTable());
386 $this->out($this->getHelp());
392 * Returns a user based on search parameter
394 * @return bool True, if the command was successful
396 private function searchUser()
410 $subCmd = $this->getArgument(1);
411 $param = $this->getArgument(2);
413 $table = new Console_Table();
414 $table->setHeaders(['UID', 'GUID', 'Name', 'Nick', 'E-Mail', 'Register', 'Login', 'Verified', 'Blocked']);
418 $user = UserModel::getById($param, $fields);
421 $user = UserModel::getByGuid($param, $fields);
424 $user = UserModel::getByEmail($param, $fields);
427 $user = UserModel::getByNickname($param, $fields);
430 $this->out($this->getHelp());
434 $table->addRow($user);
435 $this->out($table->getTable());
441 * Queries and modifies user-specific configuration
443 * @return bool True, if the command was successful
445 private function configUser()
447 $subCmd = $this->getArgument(1);
449 $user = $this->getUserByNick(2);
451 $category = $this->getArgument(3);
453 if (is_null($category)) {
454 $this->out($this->l10n->t('Enter category: '), false);
455 $category = CliPrompt::prompt();
456 if (empty($category)) {
457 throw new RuntimeException('A category must be selected.');
461 $key = $this->getArgument(4);
463 if ($subCmd != 'list' and is_null($key)) {
464 $this->out($this->l10n->t('Enter key: '), false);
465 $key = CliPrompt::prompt();
467 throw new RuntimeException('A key must be selected.');
471 $values = $this->pConfig->load($user['uid'], $category);
475 $table = new Console_Table();
476 $table->setHeaders(['Key', 'Value']);
477 if (array_key_exists($category, $values)) {
478 foreach (array_keys($values[$category]) as $key) {
479 $table->addRow([$key, $values[$category][$key]]);
482 $this->out($table->getTable());
485 if (!array_key_exists($category, $values)) {
486 throw new RuntimeException('Category does not exist');
488 if (!array_key_exists($key, $values[$category])) {
489 throw new RuntimeException('Key does not exist');
492 $this->out($pconfig->get($user['uid'], $category, $key));
495 $value = $this->getArgument(5);
497 if (is_null($value)) {
498 $this->out($this->l10n->t('Enter value: '), false);
499 $value = CliPrompt::prompt();
501 throw new RuntimeException('A value must be specified.');
505 if (array_key_exists($category, $values) and
506 array_key_exists($key, $values[$category]) and
507 $values[$category][$key] == $value) {
508 throw new RuntimeException('Value not changed');
511 $pconfig->set($user['uid'], $category, $key, $value);
514 if (!array_key_exists($category, $values)) {
515 throw new RuntimeException('Category does not exist');
517 if (!array_key_exists($key, $values[$category])) {
518 throw new RuntimeException('Key does not exist');
521 $pconfig->delete($user['uid'], $category, $key);
524 $this->out($this->getHelp());