You are here

public function SimpleLdapUser::save in Simple LDAP 7

Same name and namespace in other branches
  1. 7.2 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 213
Class defining a simple LDAP user.

Class

SimpleLdapUser
@file Class defining a simple LDAP user.

Code

public function save() {

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

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

  // 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.
    $this->server
      ->modify($this->dn, $this->attributes);
  }
  else {

    // Create new entry.
    try {
      $this->attributes['objectclass'] = array_values(simple_ldap_user_variable_get('simple_ldap_user_objectclass'));
      $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.
        $this->server
          ->modify($this->dn, $this->attributes);
      }
      else {
        throw $e;
      }
    }
  }

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