newtondasilva/ldaptools

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

1 2017-03-16 18:20 UTC

README

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

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

安装

推荐使用 Composer 安装 LdapTools。

composer require ldaptools/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 查询,而无需记住所有特殊的 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 对象

创建 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模式需要更多工作。