itseasy/ldaptools

LdapTools 是一个功能丰富的 PHP 5.6+ LDAP 库。

dev-master 2023-04-28 12:18 UTC

This package is auto-updated.

Last update: 2024-09-28 15:40:36 UTC


README

LdapTools 是一个功能丰富的 PHP 5.6+ LDAP 库。它被设计为可定制的,可以与几乎任何目录服务一起使用,但包含 Active Directory 和 OpenLDAP 的默认属性转换器和模式。

  • 用于生成 LDAP 查询的流畅且易于理解的语法。
  • 轻松创建/修改/删除/恢复常见的 LDAP 对象(用户、组、联系人、计算机、组织单位)。
  • 以简单数组或具有自动魔法设置器/获取器的对象检索 LDAP 对象。
  • 所有 LDAP 操作的日志机制
  • 用于进一步自定义、扩展和集成的事件系统
  • 解析和创建 LDIF 文件
  • 查看和修改 Active Directory 权限

安装

安装 LdapTools 的推荐方法是使用 Composer

composer require ldaptools/ldaptools

入门

开始使用最简单的方法是创建一个 YAML 配置文件。请参阅示例配置文件以了解基本用法。有关所有可用选项的列表,请参阅配置文件参考文档

定义配置文件后,您可以按照以下步骤开始使用:

use LdapTools\Configuration;
use LdapTools\LdapManager;

$config = (new Configuration())->load('/path/to/ldap/config.yml');
$ldap = new LdapManager($config);

搜索 LDAP

使用 LdapManager,您现在可以轻松构建 LDAP 查询,而无需记住所有特殊语法。所有值也都会自动转义。请参阅教程,了解所有可用方法,以及食谱,了解更多查询示例。

use LdapTools\Object\LdapObjectType;

// Get an instance of the query...
$query = $ldap->buildLdapQuery();

// Returns a LdapObjectCollection of all users whose first name 
// starts with 'Foo' and last name is 'Bar' or 'Smith'.
// The result set will also be ordered by state name (ascending).
$users = $query->fromUsers()
    ->where($query->filter()->startsWith('firstName', 'Foo'))
    ->orWhere(['lastName' => 'Bar'])
    ->orWhere(['lastName' => 'Smith'])
    ->orderBy('state')
    ->getLdapQuery()
    ->getResult();

echo "Found ".$users->count()." user(s).";
foreach ($users as $user) {
    echo "User: ".$user->getUsername();
}

// Get all OUs and Containers at the base of the domain, ordered by name.
$results = $ldap->buildLdapQuery()
    ->from(LdapObjectType::OU)
    ->from(LdapObjectType::CONTAINER)
    ->orderBy('name')
    ->setScopeOneLevel()
    ->getLdapQuery()
    ->getResult();

// Get a single LDAP object and select some specific attributes...
$user = $ldap->buildLdapQuery()
    ->select(['upn', 'guid', 'sid', 'passwordLastSet'])
    ->fromUsers()
    ->where(['username' => 'chad'])
    ->getLdapQuery()
    ->getSingleResult();

// Get a single attribute value from a LDAP object...
$guid = $ldap->buildLdapQuery()
    ->select('guid')
    ->fromUsers()
    ->where(['username' => 'chad'])
    ->getLdapQuery()
    ->getSingleScalarResult();
    
// It also supports the concepts of repositories...
$userRepository = $ldap->getRepository('user');

// Find all users whose last name equals Smith.
$users = $userRepository->findByLastName('Smith');

// Get the first user whose username equals 'jsmith'. Returns a `LdapObject`.
$user = $userRepository->findOneByUsername('jsmith');
echo "First name ".$user->getFirstName()." and last name ".$user->getLastName();

有关构建 LDAP 查询的更多信息,请参阅文档

修改 LDAP 对象

修改 LDAP 与上述搜索 LDAP 对象一样简单,然后在对象上直接进行更改,并使用 LdapManager 将其保存回 LDAP。

$user = $ldap->buildLdapQuery()
    ->select(['title', 'mobilePhone', 'disabled'])
    ->fromUsers()
    ->where(['username' => 'jsmith'])
    ->getLdapQuery()
    ->getSingleResult();

// Make some modifications to the user account.
// All these changes are tracked so it knows how to modify the object.
$user->setTitle('CEO');

if ($user->hasMobilePhone()) {
    $user->resetMobilePhone();
}

// Set a field by a property instead...
if ($user->disabled) {
    $user->disabled = false;
}

// Add a value to an attribute...
$user->addOtherIpPhones('#001-5555');
// Add a few values at one time...
$user->addOtherIpPhones('#001-4444', '#001-3333', '#001-2222');

// Now actually save the changes back to LDAP...
try {
    $ldap->persist($user);
} catch (\Exception $e) {
    echo "Error updating user! ".$e->getMessage();
}

有关修改 LDAP 对象的更多信息,请参阅文档

删除 LDAP 对象

删除 LDAP 对象很简单,只需搜索要删除的对象,然后将它传递给 LdapManager 上的 delete 方法即可。

// Decide they no longer work here and should be deleted?
$user = $userRepository->findOneByUsername('jsmith');

try {
    $ldap->delete($user);
} catch (\Exception $e) {
    echo "Error deleting user! ".$e->getMessage();
}

创建 LDAP 对象

通过仅传递您希望属性成为什么以及对象应该最终位于哪个容器/OU,可以轻松创建LDAP对象。

$ldapObject = $ldap->createLdapObject();

// Creating a user account (enabled by default)
$ldapObject->createUser()
    ->in('cn=Users,dc=example,dc=local')
    ->with(['username' => 'jsmith', 'password' => '12345'])
    ->execute();

// Create a typical AD global security group...
$ldapObject->createGroup()
    ->in('dc=example,dc=local')
    ->with(['name' => 'Generic Security Group'])
    ->execute();

// Creates a contact user...
$ldapObject->createContact()
    ->in('dc=example,dc=local')
    ->with(['name' => 'Some Guy', 'emailAddress' => 'SomeGuy@SomeDomain.com'])
    ->execute();

// Creates a computer object...
$ldapObject->createComputer()
    ->in('dc=example,dc=local')
    ->with(['name' => 'MYWOKRSTATION'])
    ->execute();
    
// Creates an OU object...
$ldapObject->createOU()
    ->in('dc=example,dc=local')
    ->with(['name' => 'Employees'])
    ->execute();

有关创建LDAP对象的更多信息,请参阅文档

文档

浏览文档文件夹以获取有关LdapTools的更多信息。

待办事项

尚未实现的功能

  • 基于LDAP中的信息自动生成模式。
  • OpenLDAP模式还需要更多工作。