Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

95 lines
2.4KB

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