‪TYPO3CMS  11.5
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 {
43  private ‪$context;
44 
49 
51  {
52  $this->context = ‪$context;
53  $this->passwordReset = ‪$passwordReset;
54  parent::__construct();
55  }
56 
60  protected function ‪configure()
61  {
62  $this
63  ->addArgument(
64  'backendurl',
65  InputArgument::REQUIRED,
66  'The URL of the TYPO3 Backend, e.g. https://www.example.com/typo3/'
67  )->addArgument(
68  'email',
69  InputArgument::REQUIRED,
70  'The email address of a valid backend user'
71  );
72  }
78  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
79  {
80  $io = new SymfonyStyle($input, ‪$output);
81  $email = $input->getArgument('email');
82  $email = is_string($email) ? $email : '';
83  if (!GeneralUtility::validEmail($email)) {
84  $io->error('The given email "' . $email . '" is not a valid email address.');
85  return 1;
86  }
87  $backendUrl = $input->getArgument('backendurl');
88  $backendUrl = is_string($backendUrl) ? $backendUrl : '';
89  if (!‪GeneralUtility::isValidUrl($backendUrl)) {
90  $io->error('The given backend URL "' . $backendUrl . '" is not a valid URL.');
91  return 1;
92  }
93  $request = $this->‪createFakeWebRequest($backendUrl);
94  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
95  $this->passwordReset->initiateReset($request, $this->context, $email);
96  $io->success('Password reset for email address "' . $email . '" initiated.');
97  return 0;
98  }
99 
106  protected function ‪createFakeWebRequest(string $backendUrl): ServerRequestInterface
107  {
108  $uri = new Uri($backendUrl);
109  $request = new ServerRequest(
110  $uri,
111  'GET',
112  'php://input',
113  [],
114  [
115  'HTTP_HOST' => $uri->getHost(),
116  'SERVER_NAME' => $uri->getHost(),
117  'HTTPS' => $uri->getScheme() === 'https',
118  'SCRIPT_FILENAME' => __FILE__,
119  'SCRIPT_NAME' => rtrim($uri->getPath(), '/') . '/',
120  ]
121  );
122  $backedUpEnvironment = $this->‪simulateEnvironmentForBackendEntryPoint();
123  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
124 
125  // Restore the environment
134  $backedUpEnvironment['currentScript'],
135  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
136  );
137 
138  return $request
139  ->withAttribute('normalizedParams', $normalizedParams)
140  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
141  }
142 
147  protected function ‪simulateEnvironmentForBackendEntryPoint(): array
148  {
149  $currentEnvironment = ‪Environment::toArray();
158  // This is ugly, as this change fakes the directory
159  dirname(‪Environment::getCurrentScript(), 4) . DIRECTORY_SEPARATOR . 'index.php',
160  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
161  );
162  return $currentEnvironment;
163  }
164 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:206
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Backend\Authentication\PasswordReset
Definition: PasswordReset.php:59
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:318
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Core\Core\Environment\getCurrentScript
‪static string getCurrentScript()
Definition: Environment.php:246
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\__construct
‪__construct(Context $context, PasswordReset $passwordReset)
Definition: ResetPasswordCommand.php:48
‪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:141
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\$passwordReset
‪PasswordReset $passwordReset
Definition: ResetPasswordCommand.php:46
‪TYPO3\CMS\Core\Core\Environment\getProjectPath
‪static string getProjectPath()
Definition: Environment.php:177
‪TYPO3\CMS\Backend\Command
Definition: LockBackendCommand.php:16
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\configure
‪configure()
Definition: ResetPasswordCommand.php:58
‪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:111
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\createFakeWebRequest
‪ServerRequestInterface createFakeWebRequest(string $backendUrl)
Definition: ResetPasswordCommand.php:104
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ResetPasswordCommand.php:76
‪$output
‪$output
Definition: annotationChecker.php:121
‪TYPO3\CMS\Core\Utility\GeneralUtility\isValidUrl
‪static bool isValidUrl($url)
Definition: GeneralUtility.php:883
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static bool isComposerMode()
Definition: Environment.php:152
‪TYPO3\CMS\Core\Core\Environment\toArray
‪static array toArray()
Definition: Environment.php:353
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\$context
‪Context $context
Definition: ResetPasswordCommand.php:42
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static string getConfigPath()
Definition: Environment.php:236
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\simulateEnvironmentForBackendEntryPoint
‪simulateEnvironmentForBackendEntryPoint()
Definition: ResetPasswordCommand.php:145
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:843
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:162
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand
Definition: ResetPasswordCommand.php:39
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:218