|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For license terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Token\AccessTokenInterface; |
|
6
|
|
|
|
|
7
|
|
|
class AzureSyncUsersCommand extends AzureCommand |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @throws Exception |
|
11
|
|
|
* |
|
12
|
|
|
* @return Generator<int, string> |
|
13
|
|
|
*/ |
|
14
|
|
|
public function __invoke(): Generator |
|
15
|
|
|
{ |
|
16
|
|
|
yield 'Synchronizing users from Azure.'; |
|
17
|
|
|
|
|
18
|
|
|
$token = $this->getToken(); |
|
19
|
|
|
|
|
20
|
|
|
$existingUsers = []; |
|
21
|
|
|
|
|
22
|
|
|
foreach ($this->getAzureUsers($token) as $azureUserInfo) { |
|
23
|
|
|
try { |
|
24
|
|
|
$token = $this->getToken($token); |
|
25
|
|
|
|
|
26
|
|
|
$userId = $this->plugin->registerUser( |
|
27
|
|
|
$token, |
|
28
|
|
|
$this->provider, |
|
29
|
|
|
$azureUserInfo, |
|
30
|
|
|
'users/'.$azureUserInfo['id'].'/memberOf', |
|
31
|
|
|
'id', |
|
32
|
|
|
'id' |
|
33
|
|
|
); |
|
34
|
|
|
} catch (Exception $e) { |
|
35
|
|
|
yield $e->getMessage(); |
|
36
|
|
|
|
|
37
|
|
|
continue; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$existingUsers[] = $userId; |
|
41
|
|
|
|
|
42
|
|
|
$userInfo = api_get_user_info($userId); |
|
43
|
|
|
|
|
44
|
|
|
yield sprintf('User info: %s', serialize($userInfo)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ('true' === $this->plugin->get(AzureActiveDirectory::SETTING_DEACTIVATE_NONEXISTING_USERS)) { |
|
48
|
|
|
yield '----------------'; |
|
49
|
|
|
|
|
50
|
|
|
yield 'Trying deactivate non-existing users in Azure'; |
|
51
|
|
|
|
|
52
|
|
|
$users = UserManager::getRepository()->findByAuthSource('azure'); |
|
53
|
|
|
$userIdList = array_map( |
|
54
|
|
|
function ($user) { |
|
55
|
|
|
return $user->getId(); |
|
56
|
|
|
}, |
|
57
|
|
|
$users |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$nonExistingUsers = array_diff($userIdList, $existingUsers); |
|
61
|
|
|
|
|
62
|
|
|
UserManager::deactivate_users($nonExistingUsers); |
|
63
|
|
|
|
|
64
|
|
|
yield sprintf( |
|
65
|
|
|
'Deactivated users IDs: %s', |
|
66
|
|
|
implode(', ', $nonExistingUsers) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws Exception |
|
73
|
|
|
* |
|
74
|
|
|
* @return Generator<int, array<string, string>> |
|
75
|
|
|
*/ |
|
76
|
|
|
private function getAzureUsers(AccessTokenInterface $token): Generator |
|
77
|
|
|
{ |
|
78
|
|
|
$userFields = [ |
|
79
|
|
|
'givenName', |
|
80
|
|
|
'surname', |
|
81
|
|
|
'mail', |
|
82
|
|
|
'userPrincipalName', |
|
83
|
|
|
'businessPhones', |
|
84
|
|
|
'mobilePhone', |
|
85
|
|
|
'accountEnabled', |
|
86
|
|
|
'mailNickname', |
|
87
|
|
|
'id', |
|
88
|
|
|
]; |
|
89
|
|
|
|
|
90
|
|
|
$query = sprintf( |
|
91
|
|
|
'$top=%d&$select=%s', |
|
92
|
|
|
AzureActiveDirectory::API_PAGE_SIZE, |
|
93
|
|
|
implode(',', $userFields) |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
do { |
|
97
|
|
|
$token = $this->getToken($token); |
|
98
|
|
|
|
|
99
|
|
|
try { |
|
100
|
|
|
$azureUsersRequest = $this->provider->request( |
|
101
|
|
|
'get', |
|
102
|
|
|
"users?$query", |
|
103
|
|
|
$token |
|
104
|
|
|
); |
|
105
|
|
|
} catch (Exception $e) { |
|
106
|
|
|
throw new Exception('Exception when requesting users from Azure: '.$e->getMessage()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$azureUsersInfo = $azureUsersRequest['value'] ?? []; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($azureUsersInfo as $azureUserInfo) { |
|
112
|
|
|
yield $azureUserInfo; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$hasNextLink = false; |
|
116
|
|
|
|
|
117
|
|
|
if (!empty($azureUsersRequest['@odata.nextLink'])) { |
|
118
|
|
|
$hasNextLink = true; |
|
119
|
|
|
$query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY); |
|
120
|
|
|
} |
|
121
|
|
|
} while ($hasNextLink); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|