dolibarr 24.0.0-beta
sync_users_dolibarr2ldap.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
29if (!defined('NOSESSION')) {
30 define('NOSESSION', '1');
31}
32
33$sapi_type = php_sapi_name();
34$script_file = basename(__FILE__);
35$path = __DIR__.'/';
36
37// Test if batch mode
38if (substr($sapi_type, 0, 3) == 'cgi') {
39 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
40 exit(1);
41}
42
43if (!isset($argv[1]) || !$argv[1]) {
44 print "Usage: $script_file now\n";
45 exit(1);
46}
47$now = $argv[1];
48
49require_once $path."../../htdocs/master.inc.php";
50require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
51require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
52require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
53
62// Global variables
63$version = DOL_VERSION;
64$error = 0;
65
66$hookmanager->initHooks(array('cli'));
67
68
69/*
70 * Main
71 */
72
73@set_time_limit(0);
74print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
75dol_syslog($script_file." launched with arg ".implode(',', $argv));
76
77/*
78 * if (! getDolGlobalString('LDAP_SYNCHRO_ACTIVE')) {
79 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
80 * exit(1);
81 * }
82 */
83
84$sql = "SELECT rowid";
85$sql .= " FROM ".MAIN_DB_PREFIX."user";
86
87$resql = $db->query($sql);
88if ($resql) {
89 $num = $db->num_rows($resql);
90 $i = 0;
91
92 $ldap = new Ldap();
93 $ldap->connectBind();
94
95 while ($i < $num) {
96 $ldap->error = "";
97
98 $obj = $db->fetch_object($resql);
99
100 $fuser = new User($db);
101 $fuser->fetch($obj->rowid);
102
103 print $langs->trans("UpdateUser")." rowid=".$fuser->id." ".$fuser->getFullName($langs);
104
105 $oldobject = $fuser;
106
107 $oldinfo = $oldobject->_load_ldap_info();
108 $olddn = $oldobject->_load_ldap_dn($oldinfo);
109
110 $info = $fuser->_load_ldap_info();
111 $dn = $fuser->_load_ldap_dn($info);
112
113 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
114 $result = $ldap->update($dn, $info, $user, $olddn);
115 if ($result > 0) {
116 print " - ".$langs->trans("OK");
117 } else {
118 $error++;
119 print " - ".$langs->trans("KO").' - '.$ldap->error;
120 }
121 print "\n";
122
123 $i++;
124 }
125
126 $ldap->unbind();
127} else {
129}
130
131exit($error);
Class to manage LDAP features.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.