Webインターフェイスを介してADユーザーを作成する





すべてのWeb開発者がADに遭遇するわけではありません。それは phpを介してldapを操作するモジュールがまずサーバー管理者を構成する必要があるためです。 しかし、時々これはまだ起こります-会社には優れた管理者と従業員の集中管理の必要性があります。



簡単に言えば、これはまさにADが行うことです。従業員に関する情報を保存し、MySQLなどのデータベースとしてさまざまなアプリケーションやサービスに提供します。 ADのセットアップと管理を担当する従業員がいる場合もありますが、Webインターフェースを介したアクセスの実装は、Webプログラマーの予想通りになります。 そして、ここで特定の困難が生じるかもしれません。



ADの一般原則を理解し、たとえば、データベースに詳しい人のために会社の構造をブラウザに出力するなどの実装を行うと、非常に簡単になります。入力を整理することはすでに難しくなります。 実際、実際のADスペシャリストはデスクトップアプリケーション、コンソール、VBScript(と思われる)を使用してユーザーをオブジェクトとして操作し、PHPプログラマーが使用できるのはデータ配列形式の一部の機能とユーザー転送のみです。 書き込むキーと値の形式を決定することは、最も簡単な作業ではありません。 ネットワーク上のldap_add()関数のユースケース1つしかないためです。



したがって、別のサンプルを作成することが重要です。詳細なサンプルと「ボディキット」全体を使用して、大幅な変更なしに作業を確認できます。



サーバーが既に構成されており、アクセスが受信され、ユーザーに書き込み権限が付与されていると想定しています。 最初の段階-接続-は、マニュアルで十分に詳細に説明されています。



/* Connection parameters */ $ldap_host = "xxx.local"; # domain controller or ip-address $ldap_port = "389"; # port for connection $ldap_user ="admin@xxx.local"; # AD-user $ldap_pass = "123321";# AD-user's password /* Open connection */ $connect = ldap_connect( $ldap_host, $ldap_port); ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); $bind = ldap_bind($connect, $ldap_user, $ldap_pass);
      
      







次に、出力を確認し、会社の部門の階層ツリーを構築します。



 /* Hierarchical tree */ function build_tree($connect, $ou = '', $base_dn = 'OU=!Users_all,DC=xxx,DC=local') { $specific_dn = $base_dn; if ('' != $ou) {$specific_dn = 'OU='.$ou.','.$base_dn;} $filter = 'ou=*'; $justthese = array("ou"); $sr = ldap_list($connect, $specific_dn, $filter, $justthese); $info = ldap_get_entries($connect, $sr); $result = ''; for ($i=0; $i < $info["count"]; $i++) { $specific_ou = $info[$i]["ou"][0]; $result .= '<ul>'; $result .= '<li><input type="radio" name="dep" value="OU='.$specific_ou.','.$specific_dn.'" title="'.$specific_ou.'" /> '.$specific_ou; $result .= build_tree($connect, $specific_ou, $specific_dn); $result .= '</li>'; $result .= '</ul>'; } return $result; }
      
      







出力では、次のステップで必要になるフォーム要素をすぐに追加します。 関数はそれ自体を呼び出します。これにより、あらゆるレベルのネストを実行できます。

すべてがうまくいけば、従業員を追加するための簡単なフォームを書きます。



 /* Output */ echo ' <form method="POST" action="'.$_SERVER['REQUEST_URI'].'"> <h3></h3> <input type="hidden" name="new_emp" value="1" /> <input type="text" name="family_name" value="" size="20" /> <input type="text" name="first_name" value="" size="20" /> <input type="text" name="fathers_name" value="" size="20" /> <br/> <h3></h3> <input type="text" name="position" value="" size="40" /> <br/> <h3>  </h3> <input type="text" name="tel_num" value="+7(999) 999-9999" size="20" /> <h3></h3> <div class="tree"> '.build_tree($connect).' </div> <br/> <input type="submit" value="" /> </form>';
      
      







