Creating an Alias via LDAP in eDirectory

Posted on October 1st, 2005 in Work by pete

I had to do some coding this week, the first time in a long while. The PHP script I was writing was a straightforward “lookup the MySQL table” type jobbie which then created an eDirectory account if the details supplied by the user matched what the database held. All well and good.

Then I had to create an alias to point to a user object. Straightforward enough, mormally, but how to do it via LDAP? There wasn’t much help available, but I got there in the end. Here’s the code that does the name and alias creation.

// prepare data for adding user account.
$password = randompassword();
$info["firstname"]=$firstname;
$info["sn"]=$surname;
$info["fullname"] = $firstname . ” ” . $surname;
$info["mail"]=$emailname . “@itnet.ie”;
$info["objectclass"]=”User”;
$info["userPassword"]=$password;
$info["description"]=”Banner ID: ” . $id;

// Build up the full user DN that we’re about to try to create.

$object_dn = “cn=” . $emailname . “, ou= ” . $year . “,ou=” . $context . $base;
if ($debug) {
echo “In createLDAPAccount: User data is ” . $object_dn . “
“;
}

// prepare data for adding alias to user account (NDS alias object)
$alias["objectclass"] = “aliasObject”;
$alias["aliasedObjectName"] = $object_dn;
$alias["cn"] = $id;
$alias_dn = “cn=” . $id . “,ou= ” . $year . “,ou=” . $context . $base;

// Check to see if this user exists already.

$filter=”(cn=$emailname)”;
$search_result=ldap_search($ds, $base_dn, $filter);
$existing_user = ldap_get_entries($ds, $search_result);

//User account exists. Print error message and stop.

if ($existing_user["count"] > 0) {
// if ($debug) { echo “
Duplicate name in directory:”; }
$resultspage->assign(”errortext”,”The requested login name is already taken, please try again”);
$resultspage->display(’template2.tpl’);
exit;
}
else
{
// add data to directory
if(!(ldap_add($ds, $object_dn, $info))) {
$resultspage->assign(’errortext’,”An attempt to add your account to the system failed. Please
contact the helpdesk”);
$resultspage->display(’template2.tpl’);
exit;
}
else {
// Add Alias in the same context….

if(!(ldap_add($ds, $alias_dn, $alias))) {
$resultspage->assign(’errortext’,”An attempt to add your alias to the system failed. Please contact the helpdesk”);
$resultspage->display(’template2.tpl’);
exit;

As an aside, I’m working too much and need some time off.