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;
32 use Seld\CliPrompt\CliPrompt;
35 * tool to manage users of the current node
37 class User extends \Asika\SimpleConsole\Console
39 protected $helpOptions = ['h', 'help', '?'];
54 protected function getHelp()
57 console user - Modify user settings per console commands.
59 bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
60 bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-h|--help|-?] [-v]
61 bin/console user delete [<nickname>] [-q] [-h|--help|-?] [-v]
62 bin/console user allow [<nickname>] [-h|--help|-?] [-v]
63 bin/console user deny [<nickname>] [-h|--help|-?] [-v]
64 bin/console user block [<nickname>] [-h|--help|-?] [-v]
65 bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
66 bin/console user list pending [start=0 [count=50]] [-h|--help|-?] [-v]
67 bin/console user list all [start=0 [count=50]] [-h|--help|-?] [-v]
70 Modify user settings per console commands.
73 -h|--help|-? Show help information
74 -v Show more debug information.
75 -q Quiet mode (don't ask for a command).
80 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
82 parent::__construct($argv);
84 $this->appMode = $appMode;
89 protected function doExecute()
91 if ($this->getOption('v')) {
92 $this->out('Class: ' . __CLASS__);
93 $this->out('Arguments: ' . var_export($this->args, true));
94 $this->out('Options: ' . var_export($this->options, true));
97 if (count($this->args) == 0) {
98 $this->out($this->getHelp());
102 if ($this->appMode->isInstall()) {
103 throw new RuntimeException('Database isn\'t ready or populated yet');
106 $command = $this->getArgument(0);
110 return $this->password();
112 return $this->addUser();
114 return $this->pendingUser(true);
116 return $this->pendingUser(false);
118 return $this->blockUser(true);
120 return $this->blockUser(false);
122 return $this->deleteUser();
124 return $this->listUser();
126 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
131 * Sets a new password
133 * @return int Return code of this command
137 private function password()
139 $nick = $this->getArgument(1);
141 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
142 if (!$this->dba->isResult($user)) {
143 throw new RuntimeException($this->l10n->t('User not found'));
146 $password = $this->getArgument(2);
148 if (is_null($password)) {
149 $this->out($this->l10n->t('Enter new password: '), false);
150 $password = CliPrompt::hiddenPrompt(true);
154 $result = UserModel::updatePassword($user['uid'], $password);
156 if (!$this->dba->isResult($result)) {
157 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
160 $this->out($this->l10n->t('Password changed.'));
161 } catch (\Exception $e) {
162 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
169 * Adds a new user based on given console arguments
171 * @return bool True, if the command was successful
172 * @throws \ErrorException
173 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
174 * @throws \ImagickException
176 private function addUser()
178 $name = $this->getArgument(1);
179 $nick = $this->getArgument(2);
180 $email = $this->getArgument(3);
181 $lang = $this->getArgument(4);
184 $this->out($this->l10n->t('Enter user name: '));
185 $name = CliPrompt::prompt();
187 throw new RuntimeException('A name must be set.');
192 $this->out($this->l10n->t('Enter user nickname: '));
193 $nick = CliPrompt::prompt();
195 throw new RuntimeException('A nick name must be set.');
200 $this->out($this->l10n->t('Enter user email address: '));
201 $email = CliPrompt::prompt();
203 throw new RuntimeException('A email address must be set.');
208 $this->out($this->l10n->t('Enter a language (optional): '));
209 $lang = CliPrompt::prompt();
213 return UserModel::createMinimal($name, $email, $nick);
215 return UserModel::createMinimal($name, $email, $nick, $lang);
220 * Allows or denys a user based on it's nickname
222 * @param bool $allow True, if the pending user is allowed, false if denies
224 * @return bool True, if allow was successful
225 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
227 private function pendingUser(bool $allow = true)
229 $nick = $this->getArgument(1);
232 $this->out($this->l10n->t('Enter user nickname: '));
233 $nick = CliPrompt::prompt();
235 throw new RuntimeException('A nick name must be set.');
239 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
241 throw new RuntimeException($this->l10n->t('User not found'));
244 $pending = Register::getPendingForUser($user['uid'] ?? 0);
245 if (empty($pending)) {
246 throw new RuntimeException($this->l10n->t('User is not pending.'));
249 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
253 * Blocks/unblocks a user
255 * @param bool $block True, if the given user should get blocked
257 * @return bool True, if the command was successful
260 private function blockUser(bool $block = true)
262 $nick = $this->getArgument(1);
265 $this->out($this->l10n->t('Enter user nickname: '));
266 $nick = CliPrompt::prompt();
268 throw new RuntimeException('A nick name must be set.');
272 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
274 throw new RuntimeException($this->l10n->t('User not found'));
277 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
283 * @return bool True, if the delete was successful
286 private function deleteUser()
288 $nick = $this->getArgument(1);
291 $this->out($this->l10n->t('Enter user nickname: '));
292 $nick = CliPrompt::prompt();
294 throw new RuntimeException('A nick name must be set.');
298 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
300 throw new RuntimeException($this->l10n->t('User not found'));
303 if (!$this->getOption('q')) {
304 $this->out($this->l10n->t('Type "yes" to delete %s', $nick));
305 if (CliPrompt::prompt() !== 'yes') {
306 throw new RuntimeException('Delete abort.');
310 return UserModel::remove($user['uid'] ?? -1);
314 * List user of the current node
316 * @return bool True, if the command was successful
318 private function listUser()
320 $subCmd = $this->getArgument(1);
321 $start = $this->getArgument(2, 0);
322 $count = $this->getArgument(3, Pager::ITEMS_PER_PAGE);
324 $table = new Console_Table();
328 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
329 $pending = Register::getPending($start, $count);
330 foreach ($pending as $contact) {
340 $this->out($table->getTable());
344 $table->setHeaders(['Nick', 'Name', 'URL', 'E-Mail', 'Register Date', 'Comment']);
345 $contacts = UserModel::getUsers($start, $count);
346 foreach ($contacts as $contact) {
356 $this->out($table->getTable());