]> git.mxchange.org Git - friendica.git/blob - src/Console/User.php
Add "User::block()" to console command
[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         bin/console user block [<nickname>] [-h|--help|-?] [-v]
64         bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
65
66 Description
67         Modify user settings per console commands.
68
69 Options
70     -h|--help|-? Show help information
71     -v           Show more debug information.
72 HELP;
73                 return $help;
74         }
75
76         public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
77         {
78                 parent::__construct($argv);
79
80                 $this->appMode     = $appMode;
81                 $this->l10n        = $l10n;
82                 $this->dba         = $dba;
83         }
84
85         protected function doExecute()
86         {
87                 if ($this->getOption('v')) {
88                         $this->out('Class: ' . __CLASS__);
89                         $this->out('Arguments: ' . var_export($this->args, true));
90                         $this->out('Options: ' . var_export($this->options, true));
91                 }
92
93                 if (count($this->args) == 0) {
94                         $this->out($this->getHelp());
95                         return 0;
96                 }
97
98                 if ($this->appMode->isInstall()) {
99                         throw new RuntimeException('Database isn\'t ready or populated yet');
100                 }
101
102                 $command = $this->getArgument(0);
103
104                 switch ($command) {
105                         case 'password':
106                                 return $this->password();
107                         case 'add':
108                                 return $this->addUser();
109                         case 'allow':
110                                 return $this->pendingUser(true);
111                         case 'deny':
112                                 return $this->pendingUser(false);
113                         case 'block':
114                                 return $this->blockUser(true);
115                         case 'unblock':
116                                 return $this->blockUser(false);
117                         default:
118                                 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
119                 }
120         }
121
122         /**
123          * Sets a new password
124          *
125          * @return int Return code of this command
126          *
127          * @throws \Exception
128          */
129         private function password()
130         {
131                 $nick = $this->getArgument(1);
132
133                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
134                 if (!$this->dba->isResult($user)) {
135                         throw new RuntimeException($this->l10n->t('User not found'));
136                 }
137
138                 $password = $this->getArgument(2);
139
140                 if (is_null($password)) {
141                         $this->out($this->l10n->t('Enter new password: '), false);
142                         $password = CliPrompt::hiddenPrompt(true);
143                 }
144
145                 try {
146                         $result = UserModel::updatePassword($user['uid'], $password);
147
148                         if (!$this->dba->isResult($result)) {
149                                 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
150                         }
151
152                         $this->out($this->l10n->t('Password changed.'));
153                 } catch (\Exception $e) {
154                         throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
155                 }
156
157                 return 0;
158         }
159
160         /**
161          * Adds a new user based on given console arguments
162          *
163          * @return bool True, if the command was successful
164          * @throws \ErrorException
165          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
166          * @throws \ImagickException
167          */
168         private function addUser()
169         {
170                 $name  = $this->getArgument(1);
171                 $nick  = $this->getArgument(2);
172                 $email = $this->getArgument(3);
173                 $lang  = $this->getArgument(4);
174
175                 if (empty($name)) {
176                         $this->out($this->l10n->t('Enter user name: '));
177                         $name = CliPrompt::prompt();
178                         if (empty($name)) {
179                                 throw new RuntimeException('A name must be set.');
180                         }
181                 }
182
183                 if (empty($nick)) {
184                         $this->out($this->l10n->t('Enter user nickname: '));
185                         $nick = CliPrompt::prompt();
186                         if (empty($nick)) {
187                                 throw new RuntimeException('A nick name must be set.');
188                         }
189                 }
190
191                 if (empty($email)) {
192                         $this->out($this->l10n->t('Enter user email address: '));
193                         $email = CliPrompt::prompt();
194                         if (empty($email)) {
195                                 throw new RuntimeException('A email address must be set.');
196                         }
197                 }
198
199                 if (empty($lang)) {
200                         $this->out($this->l10n->t('Enter a language (optional): '));
201                         $lang = CliPrompt::prompt();
202                 }
203
204                 if (empty($lang)) {
205                         return UserModel::createMinimal($name, $email, $nick);
206                 } else {
207                         return UserModel::createMinimal($name, $email, $nick, $lang);
208                 }
209         }
210
211         /**
212          * Allows or denys a user based on it's nickname
213          *
214          * @param bool $allow True, if the pending user is allowed, false if denies
215          *
216          * @return bool True, if allow was successful
217          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
218          */
219         private function pendingUser(bool $allow = true)
220         {
221                 $nick = $this->getArgument(1);
222
223                 if (!$nick) {
224                         $this->out($this->l10n->t('Enter user nickname: '));
225                         $nick = CliPrompt::prompt();
226                         if (empty($nick)) {
227                                 throw new RuntimeException('A nick name must be set.');
228                         }
229                 }
230
231                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
232                 if (empty($user)) {
233                         throw new RuntimeException($this->l10n->t('User not found'));
234                 }
235
236                 $pending = Register::getPendingForUser($user['uid'] ?? 0);
237                 if (empty($pending)) {
238                         throw new RuntimeException($this->l10n->t('User is not pending.'));
239                 }
240
241                 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
242         }
243
244         /**
245          * Blocks/unblocks a user
246          *
247          * @param bool $block True, if the given user should get blocked
248          *
249          * @return bool True, if the command was successful
250          * @throws \Exception
251          */
252         private function blockUser(bool $block = true)
253         {
254                 $nick = $this->getArgument(1);
255
256                 if (!$nick) {
257                         $this->out($this->l10n->t('Enter user nickname: '));
258                         $nick = CliPrompt::prompt();
259                         if (empty($nick)) {
260                                 throw new RuntimeException('A nick name must be set.');
261                         }
262                 }
263
264                 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
265                 if (empty($user)) {
266                         throw new RuntimeException($this->l10n->t('User not found'));
267                 }
268
269                 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
270         }
271 }