diff --git a/Core/Manager.php b/Core/Manager.php index 7df16ee..864adc5 100644 --- a/Core/Manager.php +++ b/Core/Manager.php @@ -328,6 +328,43 @@ public function save(Node $node) return false; } + /** + * Change Node's password (a LDAP user) + * + * @param Node $node Node to be changed + * @param string $password Node's (LDAP user) password + * @param string $newpassword Node's new password + * + * @return void + * + * @throws PersistenceException if entry could not be updated + * @throws NodeNotFoundException if Node not found + */ + public function changePassword(Node $node, $password, $newpassword) + { + $this->validateBinding(); + if (strlen(trim($node->getDn())) == 0) { + throw new PersistenceException('Cannot change password: dn missing for the entry'); + } + + if (!$node->isHydrated()) { + try { + $origin = $this->getNode($node->getDn()); + $node->rebaseDiff($origin); + } catch(NodeNotFoundException $e) { + $this->connection->addEntry($node->getDn(), $node->getRawAttributes()); + $node->snapshot(); + } + } + + // New bindindg with Node's parameters (user and password) + $this->bind($node->getDn(), $password); + $this->validateBinding(); + + $encodedNewPassword = "{SHA}" . base64_encode(pack("H*", sha1($_POST['newpassword']))); + $this->connection->changePassword($node->getDn(), $encodedNewPassword); + } + /** * Retrieves immediate children for the given node * diff --git a/Platform/Native/Connection.php b/Platform/Native/Connection.php index 42d359a..6c0c03c 100644 --- a/Platform/Native/Connection.php +++ b/Platform/Native/Connection.php @@ -242,6 +242,26 @@ public function addAttributeValues($dn, $data) } } + /** + * Change Node's password (a LDAP user) + * + * @param string $dn Distinguished name of the entry to modify + * @param string $newpassword Node's new password + * + * @return void + * + * @throws PersistenceException if entry could not be updated + */ + public function changePassword($dn, $newpassword) + { + if (ldap_modify($this->connection, $dn, ['userPassword' => "$newpassword"]) === false){ + $errno = ldap_errno($this->connection); + $message = 'Error changing password (' . $errno . ')'; + + throw new PersistenceException($message); + } + } + /** * Replaces value(s) for some entry attribute(s) * @@ -410,4 +430,4 @@ protected function normalizeData($data) } return $data; } -} \ No newline at end of file +}