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