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