1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kuleuven\AuthenticationBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
7
|
|
|
|
8
|
|
|
class ShibbolethAttributesInjectionProviderManager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var AttributeDefinitionsProviderInterface |
12
|
|
|
*/ |
13
|
|
|
protected $attributeDefinitionsProvider; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var AttributesInjectionProviderInterface[]|ArrayCollection |
17
|
|
|
*/ |
18
|
|
|
protected $providerPropertiesCollection; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param AttributeDefinitionsProviderInterface $attributeDefinitionsProvider |
22
|
|
|
*/ |
23
|
|
|
public function __construct(AttributeDefinitionsProviderInterface $attributeDefinitionsProvider) |
24
|
|
|
{ |
25
|
|
|
$this->attributeDefinitionsProvider = $attributeDefinitionsProvider; |
26
|
|
|
$this->providerPropertiesCollection = new ArrayCollection(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param AttributesInjectionProviderInterface $provider |
31
|
|
|
* @param int $priority |
32
|
|
|
*/ |
33
|
|
|
public function addProvider(AttributesInjectionProviderInterface $provider, $priority = 0) |
34
|
|
|
{ |
35
|
|
|
if ($provider instanceof ParameterAttributesProvider && 0 === $priority) { |
36
|
|
|
$priority = -INF; |
37
|
|
|
} |
38
|
|
|
if ($provider instanceof HeaderAttributesProvider && 0 === $priority) { |
39
|
|
|
$priority = -INF; |
40
|
|
|
} |
41
|
|
|
$this->providerPropertiesCollection->add(['priority' => $priority, 'provider' => $provider]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
48
|
|
|
{ |
49
|
|
|
if (!$event->isMasterRequest()) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!empty($this->providerPropertiesCollection)) { |
54
|
|
|
$attributeDefinitions = $this->attributeDefinitionsProvider->getAttributeDefinitions(); |
55
|
|
|
$lcIdOrAliasMap = []; |
56
|
|
|
foreach ($attributeDefinitions as $idOrAlias => $attributeDefinition) { |
57
|
|
|
$lcIdOrAliasMap[strtolower($idOrAlias)] = $idOrAlias; |
58
|
|
|
} |
59
|
|
|
$server = $event->getRequest()->server; |
60
|
|
|
$providerPropertiesCollectionIterator = $this->providerPropertiesCollection->getIterator(); |
61
|
|
|
$providerPropertiesCollectionIterator->uasort(function ($first, $second) { |
62
|
|
|
// Place highest priority first |
63
|
|
|
if ($first['priority'] === $second['priority']) { |
64
|
|
|
return 0; |
65
|
|
|
} |
66
|
|
|
return (int)$first['priority'] > (int)$second['priority'] ? -1 : 1; |
67
|
|
|
}); |
68
|
|
|
foreach ($providerPropertiesCollectionIterator as $providerProperties) { |
69
|
|
|
/** @var AttributesInjectionProviderInterface $provider */ |
70
|
|
|
$provider = $providerProperties['provider']; |
71
|
|
|
if (!$provider->isEnabled()) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
$attributes = $provider->getAttributes(); |
75
|
|
|
if (empty($attributes)) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
foreach ($attributes as $name => $value) { |
79
|
|
|
switch (true) { |
80
|
|
|
case isset($attributeDefinitions[$name]): |
81
|
|
|
$attributeDefinition = $attributeDefinitions[$name]; |
82
|
|
|
break; |
83
|
|
|
case isset($lcIdOrAliasMap[$name], $attributeDefinitions[$lcIdOrAliasMap[$name]]): |
84
|
|
|
$attributeDefinition = $attributeDefinitions[$lcIdOrAliasMap[$name]]; |
85
|
|
|
break; |
86
|
|
|
default: |
87
|
|
|
continue 2; // switch is considered a looping structure, we have to continue the foreach |
88
|
|
|
} |
89
|
|
|
$id = $attributeDefinition['id']; |
90
|
|
|
$aliases = $attributeDefinition['aliases']; |
91
|
|
|
$server->set($id, (string)$value); |
92
|
|
|
foreach ($aliases as $alias) { |
93
|
|
|
$server->set($alias, (string)$value); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|