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;
31 * tool to set a new password for a user
33 * With this tool, you can set a new password for a user
35 class User extends \Asika\SimpleConsole\Console
37 protected $helpOptions = ['h', 'help', '?'];
52 protected function getHelp()
55 console user - Modify user settings per console commands.
57 bin/console user <nickname> password [<password>] [-h|--help|-?] [-v]
60 Modify user settings per console commands.
63 -h|--help|-? Show help information
64 -v Show more debug information.
69 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
71 parent::__construct($argv);
73 $this->appMode = $appMode;
78 protected function doExecute()
80 if ($this->getOption('v')) {
81 $this->out('Class: ' . __CLASS__);
82 $this->out('Arguments: ' . var_export($this->args, true));
83 $this->out('Options: ' . var_export($this->options, true));
86 if (count($this->args) == 0) {
87 $this->out($this->getHelp());
91 if (count($this->args) < 2) {
92 throw new \Asika\SimpleConsole\CommandArgsException('Not enough arguments.');
95 if ($this->appMode->isInstall()) {
96 throw new RuntimeException('Database isn\'t ready or populated yet');
99 $nick = $this->getArgument(0);
101 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
102 if (!$this->dba->isResult($user)) {
103 throw new RuntimeException($this->l10n->t('User not found'));
106 $command = $this->getArgument(1);
110 return $this->setPassword($user);
112 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
117 * Sets a new password
119 * @param array $user The user
121 * @return int Return code of this command
123 private function setPassword(array $user)
125 $password = $this->getArgument(2);
127 if (is_null($password)) {
128 $this->out($this->l10n->t('Enter new password: '), false);
129 $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
133 $result = UserModel::updatePassword($user['uid'], $password);
135 if (!$this->dba->isResult($result)) {
136 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
139 $this->out($this->l10n->t('Password changed.'));
140 } catch (\Exception $e) {
141 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);