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\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 * Retrieves the user nick, either as an argument or from a prompt
142 * @param int $arg_index Index of the nick argument in the arguments list
144 * @return string nick of the user
145 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
147 private function getNick($arg_index)
149 $nick = $this->getArgument($arg_index);
152 $this->out($this->l10n->t('Enter user nickname: '));
153 $nick = CliPrompt::prompt();
155 throw new RuntimeException('A nick name must be set.');
163 * Retrieves the user from a nick supplied as an argument or from a prompt
165 * @param int $arg_index Index of the nick argument in the arguments list
167 * @return mixed user data or dba failure result
168 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
170 private function getUserByNick($arg_index)
172 $nick = $this->getNick($arg_index);
174 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
175 if (!$this->dba->isResult($user)) {
176 throw new RuntimeException($this->l10n->t('User not found'));
183 * Sets a new password
185 * @return int Return code of this command
189 private function password()
191 $user = $this->getUserByNick(1);
193 $password = $this->getArgument(2);
195 if (is_null($password)) {
196 $this->out($this->l10n->t('Enter new password: '), false);
197 $password = CliPrompt::hiddenPrompt(true);
201 $result = UserModel::updatePassword($user['uid'], $password);
203 if (!$this->dba->isResult($result)) {
204 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
207 $this->out($this->l10n->t('Password changed.'));
208 } catch (\Exception $e) {
209 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
216 * Adds a new user based on given console arguments
218 * @return bool True, if the command was successful
219 * @throws \ErrorException
220 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
221 * @throws \ImagickException
223 private function addUser()
225 $name = $this->getArgument(1);
226 $nick = $this->getArgument(2);
227 $email = $this->getArgument(3);
228 $lang = $this->getArgument(4);
231 $this->out($this->l10n->t('Enter user name: '));
232 $name = CliPrompt::prompt();
234 throw new RuntimeException('A name must be set.');
239 $this->out($this->l10n->t('Enter user nickname: '));
240 $nick = CliPrompt::prompt();
242 throw new RuntimeException('A nick name must be set.');
247 $this->out($this->l10n->t('Enter user email address: '));
248 $email = CliPrompt::prompt();
250 throw new RuntimeException('A email address must be set.');
255 $this->out($this->l10n->t('Enter a language (optional): '));
256 $lang = CliPrompt::prompt();
260 return UserModel::createMinimal($name, $email, $nick);
262 return UserModel::createMinimal($name, $email, $nick, $lang);
267 * Allows or denys a user based on it's nickname
269 * @param bool $allow True, if the pending user is allowed, false if denies
271 * @return bool True, if allow was successful
272 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
274 private function pendingUser(bool $allow = true)
276 $user = $this->getUserByNick(1);
278 $pending = Register::getPendingForUser($user['uid'] ?? 0);
279 if (empty($pending)) {
280 throw new RuntimeException($this->l10n->t('User is not pending.'));
283 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
287 * Blocks/unblocks a user
289 * @param bool $block True, if the given user should get blocked
291 * @return bool True, if the command was successful
294 private function blockUser(bool $block = true)
296 $user = $this->getUserByNick(1);
298 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
304 * @return bool True, if the delete was successful
307 private function deleteUser()
309 $user = $this->getUserByNick(1);
311 if (!empty($user['account_removed'])) {
312 $this->out($this->l10n->t('User has already been marked for deletion.'));
316 if (!$this->getOption('y')) {
317 $this->out($this->l10n->t('Type "yes" to delete %s', $this->getArgument(1)));
318 if (CliPrompt::prompt() !== 'yes') {
319 throw new RuntimeException($this->l10n->t('Deletion aborted.'));
323 return UserModel::remove($user['uid']);
327 * List users of the current node
329 * @return bool True, if the command was successful
331 private function listUser()
333 $subCmd = $this->getArgument(1);
334 $start = $this->getOption(['s', 'start'], 0);
335 $count = $this->getOption(['c', 'count'], Pager::ITEMS_PER_PAGE);
337 $table = new Console_Table();
341 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
342 $pending = Register::getPending($start, $count);
343 foreach ($pending as $contact) {
349 Temporal::getRelativeDate($contact['created']),
353 $this->out($table->getTable());
358 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
359 $contacts = UserModel::getList($start, $count, $subCmd);
360 foreach ($contacts as $contact) {
366 Temporal::getRelativeDate($contact['created']),
367 Temporal::getRelativeDate($contact['login_date']),
368 Temporal::getRelativeDate($contact['last-item']),
371 $this->out($table->getTable());
374 $this->out($this->getHelp());
380 * Returns a user based on search parameter
382 * @return bool True, if the command was successful
384 private function searchUser()
398 $subCmd = $this->getArgument(1);
399 $param = $this->getArgument(2);
401 $table = new Console_Table();
402 $table->setHeaders(['UID', 'GUID', 'Name', 'Nick', 'E-Mail', 'Register', 'Login', 'Verified', 'Blocked']);
406 $user = UserModel::getById($param, $fields);
409 $user = UserModel::getByGuid($param, $fields);
412 $user = UserModel::getByEmail($param, $fields);
415 $user = UserModel::getByNickname($param, $fields);
418 $this->out($this->getHelp());
422 $table->addRow($user);
423 $this->out($table->getTable());