You are here

public function SimpleLdapUser::save in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_user/SimpleLdapUser.class.php \SimpleLdapUser::save()

Save user to LDAP.

@throw SimpleLdapException

Return value

boolean TRUE on success.

File

simple_ldap_user/SimpleLdapUser.class.php, line 274
Class defining a simple LDAP user.

Class

SimpleLdapUser
@file Class defining a simple LDAP user.

Code

public function save() {

  // Move(rename) the entry if the DN was changed.
  if ($this->move) {
    $this->server
      ->move($this->move, $this->dn);
  }

  // If there is nothing to save, return "success".
  if (empty($this->dirty)) {
    return TRUE;
  }

  // Active Directory has some restrictions on what can be modified.
  if ($this->server->type == 'Active Directory') {
    $attribute_pass = simple_ldap_user_variable_get('simple_ldap_user_attribute_pass');
    $attribute_rdn = simple_ldap_user_variable_get('simple_ldap_user_attribute_rdn');

    // Passwords can only be changed over LDAPs.
    if (stripos($this->server->host, 'ldaps://') === FALSE) {
      unset($this->attributes[$attribute_pass]);
    }
    unset($this->attributes[$attribute_rdn]);
  }
  if ($this->exists) {

    // Update existing entry, writing out only changed values
    $this->server
      ->modify($this->dn, $this->dirty);
  }
  else {

    // Create new entry.
    try {
      $base_class = simple_ldap_user_variable_get('simple_ldap_user_objectclass');
      $object_classes = array(
        $base_class => $base_class,
      ) + simple_ldap_user_variable_get('simple_ldap_user_auxiliaryclasses');
      $this->attributes['objectclass'] = simple_ldap_user_parent_objectclasses($object_classes);
      $this->server
        ->add($this->dn, $this->attributes);
    } catch (SimpleLdapException $e) {
      if ($e
        ->getCode() == 68) {

        // An "already exists" error was returned, try to do a modify instead.
        // We don't know what is dirty, so write the whole record
        $this->server
          ->modify($this->dn, $this->attributes);
      }
      else {
        throw $e;
      }
    }
  }

  // No exceptions were thrown, so the save was successful.
  $this->exists = TRUE;
  $this->attributes += $this
    ->fetch_puid();
  $this->dirty = array();
  $this->move = FALSE;
  return TRUE;
}