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]
72 Modify user settings per console commands.
75 -h|--help|-? Show help information
76 -v Show more debug information.
77 -q Quiet mode (don't ask for a command).
82 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
84 parent::__construct($argv);
86 $this->appMode = $appMode;
91 protected function doExecute()
93 if ($this->getOption('v')) {
94 $this->out('Class: ' . __CLASS__);
95 $this->out('Arguments: ' . var_export($this->args, true));
96 $this->out('Options: ' . var_export($this->options, true));
99 if (count($this->args) == 0) {
100 $this->out($this->getHelp());
104 if ($this->appMode->isInstall()) {
105 throw new RuntimeException('Database isn\'t ready or populated yet');
108 $command = $this->getArgument(0);
112 return $this->password();
114 return $this->addUser();
116 return $this->pendingUser(true);
118 return $this->pendingUser(false);
120 return $this->blockUser(true);
122 return $this->blockUser(false);
124 return $this->deleteUser();
126 return $this->listUser();
128 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
133 * Sets a new password
135 * @return int Return code of this command
139 private function password()
141 $nick = $this->getArgument(1);
143 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
144 if (!$this->dba->isResult($user)) {
145 throw new RuntimeException($this->l10n->t('User not found'));
148 $password = $this->getArgument(2);
150 if (is_null($password)) {
151 $this->out($this->l10n->t('Enter new password: '), false);
152 $password = CliPrompt::hiddenPrompt(true);
156 $result = UserModel::updatePassword($user['uid'], $password);
158 if (!$this->dba->isResult($result)) {
159 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
162 $this->out($this->l10n->t('Password changed.'));
163 } catch (\Exception $e) {
164 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
171 * Adds a new user based on given console arguments
173 * @return bool True, if the command was successful
174 * @throws \ErrorException
175 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
176 * @throws \ImagickException
178 private function addUser()
180 $name = $this->getArgument(1);
181 $nick = $this->getArgument(2);
182 $email = $this->getArgument(3);
183 $lang = $this->getArgument(4);
186 $this->out($this->l10n->t('Enter user name: '));
187 $name = CliPrompt::prompt();
189 throw new RuntimeException('A name must be set.');
194 $this->out($this->l10n->t('Enter user nickname: '));
195 $nick = CliPrompt::prompt();
197 throw new RuntimeException('A nick name must be set.');
202 $this->out($this->l10n->t('Enter user email address: '));
203 $email = CliPrompt::prompt();
205 throw new RuntimeException('A email address must be set.');
210 $this->out($this->l10n->t('Enter a language (optional): '));
211 $lang = CliPrompt::prompt();
215 return UserModel::createMinimal($name, $email, $nick);
217 return UserModel::createMinimal($name, $email, $nick, $lang);
222 * Allows or denys a user based on it's nickname
224 * @param bool $allow True, if the pending user is allowed, false if denies
226 * @return bool True, if allow was successful
227 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
229 private function pendingUser(bool $allow = true)
231 $nick = $this->getArgument(1);
234 $this->out($this->l10n->t('Enter user nickname: '));
235 $nick = CliPrompt::prompt();
237 throw new RuntimeException('A nick name must be set.');
241 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
243 throw new RuntimeException($this->l10n->t('User not found'));
246 $pending = Register::getPendingForUser($user['uid'] ?? 0);
247 if (empty($pending)) {
248 throw new RuntimeException($this->l10n->t('User is not pending.'));
251 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
255 * Blocks/unblocks a user
257 * @param bool $block True, if the given user should get blocked
259 * @return bool True, if the command was successful
262 private function blockUser(bool $block = true)
264 $nick = $this->getArgument(1);
267 $this->out($this->l10n->t('Enter user nickname: '));
268 $nick = CliPrompt::prompt();
270 throw new RuntimeException('A nick name must be set.');
274 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
276 throw new RuntimeException($this->l10n->t('User not found'));
279 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
285 * @return bool True, if the delete was successful
288 private function deleteUser()
290 $nick = $this->getArgument(1);
293 $this->out($this->l10n->t('Enter user nickname: '));
294 $nick = CliPrompt::prompt();
296 throw new RuntimeException('A nick name must be set.');
300 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
302 throw new RuntimeException($this->l10n->t('User not found'));
305 if (!$this->getOption('q')) {
306 $this->out($this->l10n->t('Type "yes" to delete %s', $nick));
307 if (CliPrompt::prompt() !== 'yes') {
308 throw new RuntimeException('Delete abort.');
312 return UserModel::remove($user['uid'] ?? -1);
316 * List user of the current node
318 * @return bool True, if the command was successful
320 private function listUser()
322 $subCmd = $this->getArgument(1);
323 $start = $this->getArgument(2, 0);
324 $count = $this->getArgument(3, Pager::ITEMS_PER_PAGE);
326 $table = new Console_Table();
330 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
331 $pending = Register::getPending($start, $count);
332 foreach ($pending as $contact) {
338 Temporal::getRelativeDate($contact['created']),
342 $this->out($table->getTable());
347 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register', 'Login', 'Last Item']);
348 $contacts = UserModel::getUsers($start, $count);
349 foreach ($contacts as $contact) {
350 if (($subCmd != 'removed') && !empty($contact['account_removed']) ||
351 ($subCmd == 'removed') && empty($contact['account_removed'])) {
360 Temporal::getRelativeDate($contact['created']),
361 Temporal::getRelativeDate($contact['login_date']),
362 Temporal::getRelativeDate($contact['lastitem_date']),
365 $this->out($table->getTable());