81square/tiesa-ldap

简化LDAP操作 - 支持LDAP分页结果(tiesa/ldap的分支)

v1.0.2 2016-01-24 21:24 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:31:10 UTC


README

这是对原始tiesa/ldap包的分支。感谢原始作者Cyril Cottet创建了这个优秀的包!

此分支修复了一些小问题,并添加了对LDAP分页结果的支持。请注意,由于分页支持,此包需要比原始包更高的PHP版本(>= 5.4.22)。此包使用与原始包相同的命名空间,可以作为原始包的简单替代品。

安装

在您的项目中安装此组件与安装Composer一样简单,如果尚未安装。

然后,在您的项目根目录下,创建一个名为composer.json的新文件,并将其粘贴到其中

{
    "require": {
        "81square/tiesa-ldap": "dev-master"
    }
}

要选择组件名称和版本,请参阅Packagist上的信息

然后,您就可以下载所有供应商库并生成自动加载配置文件了

$ php composer.phar install

从那里,加载Ldap组件类以及任何在您的composer.json配置中支持的包,就像在您的脚本顶部添加以下代码一样简单

require_once '/path/to/project/vendor/autoload.php';

用法

连接 & 绑定

连接到LDAP的典型顺序不仅包括与服务器建立连接,还包括绑定一个用户。操作如下

<?php

use Toyota\Component\Ldap\Core\Manager;
use Toyota\Component\Ldap\Platform\Native\Driver;

$params = array(
    'hostname'      => 'ldap.example.com',
    'base_dn'       => 'dc=example,dc=com'
);
$manager = new Manager($params, new Driver());

$manager->connect();

// Anonymous binding
$manager->bind();

// Ready for searching & persisting information

大多数情况下,您必须使用具有特权的用户来绑定到LDAP以执行持久性操作

$manager->bind('cn=user,dc=example,dc=com', 'myTopSecretPassword');

如果您正在连接到Active Directory域,您也可以像这样绑定

$manager->bind('user@example.com', 'myTopSecretPassword');

连接参数

我们在连接 & 绑定介绍中看到了一组最小的参数。然而,还有很多配置选项可用

  • hostname: LDAP服务器的FQDN
  • port: 在LDAP服务器上连接的端口号(默认为389用于常规连接和636用于SSL连接)
  • security: SSL或TLS之一。当未设置安全选项时,连接将是纯文本的
  • bind_dn: 默认的区分名称用于绑定(在这种情况下,$manager->bind()将不再是匿名的)
  • bind_password: 默认密码用于绑定
  • options: 默认为连接启用LDAP选项(请参阅Toyota\Component\Ldap\API\ConnectionInterface)

错误处理

所有Ldap错误代码和消息都通过方便的异常处理,通常这些代码和消息很难追踪(并且很容易被忘记)。

注意:与php ldap_connect函数一样,Manager的connect方法实际上不会连接。实际的连接是在bind()期间完成的。如果您的DC不可达或您提供了错误的连接配置,您将在bind()而不是connect()时收到异常。

因此,例如,您可以编写以下内容

<?php
// ... namespace imports and $params configuration
$manager = new Manager($params, new Driver());

try {
    $manager->connect();
} catch (ConnectionException $e) {
    // Do something about it
}

try {
    $manager->bind();
} catch (BindingException $e) {
    // Do something about it
}

// ...

所有可用的异常都位于Toyota\Component\Ldap\Exception命名空间中

搜索LDAP

最基本和最复杂的搜索都通过一个唯一的API处理。这是ldap_read或ldap_list或ldap_search困境的终结

<?php
// ... $manager connection & binding

// Whether to search through all subtree depth (Default = true)
$inDepth = true;

// Search
$results = $manager->search('ou=comp,dc=example,dc=com', '(objectclass=*)', $inDepth);

// A search result instance is retrieved which provides iteration capability for a convenient use
foreach ($results as $node) {
    echo $node->getDn();
    foreach ($node->getAttributes() as $attribute) {
        echo sprintf('%s => %s', $attribute->getName(), implode(',', $attribute->getValues()));
    }
}

为了方便起见,该组件还提供了一种直接方法来检索您知道其区分名称的单个节点

<?php
$node = $manager->getNode('cn=my,ou=node,dc=example,dc=com');

将信息持久化到LDAP

忘记所有的 ldap_mod_add、ldap_mod_del、ldap_mod_replace、ldap_add 和 ldap_delete。你现在只需要记住 save() 和 delete()。该组件将跟踪你在 LDAP 条目上所做的所有更改,并会自动发出正确的函数调用以执行这些更改

<?php

$node = $manager->getNode('cn=node,ou=to,ou=update,dc=example,dc=com');
$node->get('username')->set('test_user');
$node->get('objectClass')->add('inetOrgPerson');
$node->get('sn')->set('Doe');
$node->removeAttribute('whatever');

$manager->save($node);

// Update done

$node = new Node()
$node->setDn('ou=create',dc=example,dc=com');
$node->get('objectClass', true)->add(array('top', 'organizationalUnit'));
// The true param creates the attribute on the fly
$node->get('ou', true)->set('create');

$manager->save($node);

// New Ldap entry saved

$manager->delete($node);

// Now it's gone

资源

要运行测试套件,从 GitHub 将项目克隆到你的工作环境中。从分发版本开始打包你自己的 autoload.php 和 phpunit.xml,你就可以开始使用了

$ php composer.phar install --dev
$ vendor/bin/phpunit

关于

要求

  • PHP >= 5.4.22,且必须安装 ldap 扩展
  • Composer

许可证

TIESA Ldap 组件采用 MIT 许可证 - 请参阅 LICENSE 文件以获取详细信息