|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\Acl\Provider; |
|
11
|
|
|
|
|
12
|
|
|
use fkooman\OAuth\Client\OAuthClient; |
|
13
|
|
|
use SURFnet\VPN\Server\Acl\ProviderInterface; |
|
14
|
|
|
|
|
15
|
|
|
class VootProvider implements ProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var \fkooman\OAuth\Client\OAuthClient */ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $vootUri; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(OAuthClient $client, $vootUri) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->client = $client; |
|
26
|
|
|
$this->vootUri = $vootUri; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the groups a user is a member of. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string userId the userID of the user to request the groups of |
|
33
|
|
|
* |
|
34
|
|
|
* @return array the groups as an array containing the keys "id" and |
|
35
|
|
|
* "displayName", empty array if no groups are available for this user |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getGroups($userId) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->client->setUserId($userId); |
|
40
|
|
|
|
|
41
|
|
|
// fetch the groups and extract the membership data |
|
42
|
|
|
return self::extractMembership( |
|
43
|
|
|
$this->fetchGroups() |
|
44
|
|
|
); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function fetchGroups() |
|
48
|
|
|
{ |
|
49
|
|
|
if (false === $response = $this->client->get('groups', $this->vootUri)) { |
|
50
|
|
|
return []; |
|
51
|
|
|
// XXX need to delete the token? |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (!$response->isOkay()) { |
|
55
|
|
|
// we should probably log some stuff here, but for now just assume |
|
56
|
|
|
// there are no groups for the user... |
|
57
|
|
|
return []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $response->json(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private static function extractMembership(array $responseData) |
|
64
|
|
|
{ |
|
65
|
|
|
$memberOf = []; |
|
66
|
|
|
foreach ($responseData as $groupEntry) { |
|
67
|
|
|
if (!is_array($groupEntry)) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
if (!array_key_exists('id', $groupEntry)) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
if (!is_string($groupEntry['id'])) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
$displayName = self::getDisplayName($groupEntry); |
|
77
|
|
|
|
|
78
|
|
|
$memberOf[] = [ |
|
79
|
|
|
'id' => $groupEntry['id'], |
|
80
|
|
|
'displayName' => $displayName, |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $memberOf; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private static function getDisplayName(array $groupEntry) |
|
88
|
|
|
{ |
|
89
|
|
|
if (!array_key_exists('displayName', $groupEntry)) { |
|
90
|
|
|
return $groupEntry['id']; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if (is_string($groupEntry['displayName'])) { |
|
94
|
|
|
return $groupEntry['displayName']; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
if (is_array($groupEntry['displayName'])) { |
|
98
|
|
|
if (array_key_exists('en', $groupEntry['displayName'])) { |
|
99
|
|
|
return $groupEntry['displayName']['en']; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return array_values($groupEntry['displayName'])[0]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $groupEntry['id']; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|