You are here

public function SimpleLdapRole::__set in Simple LDAP 7.2

Same name and namespace in other branches
  1. 7 simple_ldap_role/SimpleLdapRole.class.php \SimpleLdapRole::__set()

Magic __set() function.

Parameters

string $name: The name of the attribute to set.

mixed $value: The value to assigned to the given attribute.

File

simple_ldap_role/SimpleLdapRole.class.php, line 108
SimpleLdapRole class file.

Class

SimpleLdapRole
@file SimpleLdapRole class file.

Code

public function __set($name, $value) {
  $attribute_name = simple_ldap_role_variable_get('simple_ldap_role_attribute_name');
  switch ($name) {
    case 'attributes':
    case 'exists':
      break;
    case 'dn':
      if ($this->dn != $value) {
        try {

          // Validate the DN format before trying to use it.
          SimpleLdap::ldap_explode_dn($value);

          // Save the old DN so a move operation can be done during save().
          $this->move = $this->dn;
          $this->dn = $value;
          $this->dirty = TRUE;
        } catch (SimpleLdapException $e) {
        }
      }
      break;
    default:

      // Make sure $value is an array.
      if (!is_array($value)) {
        $value = array(
          $value,
        );
      }

      // Make sure $this->attributes[$name] exists.
      if (!isset($this->attributes[$name])) {
        $this->attributes[$name] = array();
      }

      // Compare the curent value with the given value.
      $diff1 = @array_diff($this->attributes[$name], $value);
      $diff2 = @array_diff($value, $this->attributes[$name]);

      // If there are any differences, update the current value.
      if (!empty($diff1) || !empty($diff2)) {
        $this->attributes[$name] = $value;
        $this->dirty = TRUE;

        // Reconstruct the DN if the RDN attribute was just changed.
        if ($name == $attribute_name) {
          $parts = SimpleLdap::ldap_explode_dn($this->dn);
          unset($parts['count']);
          $parts[0] = $attribute_name . '=' . $value[0];
          $this->move = $this->dn;
          $this->dn = implode(',', $parts);
        }
      }
  }
}