如何通过 PHP 在 OpenLDAP 中添加新用户?

How to add a new user in OpenLDAP via PHP?

我想在OpenLDAP中添加一个新用户,我写了下面的方法来创建一个新用户。调用此方法时,我必须传递以下变量:


public static function createNewUser($ldapconn, $username, $password) {
    if (!$ldapconn) { return false; }
    require_once("LDAPConfigurator.php");
    $r = ldap_bind($ldapconn, "cn=admin,dc=test,dc=com", "12345X");

    // Prepare data
    $info["cn"]="John Jones";
    $info["sn"]="Jones";
    $info["mail"]="jonj@example.com";
    $info["objectclass"]="person";

    // Add data to directory
    $r = ldap_add($ldapconn, "cn=John Jones,dc=test,dc=com", $info);

    return true;
}

LDAP 连接 $ldapconn 将在此方法之外关闭。


准备的数据是从this document复制过来的。

我希望它能在 phpLDAPadmin 中显示 "dc=test,dc=com" 下的一个新子项。但最后的结果是什么都没有出现并向我展示:

Warning: ldap_add(): Add: Object class violation in /var/www/html/test.php on line 23

我想请问如何解决这个问题(Object class violation)?

"inetOrgPerson" 是 OpenLDAP 中由 inetorgperson.schema 定义的人员条目的默认对象 class。所以你需要做的,

$info["objectclass"]="inetOrgPerson";

如果您想使用像 "person" 这样的自定义对象 class,您需要在架构中扩展 inetOrgPerson。