3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Console;
25 use Friendica\Core\L10n;
26 use Friendica\Database\Database;
27 use Friendica\Model\Register;
28 use Friendica\Model\User as UserModel;
30 use Seld\CliPrompt\CliPrompt;
33 * tool to set a new password for a user
35 * With this tool, you can set a new password for a user
37 class User extends \Asika\SimpleConsole\Console
39 protected $helpOptions = ['h', 'help', '?'];
54 protected function getHelp()
57 console user - Modify user settings per console commands.
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 delete [<nickname>] [-q] [-h|--help|-?] [-v]
62 bin/console user allow [<nickname>] [-h|--help|-?] [-v]
63 bin/console user deny [<nickname>] [-h|--help|-?] [-v]
64 bin/console user block [<nickname>] [-h|--help|-?] [-v]
65 bin/console user unblock [<nickname>] [-h|--help|-?] [-v]
68 Modify user settings per console commands.
71 -h|--help|-? Show help information
72 -v Show more debug information.
73 -q Quiet mode (don't ask for a command).
78 public function __construct(App\Mode $appMode, L10n $l10n, Database $dba, array $argv = null)
80 parent::__construct($argv);
82 $this->appMode = $appMode;
87 protected function doExecute()
89 if ($this->getOption('v')) {
90 $this->out('Class: ' . __CLASS__);
91 $this->out('Arguments: ' . var_export($this->args, true));
92 $this->out('Options: ' . var_export($this->options, true));
95 if (count($this->args) == 0) {
96 $this->out($this->getHelp());
100 if ($this->appMode->isInstall()) {
101 throw new RuntimeException('Database isn\'t ready or populated yet');
104 $command = $this->getArgument(0);
108 return $this->password();
110 return $this->addUser();
112 return $this->pendingUser(true);
114 return $this->pendingUser(false);
116 return $this->blockUser(true);
118 return $this->blockUser(false);
120 return $this->deleteUser();
122 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
127 * Sets a new password
129 * @return int Return code of this command
133 private function password()
135 $nick = $this->getArgument(1);
137 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
138 if (!$this->dba->isResult($user)) {
139 throw new RuntimeException($this->l10n->t('User not found'));
142 $password = $this->getArgument(2);
144 if (is_null($password)) {
145 $this->out($this->l10n->t('Enter new password: '), false);
146 $password = CliPrompt::hiddenPrompt(true);
150 $result = UserModel::updatePassword($user['uid'], $password);
152 if (!$this->dba->isResult($result)) {
153 throw new \Exception($this->l10n->t('Password update failed. Please try again.'));
156 $this->out($this->l10n->t('Password changed.'));
157 } catch (\Exception $e) {
158 throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
165 * Adds a new user based on given console arguments
167 * @return bool True, if the command was successful
168 * @throws \ErrorException
169 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
170 * @throws \ImagickException
172 private function addUser()
174 $name = $this->getArgument(1);
175 $nick = $this->getArgument(2);
176 $email = $this->getArgument(3);
177 $lang = $this->getArgument(4);
180 $this->out($this->l10n->t('Enter user name: '));
181 $name = CliPrompt::prompt();
183 throw new RuntimeException('A name must be set.');
188 $this->out($this->l10n->t('Enter user nickname: '));
189 $nick = CliPrompt::prompt();
191 throw new RuntimeException('A nick name must be set.');
196 $this->out($this->l10n->t('Enter user email address: '));
197 $email = CliPrompt::prompt();
199 throw new RuntimeException('A email address must be set.');
204 $this->out($this->l10n->t('Enter a language (optional): '));
205 $lang = CliPrompt::prompt();
209 return UserModel::createMinimal($name, $email, $nick);
211 return UserModel::createMinimal($name, $email, $nick, $lang);
216 * Allows or denys a user based on it's nickname
218 * @param bool $allow True, if the pending user is allowed, false if denies
220 * @return bool True, if allow was successful
221 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
223 private function pendingUser(bool $allow = true)
225 $nick = $this->getArgument(1);
228 $this->out($this->l10n->t('Enter user nickname: '));
229 $nick = CliPrompt::prompt();
231 throw new RuntimeException('A nick name must be set.');
235 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
237 throw new RuntimeException($this->l10n->t('User not found'));
240 $pending = Register::getPendingForUser($user['uid'] ?? 0);
241 if (empty($pending)) {
242 throw new RuntimeException($this->l10n->t('User is not pending.'));
245 return ($allow) ? UserModel::allow($pending['hash']) : UserModel::deny($pending['hash']);
249 * Blocks/unblocks a user
251 * @param bool $block True, if the given user should get blocked
253 * @return bool True, if the command was successful
256 private function blockUser(bool $block = true)
258 $nick = $this->getArgument(1);
261 $this->out($this->l10n->t('Enter user nickname: '));
262 $nick = CliPrompt::prompt();
264 throw new RuntimeException('A nick name must be set.');
268 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
270 throw new RuntimeException($this->l10n->t('User not found'));
273 return $block ? UserModel::block($user['uid'] ?? 0) : UserModel::block($user['uid'] ?? 0, false);
279 * @return bool True, if the delete was successful
282 private function deleteUser()
284 $nick = $this->getArgument(1);
287 $this->out($this->l10n->t('Enter user nickname: '));
288 $nick = CliPrompt::prompt();
290 throw new RuntimeException('A nick name must be set.');
294 $user = $this->dba->selectFirst('user', ['uid'], ['nickname' => $nick]);
296 throw new RuntimeException($this->l10n->t('User not found'));
299 if (!$this->getOption('q')) {
300 $this->out($this->l10n->t('Type "yes" to delete %s', $nick));
301 if (CliPrompt::prompt() !== 'yes') {
302 throw new RuntimeException('Delete abort.');
306 return UserModel::remove($user['uid'] ?? -1);