我有一个活动目录结构,其中用户对象驻留在OU中,例如IT,技术,人力资源,帐户等。我想编写一个PHP脚本,用于对AD进行身份验证,并根据他们的组来提供aproperiate Web服务。

ldap_search()需要基本DN。

我试图搜索

ldap_search($ldap, "dc=country,dc=company,dc=co,dc=uk", "(samaccountname=$username)", array("memberof"));

但PHP给出了“操作错误”。如果我指定OU

ldap_search($ldap, "ou=sales,dc=country,dc=company,dc=co,dc=uk", "(samaccountname=jake)", array("memberof"));

那么搜索就行了。

我可以使用通配符吗?

在旁注中,用户对象是否在OU中?因为我是第一个把他们搬到里面的noob!

解决方案是在连接和绑定之间添加2行。

ldap_connect(..)

ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);

ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

ldap_bind(..)

谢谢=)

编辑2 – 全码:

namespace ldap;

abstract class AuthStatus

{

const FAIL = "Authentication failed";

const OK = "Authentication OK";

const SERVER_FAIL = "Unable to connect to LDAP server";

const ANONYMOUS = "Anonymous log on";

}

// The LDAP server

class LDAP

{

private $server = "127.0.0.1";

private $domain = "localhost";

private $admin = "admin";

private $password = "";

public function __construct($server, $domain, $admin = "", $password = "")

{

$this->server = $server;

$this->domain = $domain;

$this->admin = $admin;

$this->password = $password;

}

// Authenticate the against server the domain\username and password combination.

public function authenticate($user)

{

$user->auth_status = AuthStatus::FAIL;

$ldap = ldap_connect($this->server) or $user->auth_status = AuthStatus::SERVER_FAIL;

ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

$ldapbind = ldap_bind($ldap, $user->username."@".$this->domain, $user->password);

if($ldapbind)

{

if(empty($user->password))

{

$user->auth_status = AuthStatus::ANONYMOUS;

}

else

{

$result = $user->auth_status = AuthStatus::OK;

$this->_get_user_info($ldap, $user);

}

}

else

{

$result = $user->auth_status = AuthStatus::FAIL;

}

ldap_close($ldap);

}

// Get an array of users or return false on error

public function get_users()

{

if(!($ldap = ldap_connect($this->server))) return false;

ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);

ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

$ldapbind = ldap_bind($ldap, $this->admin."@".$this->domain, $this->password);

$dc = explode(".", $this->domain);

$base_dn = "";

foreach($dc as $_dc) $base_dn .= "dc=".$_dc.",";

$base_dn = substr($base_dn, 0, -1);

$sr=ldap_search($ldap, $base_dn, "(&(objectClass=user)(objectCategory=person)(|(mail=*)(telephonenumber=*))(!(userAccountControl:1.2.840.113556.1.4.803:=2)))", array("cn", "dn", "memberof", "mail", "telephonenumber", "othertelephone", "mobile", "ipphone", "department", "title"));

$info = ldap_get_entries($ldap, $sr);

for($i = 0; $i < $info["count"]; $i++)

{

$users[$i]["name"] = $info[$i]["cn"][0];

$users[$i]["mail"] = $info[$i]["mail"][0];

$users[$i]["mobile"] = $info[$i]["mobile"][0];

$users[$i]["skype"] = $info[$i]["ipphone"][0];

$users[$i]["telephone"] = $info[$i]["telephonenumber"][0];

$users[$i]["department"] = $info[$i]["department"][0];

$users[$i]["title"] = $info[$i]["title"][0];

for($t = 0; $t < $info[$i]["othertelephone"]["count"]; $t++)

$users[$i]["othertelephone"][$t] = $info[$i]["othertelephone"][$t];

// set to empty array

if(!is_array($users[$i]["othertelephone"])) $users[$i]["othertelephone"] = Array();

}

return $users;

}

private function _get_user_info($ldap, $user)

{

$dc = explode(".", $this->domain);

$base_dn = "";

foreach($dc as $_dc) $base_dn .= "dc=".$_dc.",";

$base_dn = substr($base_dn, 0, -1);

$sr=ldap_search($ldap, $base_dn, "(&(objectClass=user)(objectCategory=person)(samaccountname=".$user->username."))", array("cn", "dn", "memberof", "mail", "telephonenumber", "othertelephone", "mobile", "ipphone", "department", "title"));

$info = ldap_get_entries($ldap, $sr);

$user->groups = Array();

for($i = 0; $i < $info[0]["memberof"]["count"]; $i++)

array_push($user->groups, $info[0]["memberof"][$i]);

$user->name = $info[0]["cn"][0];

$user->dn = $info[0]["dn"];

$user->mail = $info[0]["mail"][0];

$user->telephone = $info[0]["telephonenumber"][0];

$user->mobile = $info[0]["mobile"][0];

$user->skype = $info[0]["ipphone"][0];

$user->department = $info[0]["department"][0];

$user->title = $info[0]["title"][0];

for($t = 0; $t < $info[$i]["othertelephone"]["count"]; $t++)

$user->other_telephone[$t] = $info[$i]["othertelephone"][$t];

if(!is_array($user->other_telephone[$t])) $user->other_telephone[$t] = Array();

}

}

class User

