You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
77 lines
2.3 KiB
<?php
|
|
|
|
/* For licensing terms, see /license.txt */
|
|
|
|
exit;
|
|
|
|
require_once __DIR__.'/../../app/config/auth.conf.php';
|
|
require_once __DIR__.'/../auth/external_login/ldap.inc.php';
|
|
require_once __DIR__.'/../auth/external_login/functions.inc.php';
|
|
|
|
global $extldap_config;
|
|
|
|
if (empty($extldap_config)) {
|
|
echo "$extldap_config not found";
|
|
exit;
|
|
}
|
|
|
|
$ds = extldap_connect();
|
|
if (!$ds) {
|
|
echo 'ldap not connected';
|
|
exit;
|
|
}
|
|
|
|
if (api_get_configuration_value('ldap_encrypt_admin_password')) {
|
|
$ldap_pass = api_decrypt_ldap_password($extldap_config['admin_password']);
|
|
} else {
|
|
$ldap_pass = $extldap_config['admin_password'];
|
|
}
|
|
$ldapbind = @ldap_bind($ds, $extldap_config['admin_dn'], $ldap_pass);
|
|
if ($ldapbind === false) {
|
|
echo 'EXTLDAP ERROR : cannot connect with admin login/password';
|
|
|
|
return false;
|
|
}
|
|
|
|
$table = Database::get_main_table(TABLE_MAIN_USER);
|
|
$sql = "SELECT * FROM $table WHERE auth_source = 'ldap' ";
|
|
$result = Database::query($sql);
|
|
while ($user = Database::fetch_array($result, 'ASSOC')) {
|
|
$userId = $user['id'];
|
|
$username = $user['username'];
|
|
$user_search = extldap_get_user_search_string($username);
|
|
$sr = ldap_search($ds, $extldap_config['base_dn'], $user_search);
|
|
if (!$sr) {
|
|
echo "Username not found in LDAP: ".$username.PHP_EOL;
|
|
continue;
|
|
}
|
|
$users = ldap_get_entries($ds, $sr);
|
|
$extraFieldUser = new ExtraFieldValue('user');
|
|
if (!empty($users)) {
|
|
echo "Updating user #".$userId.PHP_EOL;
|
|
for ($key = 0; $key < $users['count']; $key++) {
|
|
$ldapUser = $users[$key];
|
|
//print_r($ldapUser).PHP_EOL;
|
|
$params = [
|
|
'firstname' => $ldapUser['givenname'][0],
|
|
'lastname' => $ldapUser['sn'][0],
|
|
'email' => $ldapUser['mail'][0],
|
|
];
|
|
print_r($params).PHP_EOL;
|
|
Database::update($table, $params, ['id = ?' => $userId]);
|
|
|
|
$extraFields = [
|
|
'company' => $ldapUser['department'][0],
|
|
];
|
|
foreach ($extraFields as $variable => $value) {
|
|
$params = [
|
|
'item_id' => $userId,
|
|
'variable' => $variable,
|
|
'value' => $value,
|
|
];
|
|
print_r($params).PHP_EOL;
|
|
$extraFieldUser->save($params);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|