1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For license terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Token\AccessTokenInterface; |
6
|
|
|
|
7
|
|
|
class AzureSyncUsergroupsCommand extends AzureCommand |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @throws Exception |
11
|
|
|
* |
12
|
|
|
* @return Generator<int, string> |
13
|
|
|
*/ |
14
|
|
|
public function __invoke(): Generator |
15
|
|
|
{ |
16
|
|
|
yield 'Synchronizing groups from Azure.'; |
17
|
|
|
|
18
|
|
|
$usergroup = new UserGroup(); |
19
|
|
|
|
20
|
|
|
$groupIdByUid = []; |
21
|
|
|
|
22
|
|
|
foreach ($this->getAzureGroups() as $azureGroupInfo) { |
23
|
|
|
if ($usergroup->usergroup_exists($azureGroupInfo['displayName'])) { |
24
|
|
|
$groupId = $usergroup->getIdByName($azureGroupInfo['displayName']); |
25
|
|
|
|
26
|
|
|
if ($groupId) { |
27
|
|
|
$usergroup->subscribe_users_to_usergroup($groupId, []); |
28
|
|
|
|
29
|
|
|
yield sprintf('Class exists, all users unsubscribed: %s', $azureGroupInfo['displayName']); |
30
|
|
|
} |
31
|
|
|
} else { |
32
|
|
|
$groupId = $usergroup->save([ |
33
|
|
|
'name' => $azureGroupInfo['displayName'], |
34
|
|
|
'description' => $azureGroupInfo['description'], |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
if ($groupId) { |
38
|
|
|
yield sprintf('Class created: %s', $azureGroupInfo['displayName']); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$groupIdByUid[$azureGroupInfo['id']] = $groupId; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
yield '----------------'; |
46
|
|
|
yield 'Subscribing users to groups'; |
47
|
|
|
|
48
|
|
|
foreach ($groupIdByUid as $azureGroupUid => $groupId) { |
49
|
|
|
$newGroupMembers = []; |
50
|
|
|
|
51
|
|
|
yield sprintf('Obtaining members for group (ID %d)', $groupId); |
52
|
|
|
|
53
|
|
|
foreach ($this->getAzureGroupMembers($azureGroupUid) as $azureGroupMember) { |
54
|
|
|
if ($userId = $this->plugin->getUserIdByVerificationOrder($azureGroupMember, 'id')) { |
55
|
|
|
$newGroupMembers[] = $userId; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($newGroupMembers) { |
60
|
|
|
$usergroup->subscribe_users_to_usergroup($groupId, $newGroupMembers); |
61
|
|
|
|
62
|
|
|
yield sprintf( |
63
|
|
|
'User IDs subscribed in class (ID %d): %s', |
64
|
|
|
$groupId, |
65
|
|
|
implode(', ', $newGroupMembers) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|