{

var $auth_status = AuthStatus::FAIL;

var $username = "Anonymous";

var $password = "";

var $groups = Array();

var $dn = "";

var $name = "";

var $mail = "";

var $telephone = "";

var $other_telephone = Array();

var $mobile = "";

var $skype = "";

var $department = "";

var $title = "";

public function __construct($username, $password)

{

$this->auth_status = AuthStatus::FAIL;

$this->username = $username;

$this->password = $password;

}

public function get_auth_status()

{

return $this->auth_status;

}

}

?>

用法:

$ldap = new ldap\LDAP("192.168.1.123", "company.com", "admin", "mypassword");

$users = $ldap->get_users();

php ldap_search,如果我不知道基本DN的OU,那么如何使用PHP ldap_search()获取用户OU相关推荐

  1. java 操作 ldap_JAVA操作LDAP总结

    一.LDAP概念 LDAP的全称为Lightweight Directory Access Protocol(轻量级目录访问协议), 基于X.500标准, 支持 TCP/IP. LDAP目录为数据库, ...

  2. smaba(跨平台文件共享)

    文章目录 samba (跨平台文件共享) 介绍 服务端配置 文件各功能解释即需要修改的地方: 创建smb用户并使用 共享文件夹(目录)设置 配置完毕没有权限排查思路 仅允许部分用户拥有写权限 配置文件 ...

  3. MeterSphere之系统设置

    1.介绍 一站式开源持续测试平台,涵盖测试跟踪.接口测试.性能测试.团队协作等功能,全面兼容Jmeter\Postman等开源主流标准,文档已经介绍的很详细,就不一一说了,直接上手干 相关文档: ht ...

  4. ldap基本dn_LDAP 中 DN CN DC OU

    DN 的英文名称是(distinguished name),直接翻译过来就是专有名称. 简单的就可以理解为一个路径就对了. 这个路径可以指向 OU ,也可以指到 CN. 其中 DN 有三个属性,分别是 ...

  5. 留个坑,不知道为什么sqlite3要求组权限才能执行db:migrate,而可以直接执行db:......

    为什么80%的码农都做不了架构师?>>>    如题,背景是今天下的几个项目,放在windows的硬盘上,再用smb访问,因为新装了debian6,不能直接smbmount,只能mo ...

  6. java后端面试不知道多少家重庆的公司得来的题目总结

    面试题目总结 前言 JWT使用 token是如何生成 微服务链路检测 泛型的实现 cglib的实现 有哪些开发规范 你对雪崩效应的看法 看过哪些源代码 熔断器的使用 高并发场景 mybatis一级缓存 ...

  7. LDAPserver的安装

    源代码安装,以root用户进行 由于:由于openldap须要用Berkeley DB来存放数据,所以要先安装所以需先安装Berkeley DB 4.2.52数据库. 一 安装Berkeley DB ...

  8. document.getElementById 学习总结

    document.getElementById获取控件对象为空的解决方法 1.下面是一个简单的例子,页面加载时显示一段信息 代码如下: <%@ page language="java& ...

  9. ldap配置系列二:jenkins集成ldap

    ldap配置系列二:jenkins集成ldap jenkins简介 jenkins是一个独立的.开放源码的自动化服务器,它可以用于自动化与构建.测试.交付或部署软件相关的各种任务. jenkins官方 ...

最新文章

  1. 智源论坛 | 智能处理器探索(3月21日)
  2. 【一个实体对象不能由多个 IEntityChangeTracker 实例引用】原因及解决方法
  3. Webpack介绍和使用(配置环境变量,打包依赖)
  4. python 时间序列预测 币价_python时间序列预测股票走势
  5. LiveVideoStackCon 北京站,好久不见
  6. *args 和**kwargs 的理解以及 函数的参数的总结
  7. C#中数据流(文件流、内存流、网络流等)相关知识点梳理
  8. .net 读蓝牙数据_Linux内核曝严重蓝牙漏洞,影响多个版本
  9. java8 迭代set集合_JavaSE(八)集合之Set
  10. 上传文件(ajax结合form表单)
  11. 信号与系统matlab实践实验二,信号与系统matlab实验课后习题答案
  12. 【程序员读论文】LeCun, Y., Bengio, Y. Hinton, G. Deep learning. *Nature* **521,** 436–444 (2015).
  13. 超详细!联想小新700黑苹果双系统完整教程(1)
  14. 在线caj免费转换Word格式
  15. Frame-relay帧中继配置,实现网络连接
  16. Android直连MySQL数据库
  17. centos8安装nginx
  18. OAuth2授权方式
  19. mac tortoisesvn客户端_tortoisesvn mac版下载
  20. LaTeX公式符号总结(Markdown适用)

热门文章

  1. Html5游戏封包,学习JavaScript-10-基本封包类型
  2. 三维建模思路培养——复古风书房建模
  3. C语言——跳跃游戏 II
  4. 【RF】【元素定位】 Other element would receive the click
  5. Android系统功耗优化之CPU - CPU功耗和频率的关系
  6. 真正的IT技术男是什么样的?
  7. paper survey(2019.06.05)——卷积网络feature map的传递与利用
  8. 七彩虹平板刷成android,大功告成 七彩虹G808首个MIUI ROM移植
  9. 七彩虹计算机主板维修,七彩虹C .H61U v28主板维修一例
  10. 安装与使用 supervisor(可管理Tomcat进程)