3 namespace Friendica\Core\Console;
5 use Friendica\Core\L10n;
6 use Friendica\Model\Contact;
7 use Friendica\Model\User;
8 use Friendica\Core\Config;
9 use Friendica\Database\DBM;
13 * @brief tool to set a new password for a user
15 * With this tool, you can set a new password for a user
17 * License: AGPLv3 or later, same as Friendica
19 * @author Michael Vogel <heluecht@pirati.ca>
21 class NewPassword extends \Asika\SimpleConsole\Console
23 protected $helpOptions = ['h', 'help', '?'];
25 protected function getHelp()
28 console newpassword - Creates a new password for a given user
30 bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
33 Creates a new password for a user without using the "forgot password" functionality.
36 -h|--help|-? Show help information
37 -v Show more debug information.
42 protected function doExecute()
46 if ($this->getOption('v')) {
47 $this->out('Class: ' . __CLASS__);
48 $this->out('Arguments: ' . var_export($this->args, true));
49 $this->out('Options: ' . var_export($this->options, true));
52 if (count($this->args) == 0) {
53 $this->out($this->getHelp());
57 if (count($this->args) > 2) {
58 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
61 require_once '.htconfig.php';
62 $result = \dba::connect($db_host, $db_user, $db_pass, $db_data);
63 unset($db_host, $db_user, $db_pass, $db_data);
66 throw new \RuntimeException('Unable to connect to database');
69 $nick = $this->getArgument(0);
71 $user = dba::selectFirst('user', ['uid'], ['nickname' => $nick]);
72 if (!DBM::is_result($user)) {
73 throw new \RuntimeException(L10n::t('User not found'));
76 $password = $this->getArgument(1);
77 if (is_null($password)) {
78 $this->out(L10n::t('Enter new password: '), false);
79 $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
83 throw new \RuntimeException(L10n::t('Password can\'t be empty'));
86 if (!Config::get('system', 'disable_password_exposed', false) && User::isPasswordExposed($password)) {
87 throw new \RuntimeException(L10n::t('The new password has been exposed in a public data dump, please choose another.'));
90 if (!User::updatePassword($user['uid'], $password)) {
91 throw new \RuntimeException(L10n::t('Password update failed. Please try again.'));
94 $this->out(L10n::t('Password changed.'));