Horde_Ldap::add PHP Метод

add() публичный Метод

This also links the entry to the connection used for the add, if it was a fresh entry.
См. также: HordeLdap_Entry::createFresh()
public add ( Horde_Ldap_Entry $entry )
$entry Horde_Ldap_Entry An LDAP entry.
    public function add(Horde_Ldap_Entry $entry)
    {
        /* Continue attempting the add operation in a loop until we get a
         * success, a definitive failure, or the world ends. */
        while (true) {
            $link = $this->getLink();
            if ($link === false) {
                /* We do not have a successful connection yet.  The call to
                 * getLink() would have kept trying if we wanted one. */
                throw new Horde_Ldap_Exception('Could not add entry ' . $entry->dn() . ' no valid LDAP connection could be found.');
            }
            if (@ldap_add($link, $entry->dn(), $entry->getValues())) {
                /* Entry successfully added, we should update its Horde_Ldap
                 * reference in case it is not set so far (fresh entry). */
                try {
                    $entry->getLDAP();
                } catch (Horde_Ldap_Exception $e) {
                    $entry->setLDAP($this);
                }
                /* Store that the entry is present inside the directory. */
                $entry->markAsNew(false);
                return;
            }
            /* We have a failure.  What kind?  We may be able to reconnect and
             * try again. */
            $error_code = @ldap_errno($link);
            if ($this->errorName($error_code) != 'LDAP_OPERATIONS_ERROR' | !$this->_config['auto_reconnect']) {
                /* Errors other than the above are just passed back to the user
                 * so he may react upon them. */
                throw new Horde_Ldap_Exception('Could not add entry ' . $entry->dn() . ': ' . ldap_err2str($error_code), $error_code);
            }
            /* The server has disconnected before trying the operation.  We
             * should try again, possibly with a different server. */
            $this->_link = false;
            $this->_reconnect();
        }
    }

Usage Example

Пример #1
0
 /**
  * Add a set of authentication credentials.
  *
  * @param string $userId      The userId to add.
  * @param array $credentials  The credentials to be set.
  *
  * @throws Horde_Auth_Exception
  */
 public function addUser($userId, $credentials)
 {
     if (!empty($this->_params['ad'])) {
         throw new Horde_Auth_Exception(__CLASS__ . ': Adding users is not supported for Active Directory.');
     }
     if (isset($credentials['ldap'])) {
         $entry = $credentials['ldap'];
         $dn = $entry['dn'];
         /* Remove the dn entry from the array. */
         unset($entry['dn']);
     } else {
         /* Try this simple default and hope it works. */
         $dn = $this->_params['uid'] . '=' . $userId . ',' . $this->_params['basedn'];
         $entry['cn'] = $userId;
         $entry['sn'] = $userId;
         $entry[$this->_params['uid']] = $userId;
         $entry['objectclass'] = array_merge(array('top'), $this->_params['newuser_objectclass']);
         $entry['userPassword'] = Horde_Auth::getCryptedPassword($credentials['password'], '', $this->_params['encryption'], 'true');
         if ($this->_params['password_expiration'] == 'yes') {
             $entry['shadowMin'] = $this->_params['minage'];
             $entry['shadowMax'] = $this->_params['maxage'];
             $entry['shadowWarning'] = $this->_params['warnage'];
             $entry['shadowLastChange'] = floor(time() / 86400);
         }
     }
     try {
         $this->_ldap->add(Horde_Ldap_Entry::createFresh($dn, $entry));
     } catch (Horde_Ldap_Exception $e) {
         throw new Horde_Auth_Exception(sprintf(__CLASS__ . ': Unable to add user "%s". This is what the server said: ', $userId) . $e->getMessage());
     }
 }
All Usage Examples Of Horde_Ldap::add