そして今、重要な部分:入力のチェックと処理



 /* Input check */ $errors = ''; /* EXAMPLE OF CORRECT POST new_emp: 1 family_name:  first_name:  fathers_name:  position:  tel_num: +7(999) 999-9999 dep:      */ if(isset($_POST['new_emp'])) { foreach($_POST as $key => $value) { if('' == $value) {$errors .= ' '.$key.'  <br/>'; break;} else {$$key = $value;} } if(!isset($_POST['dep'])) {$errors .= ' «»  <br/>';} /* Creating employee */ if('' == $errors) { $trans_family_name = translit($family_name); $trans_first_name = translit($first_name); $trans_fathers_name = translit($fathers_name); $account = $trans_family_name.substr($trans_first_name, 0, 1).substr($trans_fathers_name, 0, 1); $dn = 'CN='.$family_name.' '.$first_name.' '.$fathers_name.','.$dep; //Getting manager $filter = '(&(objectClass=user)(objectCategory=PERSON))'; $sr = ldap_search($connect, $dep, $filter); $info = ldap_get_entries($connect, $sr); //echo Arratomy($info); $manager = $info[0]['manager'][0]; $department = explode(',', $dep); $department = str_replace('OU=', '', $department[0]); $newuser['cn'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser["objectclass"][0] = "top"; $newuser["objectclass"][1] = "person"; $newuser["objectclass"][2] = "organizationalPerson"; $newuser["objectclass"][3] = "user"; $newuser['displayname'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser['givenname'] = $first_name.' '.$fathers_name; $newuser['name'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser['sn'] = $family_name; $newuser['description'] = $position; $newuser['mobile'] = $tel_num; $newuser['samAccountName'] = $account; $newuser['userPrincipalName'] = $account.'@xxx.local'; $newuser['title'] = $position; $newuser['department'] = $department; $newuser['manager'] = $manager; $newuser['useraccountcontrol'] = 0x0002; // Account disabled $newuser['pwdLastSet'] = 0; // User must change password $newuser['userPassword'] = '123321'; //echo '<h2>New AD user</h2>'.Arratomy($newuser); $result = ldap_add($connect, $dn, $newuser); if($result) {echo '<div class="success"> </div>';} else {$errors = ldap_error($connect);} } if('' != $errors) {echo '<h2 style="color:red;"></h2><div class="errors">'.$errors.'</div>';} }
      
      







もちろん、それほど多くのユーザー属性を入力する必要がない、またはその逆の場合があります。 しかし、そのようなセットはすでに直面しなければならないものをより明確に示しています。

重要な変数は次のとおりです。



  $dn = 'CN=  ,OU=!Users_all,DC=xxx,DC=local'; // Distinguished name $newuser['cn'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser["objectclass"][0] = "top"; $newuser["objectclass"][1] = "person"; $newuser["objectclass"][2] = "organizationalPerson"; $newuser["objectclass"][3] = "user"; $newuser['samAccountName'] = $account; $newuser['userPrincipalName'] = $account.'@xxx.local';
      
      







原則として、他のすべてがなくてもできます。



すべてをまとめる(+スタイルとユーティリティ関数):
 <style> .tree {height:500px; overflow-y:scroll; border:1px solid #ccc;} .tree li:hover {cursor:pointer;} .tree input {display:inline;} .errors {margin:10px 0px; border: 1px dotted red; padding:10px;} .success {margin:10px 0px; border: 1px dotted green; padding:10px;} </style> <?php //ini_set('display_errors','On'); error_reporting(E_ALL | E_STRICT); /* Service functions */ function Arratomy($array) { $result = "\n".'<span>Array (<b>'.sizeof($array).'</b> items)</span>'."\n".'<ol>'."\n"; foreach ($array as $key => $value) { $result .= '<li>'; if(is_array($value)) {$result .= '<b>'.$key.'</b>: '.Arratomy($array[$key]);} else {$result .= '<b>'.$key.'</b>: '.$value.'</br>'."\n";} $result .= '</li>'; } $result .= '</ol>'."\n"; return $result; } function translit($text) { $alphabet_ru = array ( '','','','','','','','', '','','','','','','','', '','','','','','','','', '','','','','','','','','', '','','','','','','','', '','','','','','','','', '','','','','','','','', '','','','','','','','','', ' ' ); $alphabet_eng = array ( 'e','y','ts','u','k','e','n','g', 'sh','sch','z','h','','f','y','v', 'a','p','r','o','l','d','j','e', 'ya','ch','s','m','i','t','','b', 'yu', 'E','Y','Ts','U','K','E','N','G', 'Sh','Sch','Z','H','','F','Y','V', 'A','P','R','O','L','D','J','E', 'Ya','Ch','S','M','I','T','','B', 'Yu', '_' ); return str_replace($alphabet_ru, $alphabet_eng, $text); } /* Connection parameters */ $ldap_host = "xxx.local"; # domain controller or ip-address $ldap_port = "389"; # port for connection // $filter = "ou=*"; # search any department // overridden in the function // $base_dn = "OU=!Users_all,DC=xxx,DC=local"; # specific OU // overridden in the function $ldap_user ="admin@xxx.local"; # AD-user $ldap_pass = "123321";# AD-user's password /* Open connection */ $connect = ldap_connect( $ldap_host, $ldap_port); ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3); ldap_set_option($connect, LDAP_OPT_REFERRALS, 0); $bind = ldap_bind($connect, $ldap_user, $ldap_pass); /* Input check */ //echo '<h2>POST</h2>'.Arratomy($_POST); $errors = ''; /* EXAMPLE OF CORRECT POST new_emp: 1 family_name:  first_name:  fathers_name:  position:  tel_num: +7(999) 999-9999 dep:      */ if(isset($_POST['new_emp'])) { foreach($_POST as $key => $value) { if('' == $value) {$errors .= ' '.$key.'  <br/>'; break;} else {$$key = $value;} } if(!isset($_POST['dep'])) {$errors .= ' «»  <br/>';} /* Creating employee */ if('' == $errors) { $trans_family_name = translit($family_name); $trans_first_name = translit($first_name); $trans_fathers_name = translit($fathers_name); $account = $trans_family_name.substr($trans_first_name, 0, 1).substr($trans_fathers_name, 0, 1); $dn = 'CN='.$family_name.' '.$first_name.' '.$fathers_name.','.$dep; //Getting manager $filter = '(&(objectClass=user)(objectCategory=PERSON))'; $sr = ldap_search($connect, $dep, $filter); $info = ldap_get_entries($connect, $sr); //echo Arratomy($info); $manager = $info[0]['manager'][0]; $department = explode(',', $dep); $department = str_replace('OU=', '', $department[0]); $newuser['cn'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser["objectclass"][0] = "top"; $newuser["objectclass"][1] = "person"; $newuser["objectclass"][2] = "organizationalPerson"; $newuser["objectclass"][3] = "user"; $newuser['displayname'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser['givenname'] = $first_name.' '.$fathers_name; $newuser['name'] = $family_name.' '.$first_name.' '.$fathers_name; $newuser['sn'] = $family_name; $newuser['description'] = $position; $newuser['mobile'] = $tel_num; $newuser['samAccountName'] = $account; $newuser['userPrincipalName'] = $account.'@xxx.local'; $newuser['title'] = $position; $newuser['department'] = $department; $newuser['manager'] = $manager; $newuser['useraccountcontrol'] = 0x0002; // Account disabled $newuser['pwdLastSet'] = 0; // User must change password $newuser['userPassword'] = '123321'; //echo '<h2>New AD user</h2>'.Arratomy($newuser); $result = ldap_add($connect, $dn, $newuser); if($result) {echo '<div class="success"> </div>';} else {$errors = ldap_error($connect);} } if('' != $errors) {echo '<h2 style="color:red;"></h2><div class="errors">'.$errors.'</div>';} } /* Hierarchical tree */ function build_tree($connect, $ou = '', $base_dn = 'OU=!Users_all,DC=xxx,DC=local') { $specific_dn = $base_dn; if ('' != $ou) {$specific_dn = 'OU='.$ou.','.$base_dn;} $filter = 'ou=*'; $justthese = array("ou"); $sr = ldap_list($connect, $specific_dn, $filter, $justthese); $info = ldap_get_entries($connect, $sr); $result = ''; for ($i=0; $i < $info["count"]; $i++) { $specific_ou = $info[$i]["ou"][0]; $result .= '<ul>'; $result .= '<li><input type="radio" name="dep" value="OU='.$specific_ou.','.$specific_dn.'" title="'.$specific_ou.'" /> '.$specific_ou; $result .= build_tree($connect, $specific_ou, $specific_dn); $result .= '</li>'; $result .= '</ul>'; } return $result; } /* Output */ echo ' <form method="POST" action="'.$_SERVER['REQUEST_URI'].'"> <h3></h3> <input type="hidden" name="new_emp" value="1" /> <input type="text" name="family_name" value="" size="20" /> <input type="text" name="first_name" value="" size="20" /> <input type="text" name="fathers_name" value="" size="20" /> <br/> <h3></h3> <input type="text" name="position" value="" size="40" /> <br/> <h3>  </h3> <input type="text" name="tel_num" value="+7(999) 999-9999" size="20" /> <h3></h3> <div class="tree"> '.build_tree($connect).' </div> <br/> <input type="submit" value="" /> </form>'; /* Close connection */ ldap_close($connect); ?>
      
      












All Articles