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