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;
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 NewPassword extends \Asika\SimpleConsole\Console
37 protected $helpOptions = ['h', 'help', '?'];
52 protected function getHelp()
55 console newpassword - Creates a new password for a given user
57 bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
60 Creates a new password for a user without using the "forgot password" functionality.
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('Too many 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 $password = $this->getArgument(1);
107 if (is_null($password)) {
108 $this->out($this->l10n->t('Enter new password: '), false);
109 $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
113 $result = User::updatePassword($user['uid'], $password);
115 if (!$this->dba->isResult($result)) {
116 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
119 $this->out($this->l10n->t('Password changed.'));
120 } catch (\Exception $e) {
121 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);