3 namespace Friendica\Core\Console;
5 use Friendica\Core\Config;
6 use Friendica\Core\L10n;
7 use Friendica\Database\DBA;
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', '?'];
24 protected function getHelp()
27 console newpassword - Creates a new password for a given user
29 bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
32 Creates a new password for a user without using the "forgot password" functionality.
35 -h|--help|-? Show help information
36 -v Show more debug information.
41 protected function doExecute()
45 if ($this->getOption('v')) {
46 $this->out('Class: ' . __CLASS__);
47 $this->out('Arguments: ' . var_export($this->args, true));
48 $this->out('Options: ' . var_export($this->options, true));
51 if (count($this->args) == 0) {
52 $this->out($this->getHelp());
56 if (count($this->args) > 2) {
57 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
60 if ($a->isInstallMode()) {
61 throw new RuntimeException('Database isn\'t ready or populated yet');
64 $nick = $this->getArgument(0);
66 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
67 if (!DBA::isResult($user)) {
68 throw new RuntimeException(L10n::t('User not found'));
71 $password = $this->getArgument(1);
72 if (is_null($password)) {
73 $this->out(L10n::t('Enter new password: '), false);
74 $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
78 throw new RuntimeException(L10n::t('Password can\'t be empty'));
81 if (!Config::get('system', 'disable_password_exposed', false) && User::isPasswordExposed($password)) {
82 throw new RuntimeException(L10n::t('The new password has been exposed in a public data dump, please choose another.'));
85 if (!User::updatePassword($user['uid'], $password)) {
86 throw new RuntimeException(L10n::t('Password update failed. Please try again.'));
89 $this->out(L10n::t('Password changed.'));