‪TYPO3CMS  ‪main
PasswordGeneratorController.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\ResponseFactoryInterface;
21 use Psr\Http\Message\ResponseInterface;
22 use Psr\Http\Message\ServerRequestInterface;
23 use Psr\Http\Message\StreamFactoryInterface;
27 
32 {
33  public function ‪__construct(
34  private readonly ‪Random $random,
35  private readonly ResponseFactoryInterface $responseFactory,
36  private readonly StreamFactoryInterface $streamFactory
37  ) {}
38 
39  public function ‪generate(ServerRequestInterface $request): ResponseInterface
40  {
41  $passwordRules = $request->getParsedBody()['passwordRules'] ?? [];
42 
43  if (is_array($passwordRules)) {
44  try {
45  $password = $this->random->generateRandomPassword($passwordRules);
46  return $this->‪createResponse([
47  'success' => true,
48  'password' => $password,
49  ]);
51  }
52  }
53 
54  return $this->‪createResponse([
55  'success' => false,
56  'message' => $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:error.misconfiguredPasswordRules'),
57  ]);
58  }
59 
60  protected function ‪createResponse(array $data): ResponseInterface
61  {
62  return $this->responseFactory->createResponse()
63  ->withHeader('Content-Type', 'application/json; charset=utf-8')
64  ->withBody($this->streamFactory->createStream((string)json_encode($data)));
65  }
66 
68  {
69  return ‪$GLOBALS['LANG'];
70  }
71 }
‪TYPO3\CMS\Core\Controller\PasswordGeneratorController\__construct
‪__construct(private readonly Random $random, private readonly ResponseFactoryInterface $responseFactory, private readonly StreamFactoryInterface $streamFactory)
Definition: PasswordGeneratorController.php:33
‪TYPO3\CMS\Core\Exception\InvalidPasswordRulesException
Definition: InvalidPasswordRulesException.php:25
‪TYPO3\CMS\Core\Controller\PasswordGeneratorController
Definition: PasswordGeneratorController.php:32
‪TYPO3\CMS\Core\Controller
Definition: ErrorPageController.php:18
‪TYPO3\CMS\Core\Controller\PasswordGeneratorController\createResponse
‪createResponse(array $data)
Definition: PasswordGeneratorController.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Crypto\Random
Definition: Random.php:27
‪TYPO3\CMS\Core\Controller\PasswordGeneratorController\getLanguageService
‪getLanguageService()
Definition: PasswordGeneratorController.php:67
‪TYPO3\CMS\Core\Controller\PasswordGeneratorController\generate
‪generate(ServerRequestInterface $request)
Definition: PasswordGeneratorController.php:39