‪TYPO3CMS  10.4
ResetPasswordCommand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
21 use Symfony\Component\Console\Command\Command;
22 use Symfony\Component\Console\Input\InputArgument;
23 use Symfony\Component\Console\Input\InputInterface;
24 use Symfony\Component\Console\Output\OutputInterface;
25 use Symfony\Component\Console\Style\SymfonyStyle;
33 
37 class ‪ResetPasswordCommand extends Command
38 {
42  protected function ‪configure()
43  {
44  $this
45  ->setDescription('Trigger a password reset for a backend user.')
46  ->addArgument(
47  'backendurl',
48  InputArgument::REQUIRED,
49  'The URL of the TYPO3 Backend, e.g. https://www.example.com/typo3/'
50  )->addArgument(
51  'email',
52  InputArgument::REQUIRED,
53  'The email address of a valid backend user'
54  );
55  }
61  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
62  {
63  $io = new SymfonyStyle($input, ‪$output);
64  $email = $input->getArgument('email');
65  $email = is_string($email) ? $email : '';
66  if (!GeneralUtility::validEmail($email)) {
67  $io->error('The given email "' . $email . '" is not a valid email address.');
68  return 1;
69  }
70  $backendUrl = $input->getArgument('backendurl');
71  $backendUrl = is_string($backendUrl) ? $backendUrl : '';
72  if (!‪GeneralUtility::isValidUrl($backendUrl)) {
73  $io->error('The given backend URL "' . $backendUrl . '" is not a valid URL.');
74  return 1;
75  }
76  $reset = GeneralUtility::makeInstance(PasswordReset::class);
77  if (!$reset->isEnabled()) {
78  $io->error('Password reset functionality is disabled');
79  return 1;
80  }
81  $context = GeneralUtility::makeInstance(Context::class);
82  $request = $this->‪createFakeWebRequest($backendUrl);
83  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
84  $reset->initiateReset($request, $context, $email);
85  $io->success('Password reset for email address "' . $email . '" initiated.');
86  return 0;
87  }
88 
95  protected function ‪createFakeWebRequest(string $backendUrl): ServerRequestInterface
96  {
97  $uri = new ‪Uri($backendUrl);
98  $request = new ‪ServerRequest(
99  $uri,
100  'GET',
101  'php://input',
102  [],
103  [
104  'HTTP_HOST' => $uri->getHost(),
105  'SERVER_NAME' => $uri->getHost(),
106  'HTTPS' => $uri->getScheme() === 'https',
107  'SCRIPT_FILENAME' => __FILE__,
108  'SCRIPT_NAME' => rtrim($uri->getPath(), '/') . '/'
109  ]
110  );
111  $backedUpEnvironment = $this->‪simulateEnvironmentForBackendEntryPoint();
112  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
113 
114  // Restore the environment
123  $backedUpEnvironment['currentScript'],
124  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
125  );
126 
127  return $request->withAttribute('normalizedParams', $normalizedParams);
128  }
129 
134  protected function ‪simulateEnvironmentForBackendEntryPoint(): array
135  {
136  $currentEnvironment = ‪Environment::toArray();
145  // This is ugly, as this change fakes the directory
146  dirname(‪Environment::getCurrentScript(), 4) . DIRECTORY_SEPARATOR . 'index.php',
147  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
148  );
149  return $currentEnvironment;
150  }
151 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Backend\Authentication\PasswordReset
Definition: PasswordReset.php:58
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:292
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static string getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static ApplicationContext getContext()
Definition: Environment.php:133
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:169
‪TYPO3\CMS\Backend\Command
Definition: LockBackendCommand.php:16
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\configure
‪configure()
Definition: ResetPasswordCommand.php:42
‪TYPO3\CMS\Core\Core\Environment\initialize
‪static initialize(ApplicationContext $context, bool $cli, bool $composerMode, string $projectPath, string $publicPath, string $varPath, string $configPath, string $currentScript, string $os)
Definition: Environment.php:104
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\createFakeWebRequest
‪ServerRequestInterface createFakeWebRequest(string $backendUrl)
Definition: ResetPasswordCommand.php:95
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ResetPasswordCommand.php:61
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Core\Utility\GeneralUtility\isValidUrl
‪static bool isValidUrl($url)
Definition: GeneralUtility.php:944
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static bool isComposerMode()
Definition: Environment.php:144
‪TYPO3\CMS\Core\Core\Environment\toArray
‪static array toArray()
Definition: Environment.php:327
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:210
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\simulateEnvironmentForBackendEntryPoint
‪simulateEnvironmentForBackendEntryPoint()
Definition: ResetPasswordCommand.php:134
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:848
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:154
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand
Definition: ResetPasswordCommand.php:38
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192