Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewPassword.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Friendica\Core\Config;
  4. use Friendica\Core\L10n;
  5. use Friendica\Database\DBA;
  6. use Friendica\Model\User;
  7. use RuntimeException;
  8. /**
  9. * @brief tool to set a new password for a user
  10. *
  11. * With this tool, you can set a new password for a user
  12. *
  13. * License: AGPLv3 or later, same as Friendica
  14. *
  15. * @author Michael Vogel <heluecht@pirati.ca>
  16. */
  17. class NewPassword extends \Asika\SimpleConsole\Console
  18. {
  19. protected $helpOptions = ['h', 'help', '?'];
  20. protected function getHelp()
  21. {
  22. $help = <<<HELP
  23. console newpassword - Creates a new password for a given user
  24. Usage
  25. bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
  26. Description
  27. Creates a new password for a user without using the "forgot password" functionality.
  28. Options
  29. -h|--help|-? Show help information
  30. -v Show more debug information.
  31. HELP;
  32. return $help;
  33. }
  34. protected function doExecute()
  35. {
  36. $a = get_app();
  37. if ($this->getOption('v')) {
  38. $this->out('Class: ' . __CLASS__);
  39. $this->out('Arguments: ' . var_export($this->args, true));
  40. $this->out('Options: ' . var_export($this->options, true));
  41. }
  42. if (count($this->args) == 0) {
  43. $this->out($this->getHelp());
  44. return 0;
  45. }
  46. if (count($this->args) > 2) {
  47. throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
  48. }
  49. if ($a->getMode()->isInstall()) {
  50. throw new RuntimeException('Database isn\'t ready or populated yet');
  51. }
  52. $nick = $this->getArgument(0);
  53. $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nick]);
  54. if (!DBA::isResult($user)) {
  55. throw new RuntimeException(L10n::t('User not found'));
  56. }
  57. $password = $this->getArgument(1);
  58. if (is_null($password)) {
  59. $this->out(L10n::t('Enter new password: '), false);
  60. $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
  61. }
  62. if (!$password) {
  63. throw new RuntimeException(L10n::t('Password can\'t be empty'));
  64. }
  65. if (!Config::get('system', 'disable_password_exposed', false) && User::isPasswordExposed($password)) {
  66. throw new RuntimeException(L10n::t('The new password has been exposed in a public data dump, please choose another.'));
  67. }
  68. if (!User::updatePassword($user['uid'], $password)) {
  69. throw new RuntimeException(L10n::t('Password update failed. Please try again.'));
  70. }
  71. $this->out(L10n::t('Password changed.'));
  72. return 0;
  73. }
  74. }