|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
declare(strict_types=1); |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\ServiceHelper; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrl; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
|
12
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
13
|
|
|
|
|
14
|
|
|
use function Symfony\Component\String\u; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
readonly class AuthenticationConfigHelper |
|
17
|
|
|
{ |
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private ParameterBagInterface $parameterBag, |
|
20
|
|
|
private AccessUrlHelper $urlHelper, |
|
21
|
|
|
private UrlGeneratorInterface $urlGenerator, |
|
22
|
|
|
) {} |
|
23
|
|
|
|
|
24
|
|
|
public function getParams(string $providerName, ?AccessUrl $url = null): array |
|
25
|
|
|
{ |
|
26
|
|
|
$providers = $this->getProvidersForUrl($url); |
|
27
|
|
|
|
|
28
|
|
|
if (!isset($providers[$providerName])) { |
|
29
|
|
|
throw new InvalidArgumentException('Invalid authentication provider for access URL'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $providers[$providerName]; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function isEnabled(string $methodName, ?AccessUrl $url = null): bool |
|
36
|
|
|
{ |
|
37
|
|
|
$configParams = $this->getParams($methodName, $url); |
|
38
|
|
|
|
|
39
|
|
|
return $configParams['enabled'] ?? false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getEnabledProviders(?AccessUrl $url = null): array |
|
43
|
|
|
{ |
|
44
|
|
|
$urlProviders = $this->getProvidersForUrl($url); |
|
45
|
|
|
|
|
46
|
|
|
$enabledProviders = []; |
|
47
|
|
|
|
|
48
|
|
|
foreach ($urlProviders as $providerName => $providerParams) { |
|
49
|
|
|
if ($providerParams['enabled'] ?? false) { |
|
50
|
|
|
$enabledProviders[] = [ |
|
51
|
|
|
'name' => $providerName, |
|
52
|
|
|
'title' => $providerParams['title'] ?? u($providerName)->title(), |
|
|
|
|
|
|
53
|
|
|
'url' => $this->urlGenerator->generate("chamilo.oauth2_{$providerName}_start"), |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $enabledProviders; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function getProvidersForUrl(?AccessUrl $url): array |
|
62
|
|
|
{ |
|
63
|
|
|
$urlId = $url ? $url->getId() : $this->urlHelper->getCurrent()->getId(); |
|
64
|
|
|
|
|
65
|
|
|
$authentication = $this->parameterBag->get('authentication'); |
|
66
|
|
|
|
|
67
|
|
|
if (isset($authentication[$urlId])) { |
|
68
|
|
|
return $authentication[$urlId]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (isset($authentication['default'])) { |
|
72
|
|
|
return $authentication['default']; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
throw new InvalidArgumentException('Invalid access URL configuration'); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|