I've written some php codes (see codes below) to add a new user in our Active Directory.
I've attached the php codes below. It does actually add a new user and put in all the settings in attributes correctly.
However, even though I've set the userpassword (I can see the attribute set in ldap), the password is still blank when you first login.
If I set the unicodepwd attribute instead of userpassword, it generates error.
Any idea? Your advise will be much appreciated 🙂
// *************
// Add new AD user
// *************
$ldapconn = ldap_start();
$user["givenname"] = "Peter Alan";
$user["sn"] = "Pan";
$user["displayname"] = "Peter Pan";
$user["distinguishedname"] = "CN=PPan,OU=CKTest,DC=CK,DC=lan";
$user["homedirectory"] = '\\sal\home$\%username%';
$user["homedrive"] = "h:";
$user["samaccountname"] = "PPan";
$user["profilepath"] = '\\sal\profiles$\mandatory';
$user["objectcategory"] =
"CN=Person,CN=Schema,CN=Configuration,DC=CK,DC=lan";
$user['cn'] = "PPan";
$user["userprincipalname"] = $user["samaccountname"]."@DOMAIN";
$user['objectclass'][0] = "top";
$user['objectclass'][1] = "person";
$user['objectclass'][2] = "organizationalPerson";
$user['objectclass'][3] = "user";
$user['mail'] = "ppan@DOMAIN";
user['userPassword'] ='pwd';
//user['userPassword'] ='{MD5}'.base64_encode(pack('H',md5('pswd')));
// $user["unicodepwd"] = "{md5}".base64_encode(pack("H",md5("'pswd'")));
$user["userAccountControl"] = "544";
print_r($user);
$dn = "CN=PPan,OU=CKTest,DC=CK,DC=lan";
$result = ldap_add($ldapconn, $dn, $user);
//assign user to AllStudents group
$group_name = "CN=AllStudents,OU=Groups,OU=Students,DC=CK,DC=lan";
$group_info['member'] = $dn; // User's DN is added to group's 'member'array
ldap_mod_add($ldapconn,$group_name,$group_info);
if ($result)
{
echo "User added!";
}
else
{ echo "There was a problem!";}
ldap_end($ldapconn);
}