Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Core/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
22 changes: 21 additions & 1 deletion Platform/Native/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*
Expand Down Expand Up @@ -410,4 +430,4 @@ protected function normalizeData($data)
}
return $data;
}
}
}