1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kuleuven\AuthenticationBundle\Service; |
4
|
|
|
|
5
|
|
|
use Kuleuven\AuthenticationBundle\Model\KuleuvenUser; |
6
|
|
|
use Kuleuven\AuthenticationBundle\Model\KuleuvenUserInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
8
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
9
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
10
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
11
|
|
|
|
12
|
|
|
class ShibbolethUserProvider implements UserProviderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AttributesByUsernameProviderInterface |
16
|
|
|
*/ |
17
|
|
|
protected $attributesProvider; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var AttributeDefinitionsProviderInterface |
21
|
|
|
*/ |
22
|
|
|
protected $attributeDefinitionsProvider; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param AttributesByUsernameProviderInterface $attributesProvider |
26
|
|
|
* @param AttributeDefinitionsProviderInterface $attributeDefinitionsProvider |
27
|
|
|
*/ |
28
|
|
|
public function __construct(AttributesByUsernameProviderInterface $attributesProvider, AttributeDefinitionsProviderInterface $attributeDefinitionsProvider) |
29
|
|
|
{ |
30
|
|
|
$this->attributesProvider = $attributesProvider; |
31
|
|
|
$this->attributeDefinitionsProvider = $attributeDefinitionsProvider; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
|
|
public function loadUserByUsername($username) |
38
|
|
|
{ |
39
|
|
|
$providerAttributes = $this->attributesProvider->getAttributesByUsername($username); |
40
|
|
|
if (empty($providerAttributes)) { |
41
|
|
|
throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$attributeDefinitions = $this->attributeDefinitionsProvider->getAttributeDefinitions(); |
45
|
|
|
$attributes = []; |
46
|
|
|
foreach ($attributeDefinitions as $idOrAlias => $attributeDefinition) { |
47
|
|
|
switch (true) { |
48
|
|
|
case isset($providerAttributes[$idOrAlias]): |
49
|
|
|
$value = $providerAttributes[$idOrAlias]; |
50
|
|
|
break; |
51
|
|
|
case isset($providerAttributes[strtolower($idOrAlias)]): |
52
|
|
|
$value = $providerAttributes[strtolower($idOrAlias)]; |
53
|
|
|
break; |
54
|
|
|
default: |
55
|
|
|
continue 2; // switch is considered a looping structure, we have to continue the foreach |
56
|
|
|
} |
57
|
|
|
$charset = isset($attributeDefinition['charset']) ? $attributeDefinition['charset'] : 'UTF-8'; |
58
|
|
|
if ($charset == 'UTF-8') { |
59
|
|
|
$value = utf8_decode($value); |
60
|
|
|
} |
61
|
|
|
if (isset($attributeDefinition['multivalue']) && $attributeDefinition['multivalue']) { |
62
|
|
|
$value = explode(';', $value); // $value is an array |
63
|
|
|
} |
64
|
|
|
$id = $attributeDefinition['id']; |
65
|
|
|
$aliases = $attributeDefinition['aliases']; |
66
|
|
|
$attributes[$id] = $value; |
67
|
|
|
foreach ($aliases as $alias) { |
68
|
|
|
$attributes[$alias] = $value; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new KuleuvenUser( |
73
|
|
|
$username, |
74
|
|
|
$attributes |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function refreshUser(UserInterface $user) |
82
|
|
|
{ |
83
|
|
|
if (!$this->supportsClass(get_class($user))) { |
84
|
|
|
throw new UnsupportedUserException(sprintf('Class "%s" should implement "%s".', get_class($user), KuleuvenUserInterface::class)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
try { |
88
|
|
|
$user = $this->loadUserByUsername($user->getUsername()); |
89
|
|
|
} catch (UsernameNotFoundException $exception) { |
90
|
|
|
throw new UnsupportedUserException($exception->getMessage(), $exception->getCode(), $exception); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $user; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritdoc |
98
|
|
|
*/ |
99
|
|
|
public function supportsClass($class) |
100
|
|
|
{ |
101
|
|
|
$interfaces = class_implements($class); |
102
|
|
|
return isset($interfaces[KuleuvenUserInterface::class]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|