You are here

public function GroupUserUpdateProcessor::runQuery in Lightweight Directory Access Protocol (LDAP) 8.3

Same name and namespace in other branches
  1. 8.4 ldap_user/src/Processor/GroupUserUpdateProcessor.php \Drupal\ldap_user\Processor\GroupUserUpdateProcessor::runQuery()

Runs the updating mechanism.

Parameters

string $id: LDAP QueryEntity ID.

File

ldap_user/src/Processor/GroupUserUpdateProcessor.php, line 120

Class

GroupUserUpdateProcessor
Provides functionality to generically update existing users.

Namespace

Drupal\ldap_user\Processor

Code

public function runQuery($id) {
  $this->queryController = new QueryController($id);
  if (!$this
    ->constraintsValid()) {
    return;
  }

  // @TODO: Batch users as OrphanProcessor does.
  $this->queryController
    ->execute();
  $accountsToProcess = $this->queryController
    ->getRawResults();
  $attribute = $this->ldapServer
    ->get('user_attr');
  $this->logger
    ->notice('Processing @count accounts for periodic update.', [
    '@count' => $accountsToProcess['count'],
  ]);
  foreach ($accountsToProcess as $account) {
    if (isset($account[$attribute], $account[$attribute][0])) {
      $username = $account[$attribute][0];
      $match = $this->ldapServer
        ->matchUsernameToExistingLdapEntry($username);
      if ($match) {
        if (ExternalAuthenticationHelper::getUidFromIdentifierMap($username)) {
          $drupalAccount = $this->entityTypeManager
            ->getStorage('user')
            ->load(ExternalAuthenticationHelper::getUidFromIdentifierMap($username));
          $this->ldapDrupalUserProcessor
            ->drupalUserLogsIn($drupalAccount);

          // Reload since data has changed.
          $drupalAccount = $this->entityTypeManager
            ->getStorage('user')
            ->load($drupalAccount
            ->id());
          $this
            ->updateAuthorizations($drupalAccount);
          $this->detailLog
            ->log('Periodic update: @name updated', [
            '@name' => $username,
          ], 'ldap_user');
        }
        else {
          $result = $this->ldapDrupalUserProcessor
            ->provisionDrupalAccount([
            'name' => $username,
            'status' => TRUE,
          ]);
          if ($result) {
            $drupalAccount = $this->ldapDrupalUserProcessor
              ->getUserAccount();
            $this->ldapDrupalUserProcessor
              ->drupalUserLogsIn($drupalAccount);

            // Reload since data has changed.
            $drupalAccount = $this->entityTypeManager
              ->getStorage('user')
              ->load($drupalAccount
              ->id());
            $this
              ->updateAuthorizations($drupalAccount);
            $this->detailLog
              ->log('Periodic update: @name created', [
              '@name' => $username,
            ], 'ldap_user');
          }
        }
      }
    }
  }
  $this->state
    ->set('ldap_user_cron_last_group_user_update', strtotime('today'));
}