【问题标题】:PHP LDAP, adding new entries into LDAPPHP LDAP,将新条目添加到 LDAP
【发布时间】:2011-12-12 17:05:09
【问题描述】:

我想创建一个我可以填写的表单,一旦我提交它,表单值就可以被提取出来,并且那个人可以被创建到 LDAP 中。我对 LDAP 不是很有经验,事实上我只是致力于使 LDAP 绑定工作,所以我需要一些帮助。如何通过我可以填写的表格将新用户添加到 LDAP?我知道 LDAP 有一个添加命令,但我不确定如何开始以及需要传递哪些信息才能在 LDAP 中创建人。如果有帮助,下面是我的 LDAP 绑定代码。

<?php
$name=$_REQUEST['name'];
$x=1;
if($x==1)
{
    //LDAP stuff here.
    $username = "myusername";
    $password = "mypass";


    $ds = ldap_connect('ldap://ldap:389');

        ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);

    //Can't connect to LDAP.
    if( !ds )
    {
        echo "Error in contacting the LDAP server -- contact ";
        echo "technical services!  (Debug 1)";

        exit;
    }

    //Connection made -- bind anonymously and get dn for username.
    $bind = @ldap_bind($ds);

    //Check to make sure we're bound.
    if( !bind )
    {
        echo "Anonymous bind to LDAP FAILED.  Contact Tech Services! (Debug 2)";

        exit;
    }

    $search = ldap_search($ds, "ou=People,DC=sde,DC=goliat,DC=com", "uid=$username");

    //Make sure only ONE result was returned -- if not, they might've thrown a * into the username.  Bad user!
    if( ldap_count_entries($ds,$search) != 1 )
    {
        echo "Error processing username -- please try to login again. (Debug 3)";
        redirect(_WEBROOT_ . "/try1b.php");

        exit;
    }

    $info = ldap_get_entries($ds, $search);

    //Now, try to rebind with their full dn and password.
    $bind = @ldap_bind($ds, $info[0][dn], $password);
    if( !$bind || !isset($bind))
    {
        echo "Login failed -- please try again. (Debug 4)";
        redirect(_WEBROOT_ . "/try1b.php");

        exit;
    }

    //Now verify the previous search using their credentials.
    $search = ldap_search($ds, "ou=People,DC=sde,DC=goliat,DC=com", "cn=$name");

    $info = ldap_get_entries($ds, $search);
    if( $username == "myusername" )
    {

/*  
    very useful set of information to view the LDAP tree info from an array
    echo $username;
echo "<pre>".print_r($info[0],true)."</pre><br />";
*/
     echo $info[0][cn][0]; 
     echo ",";
     echo $info[0][mail][0];
     echo ",";
echo $info[0][telephonenumber][0];

        exit;
    }
    else
    {
        echo "Error. Access Denied";
        redirect(_WEBROOT_ . "/try1b.php");

        exit;
    }
    ldap_close($ds);
    exit;
}
?> 

【问题讨论】:

    标签: php directory ldap bind ldap-query


    【解决方案1】:

    我会推荐一个 newUser.php(或其他)文件来检查以确保您需要的所有信息都存在,然后将该信息发送到您在上面启动的文件中。

    您的$bind 应该包含三个变量...

    $bind = ldap_bind($ds, 'cn=root,dc=example,dc=com', secretPassword);

    有关通过 PHP 将人员添加到您的 LDAP 服务器的非常好的指南,请访问 http://www.php2python.com/wiki/function.ldap-add/

    祝你好运

    【讨论】:

      【解决方案2】:

      添加请求需要添加可分辨名称和作为条目一部分的属性,以及可选的请求控件。

      在另一个主题上,您的搜索具有子树范围,并且可能返回多个与用户名匹配的条目。没有理由在代码中指定的基础对象下的不同分支中不能有多个具有相同 RDN 的条目 - 除非您的目录服务器供应商已经实现了属性唯一性约束。

      【讨论】:

        猜你喜欢
        • 2010-09-26
        • 1970-01-01
        • 2010-11-07
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 2016-08-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多