Skip to content

Commit 7df5395

Browse files
committed
Plugin: Azure: Add script to sync groups from Azure - refs BT#21930
1 parent 331d9fa commit 7df5395

File tree

3 files changed

+109
-5
lines changed

3 files changed

+109
-5
lines changed

plugin/azure_active_directory/src/AzureActiveDirectory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ public function getProvider()
9898
return $provider;
9999
}
100100

101+
public function getProviderForApiGraph(): Azure
102+
{
103+
$provider = $this->getProvider();
104+
$provider->urlAPI = "https://graph.microsoft.com/v1.0/";
105+
$provider->resource = "https://graph.microsoft.com/";
106+
$provider->tenant = $this->get(AzureActiveDirectory::SETTING_TENANT_ID);
107+
$provider->authWithResource = false;
108+
109+
return $provider;
110+
}
111+
101112
/**
102113
* @param string $urlType Type of URL to generate
103114
*
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/* For license terms, see /license.txt */
3+
4+
require __DIR__ . '/../../../../main/inc/global.inc.php';
5+
6+
if (PHP_SAPI !== 'cli') {
7+
exit('Run this script through the command line or comment this line in the code');
8+
}
9+
10+
$plugin = AzureActiveDirectory::create();
11+
12+
$provider = $plugin->getProviderForApiGraph();
13+
14+
echo 'Synchronizing groups from Azure.'.PHP_EOL;
15+
16+
try {
17+
$token = $provider->getAccessToken(
18+
'client_credentials',
19+
['resource' => $provider->resource]
20+
);
21+
22+
$groupFields = [
23+
'id',
24+
'displayName',
25+
'description',
26+
];
27+
28+
$azureGroupsInfo = $provider->get(
29+
'groups?$select='.implode(',', $groupFields),
30+
$token
31+
);
32+
} catch (Exception $e) {
33+
printf("%s - %s".PHP_EOL, time(), $e->getMessage());
34+
die;
35+
}
36+
37+
printf("%s - Number of groups obtained %d".PHP_EOL, time(), count($azureGroupsInfo));
38+
39+
/** @var array<string, string> $azureGroupInfo */
40+
foreach ($azureGroupsInfo as $azureGroupInfo) {
41+
$usergroup = new UserGroup();
42+
43+
$exists = $usergroup->usergroup_exists($azureGroupInfo['displayName']);
44+
45+
if (!$exists) {
46+
$groupId = $usergroup->save([
47+
'name' => $azureGroupInfo['displayName'],
48+
'description' => $azureGroupInfo['description'],
49+
]);
50+
51+
if ($groupId) {
52+
printf('%d - Class created: %s'.PHP_EOL, time(), $azureGroupInfo['displayName']);
53+
}
54+
} else {
55+
$groupId = $usergroup->getIdByName($azureGroupInfo['displayName']);
56+
57+
if ($groupId) {
58+
$usergroup->subscribe_users_to_usergroup($groupId, []);
59+
60+
printf('%d - Class exists, all users unsubscribed: %s'.PHP_EOL, time(), $azureGroupInfo['displayName']);
61+
}
62+
}
63+
64+
try {
65+
$userFields = [
66+
'mail',
67+
'mailNickname',
68+
'id'
69+
];
70+
$azureGroupMembers = $provider->get(
71+
sprintf('groups/%s/members?$select=%s', $azureGroupInfo['id'], implode(',', $userFields)),
72+
$token
73+
);
74+
} catch (Exception $e) {
75+
printf("%s - %s".PHP_EOL, time(), $e->getMessage());
76+
77+
continue;
78+
}
79+
80+
$newGroupMembers = [];
81+
82+
foreach ($azureGroupMembers as $azureGroupMember) {
83+
$userId = $plugin->getUserIdByVerificationOrder($azureGroupMember, 'id');
84+
85+
if ($userId) {
86+
$newGroupMembers[] = $userId;
87+
}
88+
}
89+
90+
$usergroup->subscribe_users_to_usergroup($groupId, $newGroupMembers);
91+
printf(
92+
'%d - User IDs subscribed in class %s: %s'.PHP_EOL,
93+
time(),
94+
$azureGroupInfo['displayName'],
95+
implode(', ', $newGroupMembers)
96+
);
97+
}

plugin/azure_active_directory/src/scripts/sync_users.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99

1010
$plugin = AzureActiveDirectory::create();
1111

12-
$provider = $plugin->getProvider();
13-
$provider->urlAPI = "https://graph.microsoft.com/v1.0/";
14-
$provider->resource = "https://graph.microsoft.com/";
15-
$provider->tenant = $plugin->get(AzureActiveDirectory::SETTING_TENANT_ID);
16-
$provider->authWithResource = false;
12+
$provider = $plugin->getProviderForApiGraph();
1713

1814
echo 'Synchronizing users from Azure.'.PHP_EOL;
1915

0 commit comments

Comments
 (0)