3 namespace Friendica\Core\Console;
5 use Friendica\Core\L10n;
6 use Friendica\Database\DBA;
7 use Friendica\Model\User;
11 * @brief tool to set a new password for a user
13 * With this tool, you can set a new password for a user
15 * License: AGPLv3 or later, same as Friendica
17 * @author Michael Vogel <heluecht@pirati.ca>
19 class NewPassword extends \Asika\SimpleConsole\Console
21 protected $helpOptions = ['h', 'help', '?'];
23 protected function getHelp()
26 console newpassword - Creates a new password for a given user
28 bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
31 Creates a new password for a user without using the "forgot password" functionality.
34 -h|--help|-? Show help information
35 -v Show more debug information.
40 protected function doExecute()
44 if ($this->getOption('v')) {
45 $this->out('Class: ' . __CLASS__);
46 $this->out('Arguments: ' . var_export($this->args, true));
47 $this->out('Options: ' . var_export($this->options, true));
50 if (count($this->args) == 0) {
51 $this->out($this->getHelp());
55 if (count($this->args) > 2) {
56 throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
59 if ($a->getMode()->isInstall()) {
60 throw new RuntimeException('Database isn\'t ready or populated yet');
63 $nick = $this->getArgument(0);
65 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
66 if (!DBA::isResult($user)) {
67 throw new RuntimeException(L10n::t('User not found'));
70 $password = $this->getArgument(1);
71 if (is_null($password)) {
72 $this->out(L10n::t('Enter new password: '), false);
73 $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
77 $result = User::updatePassword($user['uid'], $password);
79 if (!DBA::isResult($result)) {
80 throw new \Exception(L10n::t('Password update failed. Please try again.'));
83 $this->out(L10n::t('Password changed.'));
84 } catch (\Exception $e) {
85 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);