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]
65 Modify user settings per console commands.
68 -h|--help|-? Show help information
69 -v Show more debug information.
74 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
76 parent::__construct($argv);
78 $this->appMode = $appMode;
83 protected function doExecute()
85 if ($this->getOption('v')) {
86 $this->out('Class: ' . __CLASS__);
87 $this->out('Arguments: ' . var_export($this->args, true));
88 $this->out('Options: ' . var_export($this->options, true));
91 if (count($this->args) == 0) {
92 $this->out($this->getHelp());
96 if ($this->appMode->isInstall()) {
97 throw new RuntimeException('Database isn\'t ready or populated yet');
100 $command = $this->getArgument(0);
104 return $this->password();
106 return $this->addUser();
108 return $this->pendingUser(true);
110 return $this->pendingUser(false);
112 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
117 * Sets a new password
119 * @return int Return code of this command
123 private function password()
125 $nick = $this->getArgument(1);
127 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
128 if (!$this->dba->isResult($user)) {
129 throw new RuntimeException($this->l10n->t('User not found'));
132 $password = $this->getArgument(2);
134 if (is_null($password)) {
135 $this->out($this->l10n->t('Enter new password: '), false);
136 $password = CliPrompt::hiddenPrompt(true);
140 $result = UserModel::updatePassword($user['uid'], $password);
142 if (!$this->dba->isResult($result)) {
143 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
146 $this->out($this->l10n->t('Password changed.'));
147 } catch (\Exception $e) {
148 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
155 * Adds a new user based on given console arguments
157 * @return bool True, if the command was successful
158 * @throws \ErrorException
159 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160 * @throws \ImagickException
162 private function addUser()
164 $name = $this->getArgument(1);
165 $nick = $this->getArgument(2);
166 $email = $this->getArgument(3);
167 $lang = $this->getArgument(4);
170 $this->out($this->l10n->t('Enter user name: '));
171 $name = CliPrompt::prompt();
173 throw new RuntimeException('A name must be set.');
178 $this->out($this->l10n->t('Enter user nickname: '));
179 $nick = CliPrompt::prompt();
181 throw new RuntimeException('A nick name must be set.');
186 $this->out($this->l10n->t('Enter user email address: '));
187 $email = CliPrompt::prompt();
189 throw new RuntimeException('A email address must be set.');
194 $this->out($this->l10n->t('Enter a language (optional): '));
195 $lang = CliPrompt::prompt();
199 return UserModel::createMinimal($name, $email, $nick);
201 return UserModel::createMinimal($name, $email, $nick, $lang);
206 * Allows or denys a user based on it's nickname
208 * @param bool $allow True, if the pending user is allowed, false if denies
210 * @return bool True, if allow was successful
211 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
213 public function pendingUser(bool $allow = true)
215 $nick = $this->getArgument(1);
218 $this->out($this->l10n->t('Enter user nickname: '));
219 $nick = CliPrompt::prompt();
221 throw new RuntimeException('A nick name must be set.');
225 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
227 throw new RuntimeException($this->l10n->t('User not found'));
230 $pending = Register::getPendingForUser($user['uid'] ?? 0);
231 if (empty($pending)) {
232 throw new RuntimeException($this->l10n->t('User is not pending.'));
235 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);