‪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 {
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  }
76  protected function ‪execute(InputInterface $input, OutputInterface ‪$output): int
77  {
78  $io = new SymfonyStyle($input, ‪$output);
79  $email = $input->getArgument('email');
80  $email = is_string($email) ? $email : '';
81  if (!GeneralUtility::validEmail($email)) {
82  $io->error('The given email "' . $email . '" is not a valid email address.');
83  return Command::FAILURE;
84  }
85  $backendUrl = $input->getArgument('backendurl');
86  $backendUrl = is_string($backendUrl) ? $backendUrl : '';
87  if (!‪GeneralUtility::isValidUrl($backendUrl)) {
88  $io->error('The given backend URL "' . $backendUrl . '" is not a valid URL.');
89  return Command::FAILURE;
90  }
91  $request = $this->‪createFakeWebRequest($backendUrl);
92  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
93  $this->passwordReset->initiateReset($request, $this->context, $email);
94  $io->success('Password reset for email address "' . $email . '" initiated.');
95  return Command::SUCCESS;
96  }
97 
101  protected function ‪createFakeWebRequest(string $backendUrl): ServerRequestInterface
102  {
103  $uri = new Uri($backendUrl);
104  $request = new ServerRequest(
105  $uri,
106  'GET',
107  'php://input',
108  [],
109  [
110  'HTTP_HOST' => $uri->getHost(),
111  'SERVER_NAME' => $uri->getHost(),
112  'HTTPS' => $uri->getScheme() === 'https',
113  'SCRIPT_FILENAME' => __FILE__,
114  'SCRIPT_NAME' => rtrim($uri->getPath(), '/') . '/',
115  ]
116  );
117  $backedUpEnvironment = $this->‪simulateEnvironmentForBackendEntryPoint();
118  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
119 
120  // Restore the environment
129  $backedUpEnvironment['currentScript'],
130  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
131  );
132 
133  return $request
134  ->withAttribute('normalizedParams', $normalizedParams)
135  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
136  }
137 
142  protected function ‪simulateEnvironmentForBackendEntryPoint(): array
143  {
144  $currentEnvironment = ‪Environment::toArray();
153  // This is ugly, as this change fakes the directory
154  dirname(‪Environment::getCurrentScript(), 4) . DIRECTORY_SEPARATOR . 'index.php',
155  ‪Environment::isWindows() ? 'WINDOWS' : 'UNIX'
156  );
157  return $currentEnvironment;
158  }
159 }
‪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\Backend\Command\ResetPasswordCommand\__construct
‪__construct(Context $context, PasswordReset $passwordReset)
Definition: ResetPasswordCommand.php:48
‪TYPO3\CMS\Core\Core\Environment\toArray
‪static toArray()
Definition: Environment.php:316
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:55
‪TYPO3\CMS\Core\Core\Environment\getConfigPath
‪static getConfigPath()
Definition: Environment.php:212
‪TYPO3\CMS\Core\Http\Uri
Definition: Uri.php:29
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\$passwordReset
‪PasswordReset $passwordReset
Definition: ResetPasswordCommand.php:46
‪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: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:100
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\createFakeWebRequest
‪createFakeWebRequest(string $backendUrl)
Definition: ResetPasswordCommand.php:99
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\execute
‪execute(InputInterface $input, OutputInterface $output)
Definition: ResetPasswordCommand.php:74
‪$output
‪$output
Definition: annotationChecker.php:119
‪TYPO3\CMS\Core\Utility\GeneralUtility\isValidUrl
‪static bool isValidUrl($url)
Definition: GeneralUtility.php:797
‪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:41
‪TYPO3\CMS\Backend\Command\ResetPasswordCommand\simulateEnvironmentForBackendEntryPoint
‪simulateEnvironmentForBackendEntryPoint()
Definition: ResetPasswordCommand.php:140
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪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:287