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]
64 Modify user settings per console commands.
67 -h|--help|-? Show help information
68 -v Show more debug information.
73 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
75 parent::__construct($argv);
77 $this->appMode = $appMode;
82 protected function doExecute()
84 if ($this->getOption('v')) {
85 $this->out('Class: ' . __CLASS__);
86 $this->out('Arguments: ' . var_export($this->args, true));
87 $this->out('Options: ' . var_export($this->options, true));
90 if (count($this->args) == 0) {
91 $this->out($this->getHelp());
95 if ($this->appMode->isInstall()) {
96 throw new RuntimeException('Database isn\'t ready or populated yet');
99 $command = $this->getArgument(0);
103 return $this->password();
105 return $this->addUser();
107 return $this->allowUser();
109 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
114 * Sets a new password
116 * @return int Return code of this command
120 private function password()
122 $nick = $this->getArgument(1);
124 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
125 if (!$this->dba->isResult($user)) {
126 throw new RuntimeException($this->l10n->t('User not found'));
129 $password = $this->getArgument(2);
131 if (is_null($password)) {
132 $this->out($this->l10n->t('Enter new password: '), false);
133 $password = CliPrompt::hiddenPrompt(true);
137 $result = UserModel::updatePassword($user['uid'], $password);
139 if (!$this->dba->isResult($result)) {
140 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
143 $this->out($this->l10n->t('Password changed.'));
144 } catch (\Exception $e) {
145 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
152 * Adds a new user based on given console arguments
154 * @return bool True, if the command was successful
155 * @throws \ErrorException
156 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
157 * @throws \ImagickException
159 private function addUser()
161 $name = $this->getArgument(1);
162 $nick = $this->getArgument(2);
163 $email = $this->getArgument(3);
164 $lang = $this->getArgument(4);
167 $this->out($this->l10n->t('Enter user name: '));
168 $name = CliPrompt::prompt();
170 throw new RuntimeException('A name must be set.');
175 $this->out($this->l10n->t('Enter user nickname: '));
176 $nick = CliPrompt::prompt();
178 throw new RuntimeException('A nick name must be set.');
183 $this->out($this->l10n->t('Enter user email address: '));
184 $email = CliPrompt::prompt();
186 throw new RuntimeException('A email address must be set.');
191 $this->out($this->l10n->t('Enter a language (optional): '));
192 $lang = CliPrompt::prompt();
196 return UserModel::createMinimal($name, $email, $nick);
198 return UserModel::createMinimal($name, $email, $nick, $lang);
203 * Allows a user based on it's nickname
205 * @return bool True, if allow was successful
206 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
208 public function allowUser()
210 $nick = $this->getArgument(1);
213 $this->out($this->l10n->t('Enter user nickname: '));
214 $nick = CliPrompt::prompt();
216 throw new RuntimeException('A nick name must be set.');
220 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
222 throw new RuntimeException($this->l10n->t('User not found'));
225 $pending = Register::getPendingForUser($user['uid'] ?? 0);
226 if (empty($pending)) {
227 throw new RuntimeException($this->l10n->t('User is not pending.'));
230 return UserModel::allow($pending['hash']);