1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\OpenIdConnect\UserInfo\Claim; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\User\User; |
17
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccount; |
18
|
|
|
|
19
|
|
|
class ClaimManager |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Claim[] |
23
|
|
|
*/ |
24
|
|
|
private $claims = []; |
25
|
|
|
|
26
|
|
|
public function add(Claim $claim): void |
27
|
|
|
{ |
28
|
|
|
$this->claims[$claim->name()] = $claim; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Claim[] |
33
|
|
|
*/ |
34
|
|
|
public function list(): array |
35
|
|
|
{ |
36
|
|
|
return \array_keys($this->claims); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Claim[] |
41
|
|
|
*/ |
42
|
|
|
public function all(): array |
43
|
|
|
{ |
44
|
|
|
return $this->claims; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function has(string $claim): bool |
48
|
|
|
{ |
49
|
|
|
return \array_key_exists($claim, $this->claims); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function get(string $claim): Claim |
53
|
|
|
{ |
54
|
|
|
if (!$this->has($claim)) { |
55
|
|
|
throw new \InvalidArgumentException(\Safe\sprintf('Unsupported claim "%s".', $claim)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->claims[$claim]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getUserInfo(User $user, UserAccount $userAccount, array $claims, array $claimLocales): array |
62
|
|
|
{ |
63
|
|
|
$result = []; |
64
|
|
|
$claimLocales[] = null; |
65
|
|
|
foreach ($claims as $claimName => $config) { |
66
|
|
|
if ($this->has($claimName)) { |
67
|
|
|
$claim = $this->get($claimName); |
68
|
|
|
foreach ($claimLocales as $claimLocale) { |
69
|
|
|
if ($claim->isAvailableForUserAccount($user, $userAccount, $claimLocale)) { |
70
|
|
|
$value = $claim->getForUserAccount($user, $userAccount, $claimLocale); |
71
|
|
|
switch (true) { |
72
|
|
|
case \is_array($config) && \array_key_exists('value', $config): |
73
|
|
|
if ($claim === $config['value']) { |
74
|
|
|
$result[$claimName] = $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
break; |
78
|
|
|
case \is_array($config) && \array_key_exists('values', $config) && \is_array($config['values']): |
79
|
|
|
if (\in_array($claim, $config['values'], true)) { |
80
|
|
|
$result[$claimName] = $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
break; |
84
|
|
|
default: |
85
|
|
|
$result[$claimName] = $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $result; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|