|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hiam\mrdp\providers; |
|
4
|
|
|
|
|
5
|
|
|
use hiam\mrdp\models\Identity; |
|
6
|
|
|
use hiam\providers\ClaimsProviderInterface; |
|
7
|
|
|
use yii\helpers\ArrayHelper; |
|
8
|
|
|
use yii\web\IdentityInterface; |
|
9
|
|
|
|
|
10
|
|
|
class ClaimsProvider implements ClaimsProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public function getClaims(IdentityInterface $identity, string $claims): \stdClass |
|
13
|
|
|
{ |
|
14
|
|
|
$claimed = explode(' ', trim($claims)); |
|
15
|
|
|
|
|
16
|
|
|
$response = []; |
|
17
|
|
|
foreach ($this->claimBuilders() as $claim => $provider) { |
|
18
|
|
|
if (\in_array($claim, $claimed, true)) { |
|
19
|
|
|
$response = array_merge($response, $provider($identity)); |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return (object)$response; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function claimBuilders(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return [ |
|
29
|
|
|
'profile' => function (Identity $identity) { |
|
30
|
|
|
return ArrayHelper::toArray($identity, [ |
|
31
|
|
|
Identity::class => [ |
|
32
|
|
|
'name' => function ($model) { |
|
33
|
|
|
return trim("{$model->first_name} {$model->last_name}"); |
|
34
|
|
|
}, |
|
35
|
|
|
'username', 'email', 'email_confirmed', |
|
36
|
|
|
], |
|
37
|
|
|
]); |
|
38
|
|
|
}, |
|
39
|
|
|
'email' => function (Identity $identity) { |
|
40
|
|
|
return ArrayHelper::toArray($identity, [ |
|
41
|
|
|
Identity::class => ['email', 'email_confirmed'], |
|
42
|
|
|
]); |
|
43
|
|
|
}, |
|
44
|
|
|
'roles' => function (Identity $identity) { |
|
45
|
|
|
return ArrayHelper::toArray($identity, [ |
|
46
|
|
|
Identity::class => ['roles'], |
|
47
|
|
|
]); |
|
48
|
|
|
}, |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|