# | 417 |Username | 418 |Updated | 419 |Status | 420 |
---|---|---|---|
#agent | 482 |" . $ad_users[$key]->samaccountname . " | 483 |" . db_affected_rows() . " | 484 |" . $this->log_report['status'] . " | contains($result, "ERROR")) echo 'style="background-color: beige;"'echo $result; < / td >
486 |
487 | }
488 | }
489 | }
490 |
491 | //***************Sync Users************************
492 | if ($this->config['sync-users']) {
493 | //Cleanup ID's with empty objectguid
494 | db_query("DELETE FROM " . TABLE_PREFIX . "ldap_sync WHERE guid IS NULL OR guid = ''");
495 |
496 | //Remove objectguid that is not in the ost_user table
497 | db_query("DELETE FROM `" . TABLE_PREFIX . "ldap_sync` WHERE NOT EXISTS (SELECT * FROM `" . TABLE_PREFIX . "user` WHERE id = " . TABLE_PREFIX . "ldap_sync.id);");
498 |
499 | //Update Global Array;
500 | $sql = "SELECT " . TABLE_PREFIX . "user.id as user_id, " . TABLE_PREFIX . "user_email.id as email_id," . TABLE_PREFIX . "user.name, " . TABLE_PREFIX . "user_email.address as mail, " . TABLE_PREFIX . "user_account.username , " . TABLE_PREFIX . "user_account.backend , " . TABLE_PREFIX . "user_account.status , " . TABLE_PREFIX . "ldap_sync.guid, " . TABLE_PREFIX . "ldap_sync.updated
501 | FROM " . TABLE_PREFIX . "user
502 | LEFT JOIN " . TABLE_PREFIX . "user_email on " . TABLE_PREFIX . "user.id=" . TABLE_PREFIX . "user_email.user_id
503 | LEFT JOIN " . TABLE_PREFIX . "user_account on " . TABLE_PREFIX . "user.id = " . TABLE_PREFIX . "user_account.user_id
504 | LEFT JOIN " . TABLE_PREFIX . "ldap_sync on " . TABLE_PREFIX . "user.id = " . TABLE_PREFIX . "ldap_sync.id WHERE " . TABLE_PREFIX . "user_account.backend = 'mldap.client". $this->config['backend'] ."';";
505 | $sync_array = db_query($sql);
506 | $g;
507 |
508 | foreach (db_assoc_array($sync_array, MYSQLI_ASSOC) as $sync) {
509 | $uid = $sync->config["user_id"];
510 | unset($sync->config["user_id"]);
511 | //Update any username changes
512 | if ($guiduser = $guid_users[$sync->config["guid"]]) {
513 | if (!empty($sync->config['username']))
514 | if ($guiduser->samaccountname !== $sync->config['username']){
515 | $this->update_username($guiduser->samaccountname, $uid);
516 | //$log_header .= "GUID: " . $guiduser->samaccountname ."-" . $sync->config['username'] . "";
517 | $g++;
518 | }
519 | }
520 | $this->sync_info[$uid] = $sync;
521 | }
522 |
523 | //Query only users that have no guid.
524 | $qry_ostusers = db_query("SELECT " . TABLE_PREFIX . "user.id as user_id,
525 | " . TABLE_PREFIX . "user_email.id as email_id," . TABLE_PREFIX . "user.name, " . TABLE_PREFIX . "user_email.address as mail
526 | FROM " . TABLE_PREFIX . "user LEFT JOIN " . TABLE_PREFIX . "user_email on " . TABLE_PREFIX . "user.id=" . TABLE_PREFIX . "user_email.user_id
527 | WHERE NOT EXISTS (select " . TABLE_PREFIX . "ldap_sync.id from " . TABLE_PREFIX . "ldap_sync
528 | WHERE user_id = " . TABLE_PREFIX . "ldap_sync.id);");
529 |
530 | // Go thru every osTicket user and add them to the sync table if a match is found
531 | foreach (db_assoc_array($qry_ostusers, MYSQLI_ASSOC) as $sql_ostusers) {
532 | $key = trim(strtolower($sql_ostusers['mail'])); //Key value for matching users
533 | $user_ldap = $ad_users[$key];
534 |
535 | if (strtolower($key == $user_ldap->mail)) {
536 | //Lets check users and add them to the guid table if a match is found
537 | $result = db_query("SELECT id FROM " . TABLE_PREFIX . "ldap_sync WHERE id = '" . $sql_ostusers['user_id'] . "'");
538 | if (db_num_rows($result) == 0 && $key == $ad_users[$key]->mail) {
539 | db_query("INSERT INTO " . TABLE_PREFIX . "ldap_sync(id, guid, updated)
540 | values ('" . $sql_ostusers['user_id'] . "', '" . $ad_users[$key]->objectguid . "', '" . date('Y-m-d H:i:s') . "')
541 | ON DUPLICATE KEY UPDATE id = \"" . $sql_ostusers['user_id'] . "\", guid = \"" . $ad_users[$key]->objectguid . "\", updated = \"" . date('Y-m-d H:i:s') . "\";");
542 | }
543 | } else {
544 | if (!empty($this->config['debug-choice']) && $this->config['debug-choice'])
545 | $this->log_report['debug']['notfound'][] = $sql_ostusers;
546 | }
547 | }
548 |
549 | //Go through and create accounts for new guest users verified in LDAP
550 | $sql_guests = "SELECT id, guid
551 | FROM " . TABLE_PREFIX . "ldap_sync
552 | WHERE NOT EXISTS (SELECT user_id FROM " . TABLE_PREFIX . "user_account WHERE user_id = " . TABLE_PREFIX . "ldap_sync.id);";
553 |
554 | $default_timezone = db_result(db_query("SELECT value FROM `" . TABLE_PREFIX . "config` WHERE `key` = 'default_timezone'"));
555 | $default_lang = db_result(db_query("SELECT value FROM `" . TABLE_PREFIX . "config` WHERE `key` = 'system_language'"));
556 |
557 | $qry_guests = db_query($sql_guests);
558 | foreach (db_assoc_array($qry_guests, MYSQLI_ASSOC) as $guests) {
559 | $key = $guests['guid'];
560 | db_query("INSERT INTO " . TABLE_PREFIX . "user_account(user_id, status, timezone, username, backend, extra, registered)
561 | values ('" . $guests['id'] . "',1, '$default_timezone', '" . $guid_users[$key]->samaccountname . "','mldap.client". $this->config['backend'] ."', '{\"browser_lang\":\"$default_lang\"}', '" . date('Y-m-d H:i:s') . "');");
562 | }
563 |
564 | //Update all users based on the ObjectID
565 | $sql_ostguid = db_query("SELECT * FROM `" . TABLE_PREFIX . "ldap_sync` WHERE guid IS NOT NULL");
566 |
567 | $updateusers;
568 | $sql_guid = db_assoc_array($sql_ostguid, MYSQLI_ASSOC);
569 |
570 | foreach ($sql_guid as $guid) {
571 | $guidkey = $guid['guid']; //objectguid for matching users.
572 | //Get UserID based on objectguid
573 | if (!empty($guid_users["$guidkey"])){
574 | $guid_users["$guidkey"]->user_id = $guid['id'];
575 | if ($_REQUEST['full'] || $this->config['sync_full']) {
576 | $updateusers[] = $guid_users["$guidkey"];
577 | }
578 | elseif ($guid['updated'] !== $this->changetime($guid_users["$guidkey"]->whenchanged)) {
579 | $updateusers[] = $guid_users["$guidkey"];
580 | }
581 | }
582 | }
583 |
584 | $log_header = ("(" . db_num_rows($qry_ostusers) . ') users not in ldap. '); 585 | $log_header = ("(" . (!empty($g) ? $g : '0') . ') users renamed. '); 586 | $log_header = ("(" . (is_countable($this->sync_info) ? count($this->sync_info) : '0') . ') total users. '); 587 | $log_header = ("(" . (is_countable($updateusers) ? count($updateusers) : '0'). ') users synced. '); 588 | $this->sync_results['updatedusers'] = (is_countable($updateusers) ? count($updateusers) : '0'); 589 | $this->sync_results['totalusers'] = (is_countable($this->sync_info) ? count($this->sync_info) : '0'); 590 | 591 | //update all user attributes. 592 | $this->update_users($updateusers); 593 | } 594 | //$this->logger('debug', '', , false); 595 | //execution time of the script 596 | $execution_time = $this->formatmilliseconds(number_format(microtime(true) - $sync_time_start, 3) * 1000); 597 | $log_footer = ' |
598 |