‪TYPO3CMS  ‪main
LoginProviderResolver.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 
37 {
38  protected array ‪$loginProviders = [];
39 
40  public function ‪__construct(?array ‪$loginProviders = null)
41  {
42  if (‪$loginProviders === null) {
43  ‪$loginProviders = ‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['backend']['loginProviders'] ?? [];
44  if (!is_array(‪$loginProviders)) {
45  ‪$loginProviders = [];
46  }
47  }
48 
49  $this->loginProviders = $this->‪validateAndSortLoginProviders($loginProviders);
50  }
51 
58  protected function ‪validateAndSortLoginProviders(array $providers): array
59  {
60  if (empty($providers)) {
61  throw new \RuntimeException('No login providers are registered in $GLOBALS[\'TYPO3_CONF_VARS\'][\'EXTCONF\'][\'backend\'][\'loginProviders\'].', 1433417281);
62  }
63  foreach ($providers as ‪$identifier => $configuration) {
64  if (empty($configuration) || !is_array($configuration)) {
65  throw new \RuntimeException('Missing configuration for login provider "' . ‪$identifier . '".', 1433416043);
66  }
67  if (!is_string($configuration['provider']) || empty($configuration['provider']) || !class_exists($configuration['provider']) || !is_subclass_of($configuration['provider'], LoginProviderInterface::class)) {
68  throw new \RuntimeException('The login provider "' . ‪$identifier . '" defines an invalid provider. Ensure the class exists and implements the "' . LoginProviderInterface::class . '".', 1460977275);
69  }
70  if (empty($configuration['label'])) {
71  throw new \RuntimeException('Missing label definition for login provider "' . ‪$identifier . '".', 1433416044);
72  }
73  if (empty($configuration['iconIdentifier'])) {
74  throw new \RuntimeException('Missing icon definition for login provider "' . ‪$identifier . '".', 1433416045);
75  }
76  if (!isset($configuration['sorting'])) {
77  throw new \RuntimeException('Missing sorting definition for login provider "' . ‪$identifier . '".', 1433416046);
78  }
79  }
80  // sort providers
81  uasort($providers, static function (array $a, array $b): int {
82  return (int)$b['sorting'] - (int)$a['sorting'];
83  });
84  return $providers;
85  }
86 
90  public function ‪getLoginProviders(): array
91  {
93  }
94 
100  public function ‪hasLoginProvider(string ‪$identifier): bool
101  {
102  return isset($this->loginProviders[‪$identifier]);
103  }
104 
106  {
107  return $this->loginProviders[‪$identifier] ?? [];
108  }
109 
113  public function ‪getPrimaryLoginProviderIdentifier(): string
114  {
115  reset($this->loginProviders);
116  return (string)key($this->loginProviders);
117  }
118 
123  public function ‪resolveLoginProviderIdentifierFromRequest(ServerRequestInterface $request, string $cookieName): string
124  {
125  $loginProvider = (string)($request->getParsedBody()['loginProvider'] ?? $request->getQueryParams()['loginProvider'] ?? '');
126  if ((empty($loginProvider) || !$this->‪hasLoginProvider($loginProvider)) && !empty($request->getCookieParams()[$cookieName] ?? null)) {
127  $loginProvider = $request->getCookieParams()[$cookieName];
128  }
129  if (empty($loginProvider) || !$this->‪hasLoginProvider($loginProvider)) {
130  $loginProvider = $this->‪getPrimaryLoginProviderIdentifier();
131  }
132  return (string)$loginProvider;
133  }
134 }
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\getPrimaryLoginProviderIdentifier
‪getPrimaryLoginProviderIdentifier()
Definition: LoginProviderResolver.php:113
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\getLoginProviders
‪getLoginProviders()
Definition: LoginProviderResolver.php:90
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\hasLoginProvider
‪hasLoginProvider(string $identifier)
Definition: LoginProviderResolver.php:100
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\__construct
‪__construct(?array $loginProviders=null)
Definition: LoginProviderResolver.php:40
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\$loginProviders
‪array $loginProviders
Definition: LoginProviderResolver.php:38
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver
Definition: LoginProviderResolver.php:37
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\resolveLoginProviderIdentifierFromRequest
‪resolveLoginProviderIdentifierFromRequest(ServerRequestInterface $request, string $cookieName)
Definition: LoginProviderResolver.php:123
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\getLoginProviderConfigurationByIdentifier
‪getLoginProviderConfigurationByIdentifier(string $identifier)
Definition: LoginProviderResolver.php:105
‪TYPO3\CMS\Backend\LoginProvider\LoginProviderResolver\validateAndSortLoginProviders
‪array validateAndSortLoginProviders(array $providers)
Definition: LoginProviderResolver.php:58
‪TYPO3\CMS\Backend\LoginProvider
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37