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>] [-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]
77 Modify user settings per console commands.
80 -h|--help|-? Show help information
81 -v Show more debug information
82 -y Non-interactive mode, assume "yes" as answer to the user deletion prompt
87 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
89 parent::__construct($argv);
91 $this->appMode = $appMode;
96 protected function doExecute()
98 if ($this->getOption('v')) {
99 $this->out('Class: ' . __CLASS__);
100 $this->out('Arguments: ' . var_export($this->args, true));
101 $this->out('Options: ' . var_export($this->options, true));
104 if (count($this->args) == 0) {
105 $this->out($this->getHelp());
109 if ($this->appMode->isInstall()) {
110 throw new RuntimeException('Database isn\'t ready or populated yet');
113 $command = $this->getArgument(0);
117 return $this->password();
119 return $this->addUser();
121 return $this->pendingUser(true);
123 return $this->pendingUser(false);
125 return $this->blockUser(true);
127 return $this->blockUser(false);
129 return $this->deleteUser();
131 return $this->listUser();
133 return $this->searchUser();
135 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
140 * Sets a new password
142 * @return int Return code of this command
146 private function password()
148 $nick = $this->getArgument(1);
150 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
151 if (!$this->dba->isResult($user)) {
152 throw new RuntimeException($this->l10n->t('User not found'));
155 $password = $this->getArgument(2);
157 if (is_null($password)) {
158 $this->out($this->l10n->t('Enter new password: '), false);
159 $password = CliPrompt::hiddenPrompt(true);
163 $result = UserModel::updatePassword($user['uid'], $password);
165 if (!$this->dba->isResult($result)) {
166 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
169 $this->out($this->l10n->t('Password changed.'));
170 } catch (\Exception $e) {
171 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
178 * Adds a new user based on given console arguments
180 * @return bool True, if the command was successful
181 * @throws \ErrorException
182 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
183 * @throws \ImagickException
185 private function addUser()
187 $name = $this->getArgument(1);
188 $nick = $this->getArgument(2);
189 $email = $this->getArgument(3);
190 $lang = $this->getArgument(4);
193 $this->out($this->l10n->t('Enter user name: '));
194 $name = CliPrompt::prompt();
196 throw new RuntimeException('A name must be set.');
201 $this->out($this->l10n->t('Enter user nickname: '));
202 $nick = CliPrompt::prompt();
204 throw new RuntimeException('A nick name must be set.');
209 $this->out($this->l10n->t('Enter user email address: '));
210 $email = CliPrompt::prompt();
212 throw new RuntimeException('A email address must be set.');
217 $this->out($this->l10n->t('Enter a language (optional): '));
218 $lang = CliPrompt::prompt();
222 return UserModel::createMinimal($name, $email, $nick);
224 return UserModel::createMinimal($name, $email, $nick, $lang);
229 * Allows or denys a user based on it's nickname
231 * @param bool $allow True, if the pending user is allowed, false if denies
233 * @return bool True, if allow was successful
234 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
236 private function pendingUser(bool $allow = true)
238 $nick = $this->getArgument(1);
241 $this->out($this->l10n->t('Enter user nickname: '));
242 $nick = CliPrompt::prompt();
244 throw new RuntimeException('A nick name must be set.');
248 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
250 throw new RuntimeException($this->l10n->t('User not found'));
253 $pending = Register::getPendingForUser($user['uid'] ?? 0);
254 if (empty($pending)) {
255 throw new RuntimeException($this->l10n->t('User is not pending.'));
258 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
262 * Blocks/unblocks a user
264 * @param bool $block True, if the given user should get blocked
266 * @return bool True, if the command was successful
269 private function blockUser(bool $block = true)
271 $nick = $this->getArgument(1);
274 $this->out($this->l10n->t('Enter user nickname: '));
275 $nick = CliPrompt::prompt();
277 throw new RuntimeException('A nick name must be set.');
281 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
283 throw new RuntimeException($this->l10n->t('User not found'));
286 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
292 * @return bool True, if the delete was successful
295 private function deleteUser()
297 $nick = $this->getArgument(1);
300 $this->out($this->l10n->t('Enter user nickname: '));
301 $nick = CliPrompt::prompt();
303 throw new RuntimeException('A nick name must be set.');
307 $user = $this->dba->selectFirst('user', ['uid', 'account_removed'], ['nickname' => $nick]);
309 throw new RuntimeException($this->l10n->t('User not found'));
312 if (!empty($user['account_removed'])) {
313 $this->out($this->l10n->t('User has already been marked for deletion.'));
317 if (!$this->getOption('y')) {
318 $this->out($this->l10n->t('Type "yes" to delete %s', $nick));
319 if (CliPrompt::prompt() !== 'yes') {
320 throw new RuntimeException($this->l10n->t('Deletion aborted.'));
324 return UserModel::remove($user['uid']);
328 * List users of the current node
330 * @return bool True, if the command was successful
332 private function listUser()
334 $subCmd = $this->getArgument(1);
335 $start = $this->getOption(['s', 'start'], 0);
336 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
338 $table = new Console_Table();
342 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
343 $pending = Register::getPending($start, $count);
344 foreach ($pending as $contact) {
350 Temporal::getRelativeDate($contact['created']),
354 $this->out($table->getTable());
359 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
360 $contacts = UserModel::getList($start, $count, $subCmd);
361 foreach ($contacts as $contact) {
367 Temporal::getRelativeDate($contact['created']),
368 Temporal::getRelativeDate($contact['login_date']),
369 Temporal::getRelativeDate($contact['last-item']),
372 $this->out($table->getTable());
375 $this->out($this->getHelp());
381 * Returns a user based on search parameter
383 * @return bool True, if the command was successful
385 private function searchUser()
399 $subCmd = $this->getArgument(1);
400 $param = $this->getArgument(2);
402 $table = new Console_Table();
403 $table->setHeaders(['UID', 'GUID', 'Name', 'Nick', 'E-Mail', 'Register', 'Login', 'Verified', 'Blocked']);
407 $user = UserModel::getById($param, $fields);
410 $user = UserModel::getByGuid($param, $fields);
413 $user = UserModel::getByEmail($param, $fields);
416 $user = UserModel::getByNickname($param, $fields);
419 $this->out($this->getHelp());
423 $table->addRow($user);
424 $this->out($table->getTable());