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;
25 use Friendica\Core\L10n;
26 use Friendica\Database\Database;
27 use Friendica\Model\Register;
28 use Friendica\Model\User as UserModel;
30 use Seld\CliPrompt\CliPrompt;
33 * tool to set a new password for a user
35 * With this tool, you can set a new password for a user
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 allow [<nickname>] [-h|--help|-?] [-v]
62 bin/console user deny [<nickname>] [-h|--help|-?] [-v]
63 bin/console user block [<nickname>] [-h|--help|-?] [-v]
64 bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
67 Modify user settings per console commands.
70 -h|--help|-? Show help information
71 -v Show more debug information.
76 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
78 parent::__construct($argv);
80 $this->appMode = $appMode;
85 protected function doExecute()
87 if ($this->getOption('v')) {
88 $this->out('Class: ' . __CLASS__);
89 $this->out('Arguments: ' . var_export($this->args, true));
90 $this->out('Options: ' . var_export($this->options, true));
93 if (count($this->args) == 0) {
94 $this->out($this->getHelp());
98 if ($this->appMode->isInstall()) {
99 throw new RuntimeException('Database isn\'t ready or populated yet');
102 $command = $this->getArgument(0);
106 return $this->password();
108 return $this->addUser();
110 return $this->pendingUser(true);
112 return $this->pendingUser(false);
114 return $this->blockUser(true);
116 return $this->blockUser(false);
118 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
123 * Sets a new password
125 * @return int Return code of this command
129 private function password()
131 $nick = $this->getArgument(1);
133 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
134 if (!$this->dba->isResult($user)) {
135 throw new RuntimeException($this->l10n->t('User not found'));
138 $password = $this->getArgument(2);
140 if (is_null($password)) {
141 $this->out($this->l10n->t('Enter new password: '), false);
142 $password = CliPrompt::hiddenPrompt(true);
146 $result = UserModel::updatePassword($user['uid'], $password);
148 if (!$this->dba->isResult($result)) {
149 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
152 $this->out($this->l10n->t('Password changed.'));
153 } catch (\Exception $e) {
154 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
161 * Adds a new user based on given console arguments
163 * @return bool True, if the command was successful
164 * @throws \ErrorException
165 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
166 * @throws \ImagickException
168 private function addUser()
170 $name = $this->getArgument(1);
171 $nick = $this->getArgument(2);
172 $email = $this->getArgument(3);
173 $lang = $this->getArgument(4);
176 $this->out($this->l10n->t('Enter user name: '));
177 $name = CliPrompt::prompt();
179 throw new RuntimeException('A name must be set.');
184 $this->out($this->l10n->t('Enter user nickname: '));
185 $nick = CliPrompt::prompt();
187 throw new RuntimeException('A nick name must be set.');
192 $this->out($this->l10n->t('Enter user email address: '));
193 $email = CliPrompt::prompt();
195 throw new RuntimeException('A email address must be set.');
200 $this->out($this->l10n->t('Enter a language (optional): '));
201 $lang = CliPrompt::prompt();
205 return UserModel::createMinimal($name, $email, $nick);
207 return UserModel::createMinimal($name, $email, $nick, $lang);
212 * Allows or denys a user based on it's nickname
214 * @param bool $allow True, if the pending user is allowed, false if denies
216 * @return bool True, if allow was successful
217 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
219 private function pendingUser(bool $allow = true)
221 $nick = $this->getArgument(1);
224 $this->out($this->l10n->t('Enter user nickname: '));
225 $nick = CliPrompt::prompt();
227 throw new RuntimeException('A nick name must be set.');
231 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
233 throw new RuntimeException($this->l10n->t('User not found'));
236 $pending = Register::getPendingForUser($user['uid'] ?? 0);
237 if (empty($pending)) {
238 throw new RuntimeException($this->l10n->t('User is not pending.'));
241 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
245 * Blocks/unblocks a user
247 * @param bool $block True, if the given user should get blocked
249 * @return bool True, if the command was successful
252 private function blockUser(bool $block = true)
254 $nick = $this->getArgument(1);
257 $this->out($this->l10n->t('Enter user nickname: '));
258 $nick = CliPrompt::prompt();
260 throw new RuntimeException('A nick name must be set.');
264 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
266 throw new RuntimeException($this->l10n->t('User not found'));
269 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);