diff --git a/ldap_servers/src/Entity/Server.php b/ldap_servers/src/Entity/Server.php index 372cd0c..99f30ac 100644 --- a/ldap_servers/src/Entity/Server.php +++ b/ldap_servers/src/Entity/Server.php @@ -923,11 +923,8 @@ class Server extends ConfigEntityBase implements ServerInterface, LdapProtocol { * binary, if applicable. */ public function userPuidFromLdapEntry($ldap_entry) { - if ($this->get('unique_persistent_attr') - && isset($ldap_entry[$this->get('unique_persistent_attr')]) - && is_scalar($ldap_entry[$this->get('unique_persistent_attr')]) - ) { - $puid = $ldap_entry[$this->get('unique_persistent_attr')]; + if ($this->get('unique_persistent_attr') && isset($ldap_entry[Unicode::strtolower($this->get('unique_persistent_attr'))])) { + $puid = $ldap_entry[Unicode::strtolower($this->get('unique_persistent_attr'))]; // If its still an array... if (is_array($puid)) { $puid = $puid[0]; diff --git a/ldap_servers/src/OrphanProcessor.php b/ldap_servers/src/OrphanProcessor.php index ad1704f..2199eeb 100644 --- a/ldap_servers/src/OrphanProcessor.php +++ b/ldap_servers/src/OrphanProcessor.php @@ -3,6 +3,7 @@ namespace Drupal\ldap_servers; use Drupal\ldap_user\LdapUserConf; +use Drupal\user\Entity\User; /** * @@ -43,40 +44,39 @@ class OrphanProcessor { * - order by uid asc (get oldest first) */ - $last_uid_checked = \Drupal::config('ldap_user.settings')->get('ldap_user_cron_last_uid_checked'); - - // @FIXME : replace by D8 entityQuery - // https://www.drupal.org/node/1827278 - // $query = new EntityFieldQuery(); - // $query->entityCondition('entity_type', 'user') - // ->fieldCondition('ldap_user_current_dn', 'value', 'NULL', '!=') - // ->propertyCondition('uid', $last_uid_checked, '>') - // ->propertyOrderBy('uid', 'ASC') - // ->range(0, $this->config['orphanedCheckQty'] - 1) - // ->addMetaData('account', \Drupal::entityManager()->getStorage('user')->load(1)); // run the query as user 1 - // $result = $query->execute(); + $last_uid_checked = \Drupal::state()->get('ldap_user_cron_last_uid_checked', 1); + + $query = \Drupal::entityQuery('user') + ->exists('ldap_user_current_dn') + ->condition('uid', $last_uid_checked, '>') + ->sort('uid', 'ASC') + ->range(0, $this->config['orphanedCheckQty']) + ->addMetaData('account', \Drupal::entityManager()->getStorage('user')->load(1)); // run the query as user 1 + $result = $query->execute(); + $drupal_users = array(); $factory = \Drupal::service('ldap.servers'); $ldap_servers = $factory->getEnabledServers(); $watchdogs_sids_missing_watchdogged = array(); + /** * first produce array of form: * $drupal_users[$sid][$puid_attr][$puid]['exists'] = FALSE | TRUE; * signifying if corresponding LDAP Entry exists */ - if (!(isset($result['user']) && count($result['user']) > 0)) { + if (empty($result)) { return TRUE; } - $uids = array_keys($result['user']); + $uids = $result; $user_count = count($uids); // If maxed out reset uid check counter. if ($user_count < $this->config['orphanedCheckQty']) { - \Drupal::configFactory()->getEditable('ldap_user.settings')->set('ldap_user_cron_last_uid_checked', 1)->save(); + \Drupal::state()->set('ldap_user_cron_last_uid_checked', 1); } else { - \Drupal::configFactory()->getEditable('ldap_user.settings')->set('ldap_user_cron_last_uid_checked', $uids[count($uids) - 1])->save(); + \Drupal::state()->set('ldap_user_cron_last_uid_checked', max($uids)); } $maxfilter_ors = 30; @@ -100,12 +100,12 @@ class OrphanProcessor { $end_plus_1 = min(($batch) * $maxfilter_ors, $user_count); // e.g. 50, 50; 100, 50. $batch_uids = array_slice($uids, $start, ($end_plus_1 - $start)); - $accounts = \Drupal::entityManager()->getStorage('user'); + $accounts = \Drupal::entityManager()->getStorage('user')->loadMultiple($batch_uids); foreach ($accounts as $uid => $user) { - $sid = @$user->ldap_user_puid_sid['und'][0]['value']; - $puid = @$user->ldap_user_puid['und'][0]['value']; - $puid_attr = @$user->ldap_user_puid_property['und'][0]['value']; + $sid = @$user->ldap_user_puid_sid->value; + $puid = @$user->ldap_user_puid->value; + $puid_attr = @$user->ldap_user_puid_property->value; if ($sid && $puid && $puid_attr) { if ($ldap_servers[$sid]->unique_persistent_attr_binary) { $filters[$sid][$puid_attr][] = "($puid_attr=" . $this->binaryFilter($puid) . ")"; @@ -154,34 +154,28 @@ class OrphanProcessor { } } // 3. we now have $drupal_users[$sid][$puid_attr][$puid]['exists'] = FALSE | TRUE;. - if ($this->config['orphanedDrupalAcctBehavior'] == 'ldap_user_orphan_email') { - $email_list = array(); - global $base_url; - } $check_time = time(); $email_list = array(); foreach ($drupal_users as $sid => $puid_x_puid_attrs) { foreach ($puid_x_puid_attrs as $puid_attr => $puids) { foreach ($puids as $puid => $user_data) { - // FIXME: Unported. - /* $account = $accounts[$user_data['uid']]; - $user_edit['ldap_user_last_checked'][0]['value'] = $check_time; - $account = user_save($account, $user_edit, 'ldap_user'); + $account = User::load($user_data['uid']); + $account->ldap_user_last_checked->value = $check_time; + $account->save(); if (!$user_data['exists']) { - /** - * $this->config['orphanedDrupalAcctBehavior'] will either be - * 'ldap_user_orphan_email' or one of the user module options: - * user_cancel_block, user_cancel_block_unpublish, - * user_cancel_reassign, user_cancel_delete - */ - /* if ($this->config['orphanedDrupalAcctBehavior'] == 'ldap_user_orphan_email') { - $email_list[] = $account->name . "," . $account->mail . "," . $base_url . "/user/$uid/edit"; - } - - else { - _user_cancel(array(), $account, $this->config['orphanedDrupalAcctBehavior']); + switch ($this->config['orphanedDrupalAcctBehavior']) { + case 'ldap_user_orphan_email'; + /** FIXME */ + // $email_list[] = $account->name . "," . $account->mail . "," . $base_url . "/user/$uid/edit"; + break; + case 'user_cancel_block': + case 'user_cancel_block_unpublish': + case 'user_cancel_reassign': + case 'user_cancel_delete': + _user_cancel(array(), $account, $this->config['orphanedDrupalAcctBehavior']); + break; + } } - }*/ } } } diff --git a/ldap_user/config/install/ldap_user.settings.yml b/ldap_user/config/install/ldap_user.settings.yml index fd49ef5..9ed4368 100644 --- a/ldap_user/config/install/ldap_user.settings.yml +++ b/ldap_user/config/install/ldap_user.settings.yml @@ -1,4 +1,3 @@ -ldap_user_cron_last_uid_checked: 1 ldap_user_conf: drupalAcctProvisionServer: '0' ldapEntryProvisionServer: '0' diff --git a/ldap_user/config/schema/ldap_user.schema.yml b/ldap_user/config/schema/ldap_user.schema.yml index ff98515..9859e70 100644 --- a/ldap_user/config/schema/ldap_user.schema.yml +++ b/ldap_user/config/schema/ldap_user.schema.yml @@ -1,8 +1,6 @@ ldap_user.settings: type: config_object mapping: - ldap_user_cron_last_uid_checked: - type: integer ldap_user_conf: type: mapping mapping: @@ -38,4 +36,4 @@ ldap_user.settings: type: mapping langcode: type: string - label: 'Default language' \ No newline at end of file + label: 'Default language'