Skip to content

Commit 6949a07

Browse files
committed
Plugin: Azure: Add script to sync users from Azure - refs BT#21930
1 parent dc27ce5 commit 6949a07

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

plugin/azure_active_directory/lang/dutch.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242
$strings['group_id_teacher'] = 'Groeps-ID voor docenten';
4343
$strings['group_id_teacher_help'] = 'De groeps-ID voor docenten. Indien leeg, wordt er automatisch geen gebruiker aangemaakt als docent.';
4444
$strings['additional_interaction_required'] = 'Er is aanvullende interactie vereist om u te authenticeren. Log rechtstreeks in via <a href="https://login.microsoftonline.com" target="_blank">uw authenticatiesysteem</a> en kom dan terug naar deze pagina om in te loggen.';
45+
$strings['tenant_id'] = 'Mandanten-ID';
46+
$strings['tenant_id_help'] = 'Required to run scripts.';

plugin/azure_active_directory/lang/english.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242
$strings['group_id_teacher'] = 'Group ID for teachers';
4343
$strings['group_id_teacher_help'] = 'The group ID for teachers. If empty, no user will be automatically created as teacher.';
4444
$strings['additional_interaction_required'] = 'Some additional interaction is required to authenticate you. Please login directly through <a href="https://login.microsoftonline.com" target="_blank">your authentication system</a>, then come back to this page to login.';
45+
$strings['tenant_id'] = 'Tenant ID';
46+
$strings['tenant_id_help'] = 'Required to run scripts.';

plugin/azure_active_directory/lang/french.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242
$strings['group_id_teacher'] = 'ID du groupe enseignant';
4343
$strings['group_id_teacher_help'] = 'The group ID for teachers. Si ce champ est laissé vide, aucun utilisateur ne sera créé en tant qu\'enseignant.';
4444
$strings['additional_interaction_required'] = 'Une interaction supplémentaire est nécessaire pour vous authentifier. Veuillez vous connecter directement auprès de <a href="https://login.microsoftonline.com" target="_blank">votre système d\'authentification</a>, puis revenir ici pour vous connecter.';
45+
$strings['tenant_id'] = 'ID du client';
46+
$strings['tenant_id_help'] = 'Nécessaire pour exécuter des scripts.';

plugin/azure_active_directory/lang/spanish.php

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242
$strings['group_id_teacher'] = 'ID de grupo profesor';
4343
$strings['group_id_teacher_help'] = 'El ID de grupo para profesores. Si deja este campo vacío, ningún usuario será creado como profesor.';
4444
$strings['additional_interaction_required'] = 'Alguna interacción adicional es necesaria para identificarlo/a. Por favor conéctese primero a través de su <a href="https://login.microsoftonline.com" target="_blank">sistema de autenticación</a>, luego regrese aquí para logearse.';
45+
$strings['tenant_id'] = 'Id. del inquilino';
46+
$strings['tenant_id_help'] = 'Necesario para ejecutar scripts.';

plugin/azure_active_directory/src/AzureActiveDirectory.php

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AzureActiveDirectory extends Plugin
2626
public const SETTING_GROUP_ID_SESSION_ADMIN = 'group_id_session_admin';
2727
public const SETTING_GROUP_ID_TEACHER = 'group_id_teacher';
2828
public const SETTING_EXISTING_USER_VERIFICATION_ORDER = 'existing_user_verification_order';
29+
public const SETTING_TENANT_ID = 'tenant_id';
2930

3031
public const URL_TYPE_AUTHORIZE = 'login';
3132
public const URL_TYPE_LOGOUT = 'logout';
@@ -53,6 +54,7 @@ protected function __construct()
5354
self::SETTING_GROUP_ID_SESSION_ADMIN => 'text',
5455
self::SETTING_GROUP_ID_TEACHER => 'text',
5556
self::SETTING_EXISTING_USER_VERIFICATION_ORDER => 'text',
57+
self::SETTING_TENANT_ID => 'text',
5658
];
5759

5860
parent::__construct('2.3', 'Angel Fernando Quiroz Campos, Yannick Warnier', $settings);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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->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;
17+
18+
echo 'Synchronizing users from Azure.'.PHP_EOL;
19+
20+
try {
21+
$token = $provider->getAccessToken(
22+
'client_credentials',
23+
['resource' => $provider->resource]
24+
);
25+
26+
$userFields = [
27+
'givenName',
28+
'surname',
29+
'mail',
30+
'userPrincipalName',
31+
'businessPhones',
32+
'mobilePhone',
33+
'accountEnabled',
34+
'mailNickname',
35+
'id'
36+
];
37+
38+
$azureUsersInfo = $provider->get(
39+
'users?$select='.implode(',', $userFields),
40+
$token
41+
);
42+
} catch (Exception $e) {
43+
printf("%s - %s".PHP_EOL, time(), $e->getMessage());
44+
die;
45+
}
46+
47+
printf("%s - Number of users obtained %d".PHP_EOL, time(), count($azureUsersInfo));
48+
49+
/** @var array $user */
50+
foreach ($azureUsersInfo as $azureUserInfo) {
51+
try {
52+
$userId = $plugin->registerUser(
53+
$token,
54+
$provider,
55+
$azureUserInfo,
56+
'users/' . $azureUserInfo['id'] . '/memberOf',
57+
'id',
58+
'id'
59+
);
60+
61+
$userInfo = api_get_user_info($userId);
62+
63+
printf("%s - UserInfo %s".PHP_EOL, time(), serialize($userInfo));
64+
} catch (Exception $e) {
65+
printf("%s - %s".PHP_EOL, time(), $e->getMessage());
66+
67+
continue;
68+
}
69+
}

0 commit comments

Comments
 (0)