comodojo/ldaph

穷人版的php ldap类

1.0.3 2018-12-06 11:06 UTC

This package is auto-updated.

Last update: 2024-09-06 23:51:50 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Code Coverage

穷人版的php ldap类

Ldaph是一个简单的库,用于处理LDAP/ActiveDirectory认证和搜索。

它支持

  • ssl (ldaps)
  • tls
  • 单点登录 (Active Directory)

安装

安装 composer,然后

composer require comodojo/ldaph 1.0.*

基本用法

  • 创建实例

    类构造函数期望传入ldap服务器和端口作为参数。由于可能生成 LdaphException(参数错误或缺少php扩展),请将其包裹在try/catch块中。

    try {
    	
    	$ldap = new \Comodojo\Ldaph('ldap.exampe.com', 389);
    
    }
    catch (LdaphException $le){
    
    	// handle exception here
    
    }
  • 用户认证

    $dn = "uid=john,dc=example,dc=com";
    
    try {
    
    	$ldap = new \Comodojo\Ldaph('ldap.exampe.com', 389);
    	$lauth = $ldap->dn($dn)->auth('john', 'doe');
    
    }
    catch (LdaphException $le){
    
    	// handle exception here
    
    }

    定义DN,有一个特殊单词USERNAME,它将被替换为第一个auth()参数($username)。

    DN示例

  • 搜索LDAP树

    搜索LDAP树至少需要

    • 基本DN(base)
    • 搜索DN(searchbase)
    • 绑定DN(dn)
    • 账户(用户/密码)

    search() 方法将使用这些参数列出ldap树。

    $dn = "uid=USERNAME,dc=example,dc=com";
    $base = "dc=example,dc=com";
    $searchbase = "(uid=PATTERN)";
    
    try {
    
    	$ldap = new \Comodojo\Ldaph('ldap.exampe.com', 389);
    
    	$lsearch = $ldap->base($base)
    					->searchbase($searchbase)
    					->dn($dn)
    					->account('john', 'doe')
    					->search("*",true);
    
    }
    catch (LdaphException $le){
    
    	// handle exception here
    
    }

    搜索base中的特殊单词 'PATTERN' 将被替换为第一个 search() 参数。

    第二个参数(如果为true)将以更方便的基于数组的格式返回结果。

    搜索base示例(如果您正在寻找用户名)

    • "(&(!(objectClass=computer))(|(anr=PATTERN)))"(Active Directory用)
    • "(uid=PATTERN)"(openLDAP用)

文档

贡献

欢迎贡献,并将得到充分认可。请参阅CONTRIBUTING以获取详细信息。

许可证

comodojo/ldaph 根据 MIT 许可证(MIT)发布。请参阅许可证文件以获取更多信息。