You are here

public function DrupalUserProcessor::alterUserAttributes in Lightweight Directory Access Protocol (LDAP) 8.3

Alter the user's attributes.

Parameters

array $attributes: Attributes to change.

array $params: Parameters.

Return value

array Altered attributes.

File

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

Class

DrupalUserProcessor
Handles processing of a user from LDAP to Drupal.

Namespace

Drupal\ldap_user\Processor

Code

public function alterUserAttributes(array $attributes, array $params) {

  // Puid attributes are server specific.
  if (isset($params['sid']) && $params['sid']) {
    if (is_scalar($params['sid'])) {
      $ldapServer = $this->factory
        ->getServerByIdEnabled($params['sid']);
    }
    else {
      $ldapServer = $params['sid'];
    }
    if ($ldapServer) {
      if (!isset($attributes['dn'])) {
        $attributes['dn'] = [];
      }

      // Force dn "attribute" to exist.
      $attributes['dn'] = ConversionHelper::setAttributeMap($attributes['dn']);

      // Add the attributes required by the user configuration when
      // provisioning Drupal users.
      switch ($params['ldap_context']) {
        case 'ldap_user_insert_drupal_user':
        case 'ldap_user_update_drupal_user':
        case 'ldap_user_ldap_associate':
          if ($ldapServer
            ->get('user_attr')) {
            $attributes[$ldapServer
              ->get('user_attr')] = ConversionHelper::setAttributeMap(@$attributes[$ldapServer
              ->get('user_attr')]);
          }
          if ($ldapServer
            ->get('mail_attr')) {
            $attributes[$ldapServer
              ->get('mail_attr')] = ConversionHelper::setAttributeMap(@$attributes[$ldapServer
              ->get('mail_attr')]);
          }
          if ($ldapServer
            ->get('picture_attr')) {
            $attributes[$ldapServer
              ->get('picture_attr')] = ConversionHelper::setAttributeMap(@$attributes[$ldapServer
              ->get('picture_attr')]);
          }
          if ($ldapServer
            ->get('unique_persistent_attr')) {
            $attributes[$ldapServer
              ->get('unique_persistent_attr')] = ConversionHelper::setAttributeMap(@$attributes[$ldapServer
              ->get('unique_persistent_attr')]);
          }
          if ($ldapServer
            ->get('mail_template')) {
            ConversionHelper::extractTokenAttributes($attributes, $ldapServer
              ->get('mail_template'));
          }
          break;
      }
      $ldapContext = empty($params['ldap_context']) ? NULL : $params['ldap_context'];
      $direction = empty($params['direction']) ? $this
        ->ldapContextToProvDirection($ldapContext) : $params['direction'];
      $helper = new SyncMappingHelper();
      $attributesRequiredByOtherModuleMappings = $helper
        ->getLdapUserRequiredAttributes($direction, $ldapContext);
      $attributes = array_merge($attributesRequiredByOtherModuleMappings, $attributes);
      return $attributes;
    }
  }
  return $attributes;
}