]> git.mxchange.org Git - friendica.git/blob - src/Console/User.php
6db2cf84ee0e719b2dffe3984a29652af189f24c
[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 Friendica\Model\UserService;
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         /** @var UserService */
54         private $userService;
55
56         protected function getHelp()
57         {
58                 $help = <<<HELP
59 console user - Modify user settings per console commands.
60 Usage
61         bin/console user password <nickname> [<password>] [-h|--help|-?] [-v]
62         bin/console user add [<name> [<nickname> [<email> [<language>]]]] [-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, UserService $userService, array $argv = null)
75         {
76                 parent::__construct($argv);
77
78                 $this->appMode = $appMode;
79                 $this->l10n = $l10n;
80                 $this->dba = $dba;
81                 $this->userService = $userService;
82         }
83
84         protected function doExecute()
85         {
86                 if ($this->getOption('v')) {
87                         $this->out('Class: ' . __CLASS__);
88                         $this->out('Arguments: ' . var_export($this->args, true));
89                         $this->out('Options: ' . var_export($this->options, true));
90                 }
91
92                 if (count($this->args) == 0) {
93                         $this->out($this->getHelp());
94                         return 0;
95                 }
96
97                 if ($this->appMode->isInstall()) {
98                         throw new RuntimeException('Database isn\'t ready or populated yet');
99                 }
100
101                 $command = $this->getArgument(0);
102
103                 switch ($command) {
104                         case 'password':
105                                 return $this->password();
106                         case 'add':
107                                 return $this->addUser();
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                 if (empty($nick)) {
174                         $this->out($this->l10n->t('Enter user nickname: '));
175                         $nick = CliPrompt::prompt();
176                         if (empty($nick)) {
177                                 throw new RuntimeException('A nick name must be set.');
178                         }
179                 }
180                 if (empty($email)) {
181                         $this->out($this->l10n->t('Enter user email address: '));
182                         $email = CliPrompt::prompt();
183                         if (empty($email)) {
184                                 throw new RuntimeException('A email address must be set.');
185                         }
186                 }
187
188                 if (empty($lang)) {
189                         $this->out($this->l10n->t('Enter a language (optional): '));
190                         $lang = CliPrompt::prompt();
191                 }
192
193                 if (empty($lang)) {
194                         return $this->userService->createMinimal($name, $email, $nick);
195                 } else {
196                         return $this->userService->createMinimal($name, $email, $nick, $lang);
197                 }
198         }
199 }