3 namespace Friendica\Console;
6 use Friendica\Core\L10n\L10n;
7 use Friendica\Database\Database;
8 use Friendica\Model\User;
12 * @brief tool to set a new password for a user
14 * With this tool, you can set a new password for a user
16 * License: AGPLv3 or later, same as Friendica
18 * @author Michael Vogel <heluecht@pirati.ca>
20 class NewPassword extends \Asika\SimpleConsole\Console
22 protected $helpOptions = ['h', 'help', '?'];
37 protected function getHelp()
40 console newpassword - Creates a new password for a given user
42 bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
45 Creates a new password for a user without using the "forgot password" functionality.
48 -h|--help|-? Show help information
49 -v Show more debug information.
54 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
56 parent::__construct($argv);
58 $this->appMode = $appMode;
63 protected function doExecute()
65 if ($this->getOption('v')) {
66 $this->out('Class: ' . __CLASS__);
67 $this->out('Arguments: ' . var_export($this->args, true));
68 $this->out('Options: ' . var_export($this->options, true));
71 if (count($this->args) == 0) {
72 $this->out($this->getHelp());
76 if (count($this->args) > 2) {
77 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
80 if ($this->appMode->isInstall()) {
81 throw new RuntimeException('Database isn\'t ready or populated yet');
84 $nick = $this->getArgument(0);
86 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
87 if (!$this->dba->isResult($user)) {
88 throw new RuntimeException($this->l10n->t('User not found'));
91 $password = $this->getArgument(1);
92 if (is_null($password)) {
93 $this->out($this->l10n->t('Enter new password: '), false);
94 $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
98 $result = User::updatePassword($user['uid'], $password);
100 if (!$this->dba->isResult($result)) {
101 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
104 $this->out($this->l10n->t('Password changed.'));
105 } catch (\Exception $e) {
106 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);