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\User as UserModel;
28 use Friendica\Model\UserService;
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', '?'];
53 /** @var UserService */
56 protected function getHelp()
59 console user - Modify user settings per console commands.
61 bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
62 bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-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, UserService $userService, array $argv = null)
76 parent::__construct($argv);
78 $this->appMode = $appMode;
81 $this->userService = $userService;
84 protected function doExecute()
86 if ($this->getOption('v')) {
87 $this->out('Class: ' . __CLASS__);
88 $this->out('Arguments: ' . var_export($this->args, true));
89 $this->out('Options: ' . var_export($this->options, true));
92 if (count($this->args) == 0) {
93 $this->out($this->getHelp());
97 if ($this->appMode->isInstall()) {
98 throw new RuntimeException('Database isn\'t ready or populated yet');
101 $command = $this->getArgument(0);
105 return $this->password();
107 return $this->addUser();
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.');
174 $this->out($this->l10n->t('Enter user nickname: '));
175 $nick = CliPrompt::prompt();
177 throw new RuntimeException('A nick name must be set.');
181 $this->out($this->l10n->t('Enter user email address: '));
182 $email = CliPrompt::prompt();
184 throw new RuntimeException('A email address must be set.');
189 $this->out($this->l10n->t('Enter a language (optional): '));
190 $lang = CliPrompt::prompt();
194 return $this->userService->createMinimal($name, $email, $nick);
196 return $this->userService->createMinimal($name, $email, $nick, $lang);