]> git.mxchange.org Git - friendica.git/blob - src/Console/User.php
c12ab33832f7b26dd4ca99b5661edc5ebc4ed378
[friendica.git] / src / Console / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Database\Database;
27 use Friendica\Model\User as UserModel;
28 use RuntimeException;
29
30 /**
31  * tool to set a new password for a user
32  *
33  * With this tool, you can set a new password for a user
34  */
35 class User extends \Asika\SimpleConsole\Console
36 {
37         protected $helpOptions = ['h', 'help', '?'];
38
39         /**
40          * @var App\Mode
41          */
42         private $appMode;
43         /**
44          * @var L10n
45          */
46         private $l10n;
47         /**
48          * @var Database
49          */
50         private $dba;
51
52         protected function getHelp()
53         {
54                 $help = <<<HELP
55 console user - Modify user settings per console commands.
56 Usage
57         bin/console user <nickname> password [<password>] [-h|--help|-?] [-v]
58
59 Description
60         Modify user settings per console commands.
61
62 Options
63     -h|--help|-? Show help information
64     -v           Show more debug information.
65 HELP;
66                 return $help;
67         }
68
69         public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
70         {
71                 parent::__construct($argv);
72
73                 $this->appMode = $appMode;
74                 $this->l10n = $l10n;
75                 $this->dba = $dba;
76         }
77
78         protected function doExecute()
79         {
80                 if ($this->getOption('v')) {
81                         $this->out('Class: ' . __CLASS__);
82                         $this->out('Arguments: ' . var_export($this->args, true));
83                         $this->out('Options: ' . var_export($this->options, true));
84                 }
85
86                 if (count($this->args) == 0) {
87                         $this->out($this->getHelp());
88                         return 0;
89                 }
90
91                 if (count($this->args) < 2) {
92                         throw new \Asika\SimpleConsole\CommandArgsException('Not enough arguments.');
93                 }
94
95                 if ($this->appMode->isInstall()) {
96                         throw new RuntimeException('Database isn\'t ready or populated yet');
97                 }
98
99                 $nick = $this->getArgument(0);
100
101                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
102                 if (!$this->dba->isResult($user)) {
103                         throw new RuntimeException($this->l10n->t('User not found'));
104                 }
105
106                 $command = $this->getArgument(1);
107
108                 switch ($command) {
109                         case 'password':
110                                 return $this->setPassword($user);
111                         default:
112                                 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
113                 }
114         }
115
116         /**
117          * Sets a new password
118          *
119          * @param array $user The user
120          *
121          * @return int Return code of this command
122          */
123         private function setPassword(array $user)
124         {
125                 $password = $this->getArgument(2);
126
127                 if (is_null($password)) {
128                         $this->out($this->l10n->t('Enter new password: '), false);
129                         $password = \Seld\CliPrompt\CliPrompt::hiddenPrompt(true);
130                 }
131
132                 try {
133                         $result = UserModel::updatePassword($user['uid'], $password);
134
135                         if (!$this->dba->isResult($result)) {
136                                 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
137                         }
138
139                         $this->out($this->l10n->t('Password changed.'));
140                 } catch (\Exception $e) {
141                         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
142                 }
143
144                 return 0;
145         }
146 }