‪TYPO3CMS  ‪main
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;
34 
38 class ‪ResetPasswordCommand extends Command
39 {
40  public function ‪__construct(private readonly ‪Context $context, private readonly ‪PasswordReset $passwordReset)
41  {
42  parent::__construct();
43  }
44 
48  protected function ‪configure()
49  {
50  $this
51  ->addArgument(
52  'backendurl',
53  InputArgument::REQUIRED,
54  'The URL of the TYPO3 Backend, e.g. https://www.example.com/typo3/'
55  )->addArgument(
56  'email',
57  InputArgument::REQUIRED,
58  'The email address of a valid backend user'
59  );
60  }
64  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
65  {
66  $io = new SymfonyStyle($input, ‪$output);
67  $email = $input->getArgument('email');
68  $email = is_string($email) ? $email : '';
69  if (!GeneralUtility::validEmail($email)) {
70  $io->error('The given email "' . $email . '" is not a valid email address.');
71  return Command::FAILURE;
72  }
73  $backendUrl = $input->getArgument('backendurl');
74  $backendUrl = is_string($backendUrl) ? $backendUrl : '';
75  if (!‪GeneralUtility::isValidUrl($backendUrl)) {
76  $io->error('The given backend URL "' . $backendUrl . '" is not a valid URL.');
77  return Command::FAILURE;
78  }
79  $request = $this->‪createFakeWebRequest($backendUrl);
80  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
81  $this->passwordReset->initiateReset($request, $this->context, $email);
82  $io->success('Password reset for email address "' . $email . '" initiated.');
83  return Command::SUCCESS;
84  }
85 
89  protected function ‪createFakeWebRequest(string $backendUrl): ServerRequestInterface
90  {
91  $uri = new ‪Uri($backendUrl);
92  $request = new ‪ServerRequest(
93  $uri,
94  'GET',
95  'php://input',
96  [],
97  [
98  'HTTP_HOST' => $uri->getHost(),
99  'SERVER_NAME' => $uri->getHost(),
100  'HTTPS' => $uri->getScheme() === 'https',
101  'SCRIPT_FILENAME' => __FILE__,
102  'SCRIPT_NAME' => rtrim($uri->getPath(), '/') . '/',
103  ]
104  );
105  $backedUpEnvironment = $this->‪simulateEnvironmentForBackendEntryPoint();
106  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
107 
108  // Restore the environment
117  $backedUpEnvironment['currentScript'],
118  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
119  );
120 
121  return $request
122  ->withAttribute('normalizedParams', $normalizedParams)
123  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
124  }
125 
130  protected function ‪simulateEnvironmentForBackendEntryPoint(): array
131  {
132  $currentEnvironment = ‪Environment::toArray();
141  // This is ugly, as this change fakes the directory
142  dirname(‪Environment::getCurrentScript(), 4) . DIRECTORY_SEPARATOR . 'index.php',
143  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
144  );
145  return $currentEnvironment;
146  }
147 }
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Authentication\PasswordReset
Definition: PasswordReset.php:64
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static isComposerMode()
Definition: Environment.php:137
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static getCurrentScript()
Definition: Environment.php:220
‪TYPO3\CMS\Core\Core\Environment\toArray
‪static toArray()
Definition: Environment.php:305
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:54
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:30
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:160
‪TYPO3\CMS\Backend\Command
Definition: CreateBackendUserCommand.php:18
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\configure
‪configure()
Definition: ResetPasswordCommand.php:48
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\__construct
‪__construct(private readonly Context $context, private readonly PasswordReset $passwordReset)
Definition: ResetPasswordCommand.php:40
‪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:100
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\createFakeWebRequest
‪createFakeWebRequest(string $backendUrl)
Definition: ResetPasswordCommand.php:89
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ResetPasswordCommand.php:64
‪$output
‪$output
Definition: annotationChecker.php:114
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\isValidUrl
‪static bool isValidUrl(string $url)
Definition: GeneralUtility.php:708
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\simulateEnvironmentForBackendEntryPoint
‪simulateEnvironmentForBackendEntryPoint()
Definition: ResetPasswordCommand.php:130
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:840
‪TYPO3\CMS\Core\Core\Environment\getContext
‪static getContext()
Definition: Environment.php:128
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand
Definition: ResetPasswordCommand.php:39
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static isWindows()
Definition: Environment.php:276