簡體   English   中英

PHP LDAP,將新條目添加到LDAP

[英]PHP LDAP, adding new entries into LDAP

我想創建一個可以填寫的表單,一旦提交,就可以提取表單值,並且可以將該人創建到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;
}
?> 

我建議您使用newUser.php(或其他任何文件)進行檢查,以確保所有必需的信息都存在,然后將該信息發送到上面已啟動的文件中。

您的$bind應該采用三個變量...

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

有關通過PHP將人員添加到LDAP服務器的相當不錯的指南,請訪問http://www.php2python.com/wiki/function.ldap-add/

祝好運

添加請求要求添加可分辨名稱以及將作為條目一部分的屬性,以及可選的請求控件。

在另一主題上,您的搜索具有子樹范圍,並且可能返回多個與用戶名匹配的條目。 沒有理由為什么在代碼中指定的基礎對象下的不同分支中不會有多個具有相同RDN的條目-除非目錄服務器供應商實現了屬性唯一性約束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM