3 * @copyright Copyright (C) 2020, Friendica
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\Database\Database;
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', '?'];
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>] [-q] [-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 [start=0 [count=50]] [-h|--help|-?] [-v]
68 bin/console user list removed [start=0 [count=50]] [-h|--help|-?] [-v]
69 bin/console user list all [start=0 [count=50]] [-h|--help|-?] [-v]
70 bin/console user search id <UID> [-h|--help|-?] [-v]
71 bin/console user search nick <nick> [-h|--help|-?] [-v]
72 bin/console user search mail <mail> [-h|--help|-?] [-v]
73 bin/console user search guid <GUID> [-h|--help|-?] [-v]
76 Modify user settings per console commands.
79 -h|--help|-? Show help information
80 -v Show more debug information.
81 -q Quiet mode (don't ask for a command).
86 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
88 parent::__construct($argv);
90 $this->appMode = $appMode;
95 protected function doExecute()
97 if ($this->getOption('v')) {
98 $this->out('Class: ' . __CLASS__);
99 $this->out('Arguments: ' . var_export($this->args, true));
100 $this->out('Options: ' . var_export($this->options, true));
103 if (count($this->args) == 0) {
104 $this->out($this->getHelp());
108 if ($this->appMode->isInstall()) {
109 throw new RuntimeException('Database isn\'t ready or populated yet');
112 $command = $this->getArgument(0);
116 return $this->password();
118 return $this->addUser();
120 return $this->pendingUser(true);
122 return $this->pendingUser(false);
124 return $this->blockUser(true);
126 return $this->blockUser(false);
128 return $this->deleteUser();
130 return $this->listUser();
132 return $this->searchUser();
134 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
139 * Sets a new password
141 * @return int Return code of this command
145 private function password()
147 $nick = $this->getArgument(1);
149 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
150 if (!$this->dba->isResult($user)) {
151 throw new RuntimeException($this->l10n->t('User not found'));
154 $password = $this->getArgument(2);
156 if (is_null($password)) {
157 $this->out($this->l10n->t('Enter new password: '), false);
158 $password = CliPrompt::hiddenPrompt(true);
162 $result = UserModel::updatePassword($user['uid'], $password);
164 if (!$this->dba->isResult($result)) {
165 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
168 $this->out($this->l10n->t('Password changed.'));
169 } catch (\Exception $e) {
170 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
177 * Adds a new user based on given console arguments
179 * @return bool True, if the command was successful
180 * @throws \ErrorException
181 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
182 * @throws \ImagickException
184 private function addUser()
186 $name = $this->getArgument(1);
187 $nick = $this->getArgument(2);
188 $email = $this->getArgument(3);
189 $lang = $this->getArgument(4);
192 $this->out($this->l10n->t('Enter user name: '));
193 $name = CliPrompt::prompt();
195 throw new RuntimeException('A name must be set.');
200 $this->out($this->l10n->t('Enter user nickname: '));
201 $nick = CliPrompt::prompt();
203 throw new RuntimeException('A nick name must be set.');
208 $this->out($this->l10n->t('Enter user email address: '));
209 $email = CliPrompt::prompt();
211 throw new RuntimeException('A email address must be set.');
216 $this->out($this->l10n->t('Enter a language (optional): '));
217 $lang = CliPrompt::prompt();
221 return UserModel::createMinimal($name, $email, $nick);
223 return UserModel::createMinimal($name, $email, $nick, $lang);
228 * Allows or denys a user based on it's nickname
230 * @param bool $allow True, if the pending user is allowed, false if denies
232 * @return bool True, if allow was successful
233 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
235 private function pendingUser(bool $allow = true)
237 $nick = $this->getArgument(1);
240 $this->out($this->l10n->t('Enter user nickname: '));
241 $nick = CliPrompt::prompt();
243 throw new RuntimeException('A nick name must be set.');
247 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
249 throw new RuntimeException($this->l10n->t('User not found'));
252 $pending = Register::getPendingForUser($user['uid'] ?? 0);
253 if (empty($pending)) {
254 throw new RuntimeException($this->l10n->t('User is not pending.'));
257 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
261 * Blocks/unblocks a user
263 * @param bool $block True, if the given user should get blocked
265 * @return bool True, if the command was successful
268 private function blockUser(bool $block = true)
270 $nick = $this->getArgument(1);
273 $this->out($this->l10n->t('Enter user nickname: '));
274 $nick = CliPrompt::prompt();
276 throw new RuntimeException('A nick name must be set.');
280 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
282 throw new RuntimeException($this->l10n->t('User not found'));
285 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
291 * @return bool True, if the delete was successful
294 private function deleteUser()
296 $nick = $this->getArgument(1);
299 $this->out($this->l10n->t('Enter user nickname: '));
300 $nick = CliPrompt::prompt();
302 throw new RuntimeException('A nick name must be set.');
306 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
308 throw new RuntimeException($this->l10n->t('User not found'));
311 if (!$this->getOption('q')) {
312 $this->out($this->l10n->t('Type "yes" to delete %s', $nick));
313 if (CliPrompt::prompt() !== 'yes') {
314 throw new RuntimeException('Delete abort.');
318 return UserModel::remove($user['uid'] ?? -1);
322 * List users of the current node
324 * @return bool True, if the command was successful
326 private function listUser()
328 $subCmd = $this->getArgument(1);
329 $start = $this->getArgument(2, 0);
330 $count = $this->getArgument(3, Pager::ITEMS_PER_PAGE);
332 $table = new Console_Table();
336 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
337 $pending = Register::getPending($start, $count);
338 foreach ($pending as $contact) {
344 Temporal::getRelativeDate($contact['created']),
348 $this->out($table->getTable());
352 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
353 $contacts = UserModel::getUsers($start, $count);
354 foreach ($contacts as $contact) {
355 if (($subCmd != 'removed') && !empty($contact['account_removed']) ||
356 ($subCmd == 'removed') && empty($contact['account_removed'])) {
365 Temporal::getRelativeDate($contact['created']),
366 Temporal::getRelativeDate($contact['login_date']),
367 Temporal::getRelativeDate($contact['lastitem_date']),
370 $this->out($table->getTable());
373 $this->out($this->getHelp());
379 * Returns a user based on search parameter
381 * @return bool True, if the command was successful
383 private function searchUser()
397 $subCmd = $this->getArgument(1);
398 $param = $this->getArgument(2);
400 $table = new Console_Table();
401 $table->setHeaders(['UID', 'GUID', 'Name', 'Nick', 'E-Mail', 'Register', 'Login', 'Verified', 'Blocked']);
405 $user = UserModel::getById($param, $fields);
408 $user = UserModel::getByGuid($param, $fields);
411 $user = UserModel::getByEmail($param, $fields);
414 $user = UserModel::getByNickname($param, $fields);
417 $this->out($this->getHelp());
421 $table->addRow($user);
422 $this->out($table->getTable());