dolibarr/scripts/user/sync_users_ldap2dolibarr.php

326 lines
11 KiB
PHP
Raw Permalink Normal View History

Better Travis CI NEW: Cleaned up routines for better readability of both declaration and results. PHP versions now really covered. The old code forced install of PHP and didn't use Travis provided versions. This resulted in the process not being executed with the declared PHP version. Dropped MySQL in favor of MariaDB. This is now the FLOSS community standard. This should help avoid problems with buggy MySQL releases. Fast finish enabled to show results faster. Optimized tools installation with composer. The right version of the tool is installed for the PHP version under test. New PHP linter to check for syntax errors. Parallelized for better speed. Apache + PHP FPM for testing webservices. The previous mod_php configuration was not supported on Travis. New global DEBUG environment variable to show verbose output with configuration files content. IRC notification on #dolibarr@freenode for community awareness. FIXES: Bug in scripts preventing execution with environmentalized PHP. Wrong detection of MAIN_URL_ROOT under specific circumstances. $_SERVER["DOCUMENT_ROOT"] empty and $_SERVER["SCRIPT_NAME"] populated. Relative ignore directive in coding style ruleset to avoid bypassing test. Unit test errors without an exit status. This prevented the CI from properly detecting and reporting the error. TODOS: PostgreSQL support. This one is tricky since we only have a MySQL dump and the syntax is not directly compatible. SQLite support. Disabled in core at the moment. Nginx + PHP FPM support. Test webservices on the second most popular webserver. Run dev/* checks. We have a nice collection of scripts we could leverage. Check Javascript. Check CSS. Check SQL.
2015-12-11 05:08:32 +01:00
#!/usr/bin/env php
<?php
/**
* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file scripts/user/sync_users_ldap2dolibarr.php
* \ingroup ldap member
* \brief Script to update users into Dolibarr from LDAP
*/
2020-10-27 01:44:00 +01:00
2020-12-30 20:47:57 +01:00
if (!defined('NOSESSION')) {
define('NOSESSION', '1');
}
2020-10-27 01:44:00 +01:00
$sapi_type = php_sapi_name();
$script_file = basename(__FILE__);
$path = __DIR__.'/';
// Test if batch mode
if (substr($sapi_type, 0, 3) == 'cgi') {
echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
exit(1);
}
require_once $path."../../htdocs/master.inc.php";
require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
2013-06-05 16:12:07 +02:00
/**
* @var Conf $conf
* @var DoliDB $db
* @var HookManager $hookmanager
* @var Translate $langs
* @var User $user
*/
$langs->loadLangs(array("main", "errors"));
2013-05-15 11:24:18 +02:00
// Global variables
$version = DOL_VERSION;
$error = 0;
$forcecommit = 0;
$excludeuser = array();
$confirmed = 0;
$hookmanager->initHooks(array('cli'));
2013-05-15 11:24:18 +02:00
/*
* Main
*/
@set_time_limit(0);
print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
dol_syslog($script_file." launched with arg ".implode(',', $argv));
2013-01-16 15:54:07 +01:00
// List of fields to get from LDAP
2020-12-30 20:47:57 +01:00
$required_fields = array(
$conf->global->LDAP_KEY_USERS,
$conf->global->LDAP_FIELD_FULLNAME,
$conf->global->LDAP_FIELD_NAME,
$conf->global->LDAP_FIELD_FIRSTNAME,
$conf->global->LDAP_FIELD_LOGIN,
$conf->global->LDAP_FIELD_LOGIN_SAMBA,
$conf->global->LDAP_FIELD_PASSWORD,
$conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
$conf->global->LDAP_FIELD_PHONE,
$conf->global->LDAP_FIELD_FAX,
$conf->global->LDAP_FIELD_MOBILE,
// $conf->global->LDAP_FIELD_ADDRESS,
// $conf->global->LDAP_FIELD_ZIP,
// $conf->global->LDAP_FIELD_TOWN,
// $conf->global->LDAP_FIELD_COUNTRY,
2020-12-30 20:47:57 +01:00
$conf->global->LDAP_FIELD_MAIL,
$conf->global->LDAP_FIELD_TITLE,
$conf->global->LDAP_FIELD_DESCRIPTION,
$conf->global->LDAP_FIELD_SID
);
2013-01-16 15:54:07 +01:00
// Remove from required_fields all entries not configured in LDAP (empty) and duplicated
$required_fields = array_unique(array_values(array_filter($required_fields, "dolValidLdapElement3")));
2013-01-16 15:54:07 +01:00
if (!isset($argv[1])) {
print "Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
exit(1);
}
2014-07-05 11:50:25 +02:00
foreach ($argv as $key => $val) {
2020-12-30 20:47:57 +01:00
if ($val == 'commitiferror') {
$forcecommit = 1;
2020-12-30 20:47:57 +01:00
}
if (preg_match('/--server=([^\s]+)$/', $val, $reg)) {
$conf->global->LDAP_SERVER_HOST = $reg[1];
2020-12-30 20:47:57 +01:00
}
if (preg_match('/--excludeuser=([^\s]+)$/', $val, $reg)) {
$excludeuser = explode(',', $reg[1]);
2020-12-30 20:47:57 +01:00
}
if (preg_match('/-y$/', $val, $reg)) {
$confirmed = 1;
2020-12-30 20:47:57 +01:00
}
}
print "Mails sending disabled (useless in batch mode)\n";
$conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
print "\n";
print "----- Synchronize all records from LDAP database:\n";
2023-10-11 19:32:04 +02:00
print "host=" . getDolGlobalString('LDAP_SERVER_HOST')."\n";
print "port=" . getDolGlobalString('LDAP_SERVER_PORT')."\n";
print "login=" . getDolGlobalString('LDAP_ADMIN_DN')."\n";
2023-12-26 14:49:38 +01:00
print "pass=".preg_replace('/./i', '*', getDolGlobalString('LDAP_ADMIN_PASS'))."\n";
2023-10-11 19:32:04 +02:00
print "DN to extract=" . getDolGlobalString('LDAP_USER_DN')."\n";
2023-11-27 11:46:58 +01:00
if (getDolGlobalString('LDAP_FILTER_CONNECTION')) {
2023-10-11 19:32:04 +02:00
print 'Filter=(' . getDolGlobalString('LDAP_FILTER_CONNECTION').')'."\n"; // Note: filter is defined into function getRecords
2020-12-30 20:47:57 +01:00
} else {
2023-10-11 19:32:04 +02:00
print 'Filter=(' . getDolGlobalString('LDAP_KEY_USERS').'=*)'."\n";
2020-12-30 20:47:57 +01:00
}
print "----- To Dolibarr database:\n";
print "type=".$conf->db->type."\n";
print "host=".$conf->db->host."\n";
print "port=".$conf->db->port."\n";
print "login=".$conf->db->user."\n";
print "database=".$conf->db->name."\n";
print "----- Options:\n";
print "commitiferror=".$forcecommit."\n";
print "excludeuser=".implode(',', $excludeuser)."\n";
print "Mapped LDAP fields=".implode(',', $required_fields)."\n";
print "\n";
2014-07-05 11:50:25 +02:00
if (!$confirmed) {
print "Hit Enter to continue or CTRL+C to stop...\n";
$input = trim(fgets(STDIN));
}
2023-11-27 11:46:58 +01:00
if (!getDolGlobalString('LDAP_USER_DN')) {
print $langs->trans("Error").': '.$langs->trans("LDAP setup for users not defined inside Dolibarr");
exit(1);
}
// Load table of correspondence of countries
$hashlib2rowid = array();
$countries = array();
$sql = "SELECT rowid, code, label, active";
$sql .= " FROM ".MAIN_DB_PREFIX."c_country";
$sql .= " WHERE active = 1";
$sql .= " ORDER BY code ASC";
$resql = $db->query($sql);
if ($resql) {
$num = $db->num_rows($resql);
$i = 0;
if ($num) {
while ($i < $num) {
$obj = $db->fetch_object($resql);
if ($obj) {
// print 'Load cache for country '.strtolower($obj->label).' rowid='.$obj->rowid."\n";
$hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
$countries[$obj->rowid] = array('rowid' => $obj->rowid, 'label' => $obj->label, 'code' => $obj->code);
}
$i++;
}
}
} else {
dol_print_error($db);
exit(1);
}
$ldap = new Ldap();
$result = $ldap->connectBind();
if ($result >= 0) {
$justthese = array();
// We disable synchro Dolibarr-LDAP
$conf->global->LDAP_SYNCHRO_ACTIVE = 0;
$ldaprecords = $ldap->getRecords('*', getDolGlobalString('LDAP_USER_DN'), getDolGlobalString('LDAP_KEY_USERS'), $required_fields, 'user'); // Filter on 'user' filter param
if (is_array($ldaprecords)) {
$db->begin();
// Warning $ldapuser has a key in lowercase
foreach ($ldaprecords as $key => $ldapuser) {
// If login into exclude list, we discard record
2023-10-11 19:32:04 +02:00
if (in_array($ldapuser[getDolGlobalString('LDAP_FIELD_LOGIN')], $excludeuser)) {
print $langs->transnoentities("UserDiscarded").' # '.$key.': login='.$ldapuser[getDolGlobalString('LDAP_FIELD_LOGIN')].' --> Discarded'."\n";
continue;
}
$fuser = new User($db);
2013-05-15 11:24:18 +02:00
2023-12-26 14:49:38 +01:00
if (getDolGlobalString('LDAP_KEY_USERS') == getDolGlobalString('LDAP_FIELD_SID')) {
$fuser->fetch(0, '', $ldapuser[getDolGlobalString('LDAP_KEY_USERS')]); // Chargement du user concerné par le SID
2023-12-26 14:49:38 +01:00
} elseif (getDolGlobalString('LDAP_KEY_USERS') == getDolGlobalString('LDAP_FIELD_LOGIN')) {
$fuser->fetch(0, $ldapuser[getDolGlobalString('LDAP_KEY_USERS')]); // Chargement du user concerné par le login
}
// Propriete membre
$fuser->firstname = $ldapuser[getDolGlobalString('LDAP_FIELD_FIRSTNAME')] ?? '';
$fuser->lastname = $ldapuser[getDolGlobalString('LDAP_FIELD_NAME')] ?? '';
$fuser->login = $ldapuser[getDolGlobalString('LDAP_FIELD_LOGIN')] ?? '';
$fuser->pass = $ldapuser[getDolGlobalString('LDAP_FIELD_PASSWORD')] ?? '';
$fuser->pass_indatabase_crypted = $ldapuser[getDolGlobalString('LDAP_FIELD_PASSWORD_CRYPTED')] ?? '';
// $user->societe;
/*
* $fuser->address=$ldapuser[getDolGlobalString('LDAP_FIELD_ADDRESS')] ?? null;
* $fuser->zip=$ldapuser[getDolGlobalString('LDAP_FIELD_ZIP')] ?? null;
* $fuser->town=$ldapuser[getDolGlobalString('LDAP_FIELD_TOWN')] ?? null;
* $fuser->country=$ldapuser[getDolGlobalString('LDAP_FIELD_COUNTRY')] ?? null;
* $fuser->country_id=$countries[$hashlib2rowid[strtolower($fuser->country)]]['rowid'];
* $fuser->country_code=$countries[$hashlib2rowid[strtolower($fuser->country)]]['code'];
*/
$fuser->office_phone = $ldapuser[getDolGlobalString('LDAP_FIELD_PHONE')] ?? '';
$fuser->user_mobile = $ldapuser[getDolGlobalString('LDAP_FIELD_MOBILE')] ?? '';
$fuser->office_fax = $ldapuser[getDolGlobalString('LDAP_FIELD_FAX')] ?? '';
$fuser->email = $ldapuser[getDolGlobalString('LDAP_FIELD_MAIL')] ?? '';
$fuser->ldap_sid = $ldapuser[getDolGlobalString('LDAP_FIELD_SID')] ?? '';
$fuser->job = $ldapuser[getDolGlobalString('LDAP_FIELD_TITLE')] ?? '';
$fuser->note = $ldapuser[getDolGlobalString('LDAP_FIELD_DESCRIPTION')] ?? null;
$fuser->admin = 0;
2020-12-30 20:47:57 +01:00
$fuser->socid = 0;
$fuser->contact_id = 0;
$fuser->fk_member = 0;
$fuser->status = 1;
// TODO : revoir la gestion du status
/*
2023-12-26 14:49:38 +01:00
* if (isset($ldapuser[getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')])) {
* $fuser->datec=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
* $fuser->datevalid=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
* $fuser->statut=$ldapuser[getDolGlobalString('LDAP_FIELD_MEMBER_STATUS')] ?? null;
* }
*/
// if ($fuser->statut > 1) $fuser->statut=1;
// print_r($ldapuser);
if ($fuser->id > 0) { // User update
print $langs->transnoentities("UserUpdate").' # '.$key.': login='.$fuser->login.', fullname='.$fuser->getFullName($langs);
$res = $fuser->update($user);
if ($res < 0) {
$error++;
print ' --> '.$res.' '.$fuser->error;
} else {
print ' --> Updated user id='.$fuser->id.' login='.$fuser->login;
}
} else { // User creation
print $langs->transnoentities("UserCreate").' # '.$key.': login='.$fuser->login.', fullname='.$fuser->getFullName($langs);
$res = $fuser->create($user);
if ($res > 0) {
print ' --> Created user id='.$fuser->id.' login='.$fuser->login;
} else {
$error++;
print ' --> '.$res.' '.$fuser->error;
}
}
print "\n";
// print_r($fuser);
2013-05-15 11:24:18 +02:00
// Management of the groups
// TODO : Review the group management (or script for syncing groups)
/*
* if(!$error) {
2023-12-26 14:49:38 +01:00
* foreach ($ldapuser[getDolGlobalString('LDAP_FIELD_USERGROUPS') as $groupdn) {
* $groupdn;
* }
* }
*/
}
if (!$error || $forcecommit) {
2020-12-30 20:47:57 +01:00
if (!$error) {
print $langs->transnoentities("NoErrorCommitIsDone")."\n";
2020-12-30 20:47:57 +01:00
} else {
print $langs->transnoentities("ErrorButCommitIsDone")."\n";
}
$db->commit();
} else {
print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone", (string) $error)."\n";
$db->rollback();
}
print "\n";
} else {
2024-01-20 09:22:38 +01:00
dol_print_error(null, $ldap->error);
$error++;
}
} else {
2024-01-20 09:22:38 +01:00
dol_print_error(null, $ldap->error);
$error++;
}
exit($error);
2019-05-16 14:38:01 +02:00
/**
* Function to say if a value is empty or not
*
2019-05-16 14:38:01 +02:00
* @param string $element Value to test
* @return boolean True of false
*/
function dolValidLdapElement3($element)
{
return (trim($element) != '');
}