PHP操作AD,adLDAP类API详解与实例




本文简述通过PHP操作AD



工具 ADLDAP.php
下载位置http://adldap.sourceforge.net/download.php
API(以下来自http://adldap.sourceforge.net,翻译水平有限,如有不妥之处敬请指正)
constructor($options=array())//构造器

你可以通过配置变量的方式指定该类中AD的设置, 或者当类被调用的时候可以通过指定$option数组的方式被覆盖.

调用方式形似 $object = new adLDAP($options); $options 是一个由下列一个或多个keys组成的数组

account_suffix
 默认:”@mydomain.local”完整的域帐户后缀
base_dn
 默认: “DC=mydomain,DC=local”

域的base dn. 一般来讲 base dn 与account suffix相同, 但是被隔开的且以"DC="为前缀. base dn 可被定位于Active Directory Users及Computers MMC的扩

展属性上
如果认证用户正常,但不能搜索,一般来说是由于指定了不正确的base_dn

 

domain_controllers
 默认: array (“dc01.mydomain.local”)

域控制器数组,如果你希望该类通过多个控制器来平衡查询,可以在该数组中指定多个控制器,记住该类会向一个不可连接的域控制器发送请求,因为它只实施平衡

而无容错
.

ad_username
 默认: NULL默认地,adLDAP会以已经认证的用户帐号权限执行查询,你可以指定一个拥有更高权限的用来执行授权操作的用户帐号
 
ad_password
 默认: NULLad_username相应的密码.
 real_primarygroup
 通过“Domain Users” 覆盖 primary group use_ssl
 默认: false


adLDAP 可以通过SSL使用LDAP以提供额外功能例如修改密码,选择此项时你的域控制器和WEB服务器均需配置相应选项,不只将其设置为true,详细请参考SSL方式的

LDAP选项

recursive_groups
 默认: true 
 递归查询组成员
 如用户Fred是组“Business Unit” 的成员,“Business Unit” 是组Department”的成员,Department”是组“Company”的成员
 user_ingroup(“Fred”,”Company”)当该项开启的时候返回true,否则返回false------------------------以下主要的操作方法
 authenticate($username,$password,$prevent_rebind=false)
 鉴别域控制器用户的username/password 
group_add_group($parent,$child)
 向父组里添加子组,返回true或false 
group_add_user($group,$username)
 向一个组里添加一个用户,返回true或false 
group_create($attributes)
 以指定属性创建一个组,返回true或false Attribute Req Notes 
 group_name *   
 container *   
 description    group_del_group($parent,$child)
 从父组里删除子组,返回true或false group_del_user($group,$users)
 从一个组里删除一个用户,返回true或false group_info($group_name,$fields=NULL)
 返回一个关于指定组的信息数组,组名称区分大小写
 默认文件包含member, memberof, description, distinguishedname, objectcategory, samaccountname user_create($attributes)
 创建一个用户,操作成功或失败时返回true或false Attribute Req Notes 
 username *   
 firstname *   
 surname *   
 email *   
 container * The folder in AD to add the user to. 
 address_city    
 address_code    
 address_pobox    
 address_state    
 address_street    
 change_password   如果为0用户下次登录时无需修改密码为1时下次登录时必须修改密码 
 company   公司名称. 
 department    
 description    
 display_name    
 email   email地址,非exchange mailbox 
 enabled   0 为 disabled 1 为 enabled    
 expires   帐户有效期 (unix timestamp). 
 firstname    
 home_directory    
 home_drive    
 initials    
 logon_name   登录名称不同于其他用户名. 
 manager    
 office    
 password   The password can only be set over SSL. It must also meet the password policy for your domain. 
 profile_path    
 script_path    
 surname    
 title    
 telephone    
 web_page    user_delete($username)
 删除一个用户,返回 true 或 false user_groups($username,$recursive=NULL)
 返回用户所属组的信息 如果$recursive为 true, 将递归返回组列表.
 user_info($username,$fields=NULL)
 返回指定用户的信息数组,$fields必须为数组默认fields 为: samaccountname, mail, memberof, department, displayname, telephonenumber, primarygroupid 
 欲查看所有可用信息,将$fields 设置"*"调用此函数
 这个函数将返回一个有限集,除非当前认证帐户为administrator,一个用户也不能查询另一个用户的"memberof"域,除非它们是这个容器的管理者 user_ingroup($username,$group,$recursive=NULL)
 用户是否属于该组,返回true或false

像user_info()函数一样,这个函数只有当当前认证用户是administrator时才会返回有效结果

 

user_modify($username,$attributes)
 修改用户属性,返回true或false user_password($username,$password)
 设置指定用户的密码,. 要求配置通过ldaps. computer_info($computer_name,$fields=NULL)
 返回指定计算机的详细信息. all_users($include_desc = false, $search = "*", $sorted = true)
返回AD里用户所有列表,在大目录里可能无法工作
all_groups($include_desc = false,$search = "*", $sorted = true)
 返回AD里组所有列表,在大目录里可能无法工作Samples:
登录
 <?php
 include "adLDAP.php"
 $config['account_suffix'] = '@xxx.com';//域控制器后缀
 $config['adserver'] = array('192.168.1.10','192.168.1.1');//域控制器,如果只有一台array('192.168.1.10')
 $config['base_dn'] = 'cn=users,dc=xxx,dc=com';
 $adldap =new adLDAP(array('domain_controllers'=>$config['adserver'],'account_suffix'=>$config['account_suffix'],'base_dn'=>$config['base_dn'],'ad_username' => 'administrator','ad_password' => ''));
 if($adldap)
 {
     echo "登录成功";
 }
 else
 {
     echo "登录失败";
 }
 ?>列出所有用户
 <?php
 echo "<b>All users</b><br>";
 foreach($adldap->all_users() as $val)
 {
 echo $val."<br>";
 }?>
 列出所有组
 <?php
 echo "<b>groups</b><br>";
 foreach($adldap->all_groups() as $val)
 {
 echo $val."<br>";
 }
 ?>
 打印某台计算机信息
 <?php
 print_r($adldap->user_info("wang"));
 ?>
 创建用户<?php
if ($adldap->user_create(array('username' => 'tonix','firstname' => 'firstname','surname' => "surname",'email' => 'e@123.com','container' =>
'container')))
 {
 echo "OK";
 }
 else
 {
 echo "error";
 }
 ?>创建组
 <?phpif ($adldap->group_create("group_name=test,container=www"))
 {
 echo "OK";
 }
 else
 {
 echo "error";
 }
 ?>


转载于:https://blog.51cto.com/3619523/671933