]> git.mxchange.org Git - friendica.git/blob - src/Console/NewPassword.php
The value is used twice, so use a variable
[friendica.git] / src / Console / NewPassword.php
1 <?php
2
3 namespace Friendica\Console;
4
5 use Friendica\App;
6 use Friendica\Core\L10n\L10n;
7 use Friendica\Database\Database;
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         /**
25          * @var App\Mode
26          */
27         private $appMode;
28         /**
29          * @var L10n
30          */
31         private $l10n;
32         /**
33          * @var Database
34          */
35         private $dba;
36
37         protected function getHelp()
38         {
39                 $help = <<<HELP
40 console newpassword - Creates a new password for a given user
41 Usage
42         bin/console newpassword <nickname> [<password>] [-h|--help|-?] [-v]
43
44 Description
45         Creates a new password for a user without using the "forgot password" functionality.
46
47 Options
48     -h|--help|-? Show help information
49     -v           Show more debug information.
50 HELP;
51                 return $help;
52         }
53
54         public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
55         {
56                 parent::__construct($argv);
57
58                 $this->appMode = $appMode;
59                 $this->l10n = $l10n;
60                 $this->dba = $dba;
61         }
62
63         protected function doExecute()
64         {
65                 if ($this->getOption('v')) {
66                         $this->out('Class: ' . __CLASS__);
67                         $this->out('Arguments: ' . var_export($this->args, true));
68                         $this->out('Options: ' . var_export($this->options, true));
69                 }
70
71                 if (count($this->args) == 0) {
72                         $this->out($this->getHelp());
73                         return 0;
74                 }
75
76                 if (count($this->args) > 2) {
77                         throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
78                 }
79
80                 if ($this->appMode->isInstall()) {
81                         throw new RuntimeException('Database isn\'t ready or populated yet');
82                 }
83
84                 $nick = $this->getArgument(0);
85
86                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
87                 if (!$this->dba->isResult($user)) {
88                         throw new RuntimeException($this->l10n->t('User not found'));
89                 }
90
91                 $password = $this->getArgument(1);
92                 if (is_null($password)) {
93                         $this->out($this->l10n->t('Enter new password: '), false);
94                         $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
95                 }
96
97                 try {
98                         $result = User::updatePassword($user['uid'], $password);
99
100                         if (!$this->dba->isResult($result)) {
101                                 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
102                         }
103
104                         $this->out($this->l10n->t('Password changed.'));
105                 } catch (\Exception $e) {
106                         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
107                 }
108
109                 return 0;
110         }
111 }