1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For license terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
use Chamilo\PluginBundle\Entity\AzureActiveDirectory\AzureSyncState; |
6
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
7
|
|
|
use League\OAuth2\Client\Token\AccessTokenInterface; |
8
|
|
|
use TheNetworg\OAuth2\Client\Provider\Azure; |
9
|
|
|
|
10
|
|
|
abstract class AzureCommand |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var AzureActiveDirectory |
14
|
|
|
*/ |
15
|
|
|
protected $plugin; |
16
|
|
|
/** |
17
|
|
|
* @var Azure |
18
|
|
|
*/ |
19
|
|
|
protected $provider; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->plugin = AzureActiveDirectory::create(); |
24
|
|
|
$this->plugin->get_settings(true); |
25
|
|
|
$this->provider = $this->plugin->getProviderForApiGraph(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws IdentityProviderException |
30
|
|
|
*/ |
31
|
|
|
protected function generateOrRefreshToken(?AccessTokenInterface &$token) |
32
|
|
|
{ |
33
|
|
|
if (!$token || ($token->getExpires() && !$token->getRefreshToken())) { |
34
|
|
|
$token = $this->provider->getAccessToken( |
35
|
|
|
'client_credentials', |
36
|
|
|
['resource' => $this->provider->resource] |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @throws Exception |
43
|
|
|
* |
44
|
|
|
* @return Generator<int, array<string, string>> |
45
|
|
|
*/ |
46
|
|
|
protected function getAzureUsers(): Generator |
47
|
|
|
{ |
48
|
|
|
$userFields = [ |
49
|
|
|
'givenName', |
50
|
|
|
'surname', |
51
|
|
|
'mail', |
52
|
|
|
'userPrincipalName', |
53
|
|
|
'businessPhones', |
54
|
|
|
'mobilePhone', |
55
|
|
|
'accountEnabled', |
56
|
|
|
'mailNickname', |
57
|
|
|
'id', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$getUsersDelta = 'true' === $this->plugin->get(AzureActiveDirectory::SETTING_GET_USERS_DELTA); |
61
|
|
|
|
62
|
|
|
if ($getUsersDelta) { |
63
|
|
|
$usersDeltaLink = $this->plugin->getSyncState(AzureSyncState::USERS_DATALINK); |
64
|
|
|
|
65
|
|
|
$query = $usersDeltaLink |
66
|
|
|
? $usersDeltaLink->getValue() |
67
|
|
|
: sprintf('$select=%s', implode(',', $userFields)); |
68
|
|
|
} else { |
69
|
|
|
$query = sprintf( |
70
|
|
|
'$top=%d&$select=%s', |
71
|
|
|
AzureActiveDirectory::API_PAGE_SIZE, |
72
|
|
|
implode(',', $userFields) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$token = null; |
77
|
|
|
|
78
|
|
|
do { |
79
|
|
|
$this->generateOrRefreshToken($token); |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
$azureUsersRequest = $this->provider->request( |
83
|
|
|
'get', |
84
|
|
|
$getUsersDelta ? "users/delta?$query" : "users?$query", |
85
|
|
|
$token |
86
|
|
|
); |
87
|
|
|
} catch (Exception $e) { |
88
|
|
|
throw new Exception('Exception when requesting users from Azure: '.$e->getMessage()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$azureUsersInfo = $azureUsersRequest['value'] ?? []; |
92
|
|
|
|
93
|
|
|
foreach ($azureUsersInfo as $azureUserInfo) { |
94
|
|
|
$azureUserInfo['mail'] = $azureUserInfo['mail'] ?? null; |
95
|
|
|
$azureUserInfo['surname'] = $azureUserInfo['surname'] ?? null; |
96
|
|
|
$azureUserInfo['givenName'] = $azureUserInfo['givenName'] ?? null; |
97
|
|
|
|
98
|
|
|
yield $azureUserInfo; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$hasNextLink = false; |
102
|
|
|
|
103
|
|
|
if (!empty($azureUsersRequest['@odata.nextLink'])) { |
104
|
|
|
$hasNextLink = true; |
105
|
|
|
$query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($getUsersDelta && !empty($azureUsersRequest['@odata.deltaLink'])) { |
109
|
|
|
$this->plugin->saveSyncState( |
110
|
|
|
AzureSyncState::USERS_DATALINK, |
111
|
|
|
parse_url($azureUsersRequest['@odata.deltaLink'], PHP_URL_QUERY), |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} while ($hasNextLink); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws Exception |
119
|
|
|
* |
120
|
|
|
* @return Generator<int, array<string, string>> |
121
|
|
|
*/ |
122
|
|
|
protected function getAzureGroups(): Generator |
123
|
|
|
{ |
124
|
|
|
$groupFields = [ |
125
|
|
|
'id', |
126
|
|
|
'displayName', |
127
|
|
|
'description', |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
$getUsergroupsDelta = 'true' === $this->plugin->get(AzureActiveDirectory::SETTING_GET_USERGROUPS_DELTA); |
131
|
|
|
|
132
|
|
|
if ($getUsergroupsDelta) { |
133
|
|
|
$usergroupsDeltaLink = $this->plugin->getSyncState(AzureSyncState::USERGROUPS_DATALINK); |
134
|
|
|
|
135
|
|
|
$query = $usergroupsDeltaLink |
136
|
|
|
? $usergroupsDeltaLink->getValue() |
137
|
|
|
: sprintf('$select=%s', implode(',', $groupFields)); |
138
|
|
|
} else { |
139
|
|
|
$query = sprintf( |
140
|
|
|
'$top=%d&$select=%s', |
141
|
|
|
AzureActiveDirectory::API_PAGE_SIZE, |
142
|
|
|
implode(',', $groupFields) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$token = null; |
147
|
|
|
|
148
|
|
|
do { |
149
|
|
|
$this->generateOrRefreshToken($token); |
150
|
|
|
|
151
|
|
|
try { |
152
|
|
|
$azureGroupsRequest = $this->provider->request( |
153
|
|
|
'get', |
154
|
|
|
$getUsergroupsDelta ? "groups/delta?$query" : "groups?$query", |
155
|
|
|
$token |
156
|
|
|
); |
157
|
|
|
} catch (Exception $e) { |
158
|
|
|
throw new Exception('Exception when requesting groups from Azure: '.$e->getMessage()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$azureGroupsInfo = $azureGroupsRequest['value'] ?? []; |
162
|
|
|
|
163
|
|
|
foreach ($azureGroupsInfo as $azureGroupInfo) { |
164
|
|
|
yield $azureGroupInfo; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$hasNextLink = false; |
168
|
|
|
|
169
|
|
|
if (!empty($azureGroupsRequest['@odata.nextLink'])) { |
170
|
|
|
$hasNextLink = true; |
171
|
|
|
$query = parse_url($azureGroupsRequest['@odata.nextLink'], PHP_URL_QUERY); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($getUsergroupsDelta && !empty($azureGroupsRequest['@odata.deltaLink'])) { |
175
|
|
|
$this->plugin->saveSyncState( |
176
|
|
|
AzureSyncState::USERGROUPS_DATALINK, |
177
|
|
|
parse_url($azureGroupsRequest['@odata.deltaLink'], PHP_URL_QUERY), |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
} while ($hasNextLink); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @throws Exception |
185
|
|
|
* |
186
|
|
|
* @return Generator<int, array<string, string>> |
187
|
|
|
*/ |
188
|
|
|
protected function getAzureGroupMembers(string $groupUid): Generator |
189
|
|
|
{ |
190
|
|
|
$userFields = [ |
191
|
|
|
'mail', |
192
|
|
|
'mailNickname', |
193
|
|
|
'id', |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
$query = sprintf( |
197
|
|
|
'$top=%d&$select=%s', |
198
|
|
|
AzureActiveDirectory::API_PAGE_SIZE, |
199
|
|
|
implode(',', $userFields) |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$token = null; |
203
|
|
|
|
204
|
|
|
do { |
205
|
|
|
$this->generateOrRefreshToken($token); |
206
|
|
|
|
207
|
|
|
try { |
208
|
|
|
$azureGroupMembersRequest = $this->provider->request( |
209
|
|
|
'get', |
210
|
|
|
"groups/$groupUid/members?$query", |
211
|
|
|
$token |
212
|
|
|
); |
213
|
|
|
} catch (Exception $e) { |
214
|
|
|
throw new Exception('Exception when requesting group members from Azure: '.$e->getMessage()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
$azureGroupMembers = $azureGroupMembersRequest['value'] ?? []; |
218
|
|
|
|
219
|
|
|
foreach ($azureGroupMembers as $azureGroupMember) { |
220
|
|
|
yield $azureGroupMember; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$hasNextLink = false; |
224
|
|
|
|
225
|
|
|
if (!empty($azureGroupMembersRequest['@odata.nextLink'])) { |
226
|
|
|
$hasNextLink = true; |
227
|
|
|
$query = parse_url($azureGroupMembersRequest['@odata.nextLink'], PHP_URL_QUERY); |
228
|
|
|
} |
229
|
|
|
} while ($hasNextLink); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|