]> git.mxchange.org Git - friendica.git/blob - src/Console/User.php
85aad7edd802a8ecda9cf4a9ea3c4aba934d3261
[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\Register;
28 use Friendica\Model\User as UserModel;
29 use RuntimeException;
30 use Seld\CliPrompt\CliPrompt;
31
32 /**
33  * tool to set a new password for a user
34  *
35  * With this tool, you can set a new password for a user
36  */
37 class User extends \Asika\SimpleConsole\Console
38 {
39         protected $helpOptions = ['h', 'help', '?'];
40
41         /**
42          * @var App\Mode
43          */
44         private $appMode;
45         /**
46          * @var L10n
47          */
48         private $l10n;
49         /**
50          * @var Database
51          */
52         private $dba;
53
54         protected function getHelp()
55         {
56                 $help = <<<HELP
57 console user - Modify user settings per console commands.
58 Usage
59         bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
60         bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-h|--help|-?] [-v]
61         bin/console user allow [<nickname>] [-h|--help|-?] [-v]
62         bin/console user deny [<nickname>] [-h|--help|-?] [-v]
63
64 Description
65         Modify user settings per console commands.
66
67 Options
68     -h|--help|-? Show help information
69     -v           Show more debug information.
70 HELP;
71                 return $help;
72         }
73
74         public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
75         {
76                 parent::__construct($argv);
77
78                 $this->appMode     = $appMode;
79                 $this->l10n        = $l10n;
80                 $this->dba         = $dba;
81         }
82
83         protected function doExecute()
84         {
85                 if ($this->getOption('v')) {
86                         $this->out('Class: ' . __CLASS__);
87                         $this->out('Arguments: ' . var_export($this->args, true));
88                         $this->out('Options: ' . var_export($this->options, true));
89                 }
90
91                 if (count($this->args) == 0) {
92                         $this->out($this->getHelp());
93                         return 0;
94                 }
95
96                 if ($this->appMode->isInstall()) {
97                         throw new RuntimeException('Database isn\'t ready or populated yet');
98                 }
99
100                 $command = $this->getArgument(0);
101
102                 switch ($command) {
103                         case 'password':
104                                 return $this->password();
105                         case 'add':
106                                 return $this->addUser();
107                         case 'allow':
108                                 return $this->pendingUser(true);
109                         case 'deny':
110                                 return $this->pendingUser(false);
111                         default:
112                                 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
113                 }
114         }
115
116         /**
117          * Sets a new password
118          *
119          * @return int Return code of this command
120          *
121          * @throws \Exception
122          */
123         private function password()
124         {
125                 $nick = $this->getArgument(1);
126
127                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
128                 if (!$this->dba->isResult($user)) {
129                         throw new RuntimeException($this->l10n->t('User not found'));
130                 }
131
132                 $password = $this->getArgument(2);
133
134                 if (is_null($password)) {
135                         $this->out($this->l10n->t('Enter new password: '), false);
136                         $password = CliPrompt::hiddenPrompt(true);
137                 }
138
139                 try {
140                         $result = UserModel::updatePassword($user['uid'], $password);
141
142                         if (!$this->dba->isResult($result)) {
143                                 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
144                         }
145
146                         $this->out($this->l10n->t('Password changed.'));
147                 } catch (\Exception $e) {
148                         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
149                 }
150
151                 return 0;
152         }
153
154         /**
155          * Adds a new user based on given console arguments
156          *
157          * @return bool True, if the command was successful
158          * @throws \ErrorException
159          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
160          * @throws \ImagickException
161          */
162         private function addUser()
163         {
164                 $name  = $this->getArgument(1);
165                 $nick  = $this->getArgument(2);
166                 $email = $this->getArgument(3);
167                 $lang  = $this->getArgument(4);
168
169                 if (empty($name)) {
170                         $this->out($this->l10n->t('Enter user name: '));
171                         $name = CliPrompt::prompt();
172                         if (empty($name)) {
173                                 throw new RuntimeException('A name must be set.');
174                         }
175                 }
176
177                 if (empty($nick)) {
178                         $this->out($this->l10n->t('Enter user nickname: '));
179                         $nick = CliPrompt::prompt();
180                         if (empty($nick)) {
181                                 throw new RuntimeException('A nick name must be set.');
182                         }
183                 }
184
185                 if (empty($email)) {
186                         $this->out($this->l10n->t('Enter user email address: '));
187                         $email = CliPrompt::prompt();
188                         if (empty($email)) {
189                                 throw new RuntimeException('A email address must be set.');
190                         }
191                 }
192
193                 if (empty($lang)) {
194                         $this->out($this->l10n->t('Enter a language (optional): '));
195                         $lang = CliPrompt::prompt();
196                 }
197
198                 if (empty($lang)) {
199                         return UserModel::createMinimal($name, $email, $nick);
200                 } else {
201                         return UserModel::createMinimal($name, $email, $nick, $lang);
202                 }
203         }
204
205         /**
206          * Allows or denys a user based on it's nickname
207          *
208          * @param bool $allow True, if the pending user is allowed, false if denies
209          *
210          * @return bool True, if allow was successful
211          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
212          */
213         public function pendingUser(bool $allow = true)
214         {
215                 $nick = $this->getArgument(1);
216
217                 if (!$nick) {
218                         $this->out($this->l10n->t('Enter user nickname: '));
219                         $nick = CliPrompt::prompt();
220                         if (empty($nick)) {
221                                 throw new RuntimeException('A nick name must be set.');
222                         }
223                 }
224
225                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
226                 if (empty($user)) {
227                         throw new RuntimeException($this->l10n->t('User not found'));
228                 }
229
230                 $pending = Register::getPendingForUser($user['uid'] ?? 0);
231                 if (empty($pending)) {
232                         throw new RuntimeException($this->l10n->t('User is not pending.'));
233                 }
234
235                 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
236         }
237 }