You are here

private function DrupalUserProcessor::userPictureFromLdapEntry in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_user/src/Processor/DrupalUserProcessor.php \Drupal\ldap_user\Processor\DrupalUserProcessor::userPictureFromLdapEntry()

Process user picture from LDAP entry.

Return value

array|null Drupal file object image user's thumbnail or NULL if none present or an error occurs.

1 call to DrupalUserProcessor::userPictureFromLdapEntry()
DrupalUserProcessor::setLdapBaseFields in ldap_user/src/Processor/DrupalUserProcessor.php
Sets the fields required by LDAP.

File

ldap_user/src/Processor/DrupalUserProcessor.php, line 503

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

private function userPictureFromLdapEntry() : ?array {
  $picture_attribute = $this->server
    ->getPictureAttribute();
  if (!$this->ldapEntry || !$picture_attribute || !$this->ldapEntry
    ->hasAttribute($picture_attribute, FALSE)) {
    return NULL;
  }
  $ldapUserPicture = $this->ldapEntry
    ->getAttribute($picture_attribute, FALSE)[0];
  $currentUserPicture = $this->account
    ->get('user_picture')
    ->getValue();
  if (empty($currentUserPicture)) {
    return $this
      ->saveUserPicture($this->account
      ->get('user_picture'), $ldapUserPicture);
  }

  /** @var \Drupal\file\Entity\File $file */
  $file = $this->entityTypeManager
    ->getStorage('file')
    ->load($currentUserPicture[0]['target_id']);
  if ($file && file_exists($file
    ->getFileUri())) {
    $file_data = file_get_contents($file
      ->getFileUri());
    if (md5($file_data) === md5($ldapUserPicture)) {

      // Same image, do nothing.
      return NULL;
    }
  }
  return $this
    ->saveUserPicture($this->account
    ->get('user_picture'), $ldapUserPicture);
}