|
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
|
|
|
$attributeDefinition = null; |
|
|
|
|
|
|
80
|
|
|
switch (true) { |
|
81
|
|
|
case isset($attributeDefinitions[$name]): |
|
82
|
|
|
$attributeDefinition = $attributeDefinitions[$name]; |
|
83
|
|
|
break; |
|
84
|
|
|
case isset($lcIdOrAliasMap[$name], $attributeDefinitions[$lcIdOrAliasMap[$name]]): |
|
85
|
|
|
$attributeDefinition = $attributeDefinitions[$lcIdOrAliasMap[$name]]; |
|
86
|
|
|
break; |
|
87
|
|
|
default: |
|
88
|
|
|
continue 2; // switch is considered a looping structure, we have to continue the foreach |
|
89
|
|
|
} |
|
90
|
|
|
$id = $attributeDefinition['id']; |
|
91
|
|
|
$aliases = $attributeDefinition['aliases']; |
|
92
|
|
|
$server->set($id, (string)$value); |
|
93
|
|
|
foreach ($aliases as $alias) { |
|
94
|
|
|
$server->set($alias, (string)$value); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.