|
| 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 | +} |
0 commit comments