freedsx / ldap
纯PHP LDAP库
0.8.0
2022-05-21 19:17 UTC
Requires
- php: >=7.1
- freedsx/asn1: ^0.4.0
- freedsx/sasl: ^0.1.0
- freedsx/socket: ^0.5.2
- psr/log: ^1|^2|^3
Requires (Dev)
- friends-of-phpspec/phpspec-code-coverage: ^4.3|^6.1|dev-master
- phpspec/phpspec: ^7.2|^7.1|^6.1|^5.1
- phpstan/phpstan: ^0.12.70
- phpunit/phpunit: ^9.3|^8.0|^7.0
- slevomat/coding-standard: ~7.0
- squizlabs/php_codesniffer: 3.*
- symfony/process: ^3.0|^4.0|^5.0
- symplify/easy-coding-standard: ^6.1|^7.3|^9.0
Suggests
- ext-openssl: For SSL/TLS support and some SASL mechanisms.
- ext-pcntl: For LDAP server functionality.
This package is auto-updated.
Last update: 2024-09-16 16:22:27 UTC
README
FreeDSx LDAP是一个纯PHP LDAP库。它不依赖于PHP LDAP核心扩展。该库目前实现了RFC 4511中描述的大多数客户端功能以及一些非常有限的LDAP服务器功能。它还实现了来自不同RFC的一些其他客户端功能。
- 分页控制支持 (RFC 2696)
- VLV控制支持 (草案-ietf-ldapext-ldapv3-vlv-09)
- 服务器端排序控制 (RFC 2891)
- 密码修改请求 (RFC 3062)
- 搜索过滤器字符串表示 (RFC 4515)
- 支持某些机制的SASL身份验证/完整性层 (RFC 4513)
如果可用,它通过OpenSSL扩展支持通过TLS加密LDAP连接。
文档
入门
通过 composer 安装
composer require freedsx/ldap
使用LdapClient类和辅助类
use FreeDSx\Ldap\LdapClient; use FreeDSx\Ldap\Operations; use FreeDSx\Ldap\Search\Filters; $ldap = new LdapClient([ # Servers are tried in order until one connects 'servers' => ['dc1', 'dc2'], # The base_dn is used as the default for searches 'base_dn' => 'dc=example,dc=local' ]); # Encrypt the connection prior to binding $ldap->startTls(); # Bind to LDAP with a specific user. $ldap->bind('user@example.local', '12345'); # Build up a LDAP filter using the helper methods $filter = Filters::and( Filters::equal('objectClass', 'user'), Filters::startsWith('cn', 'S'), # Add a filter object based off a raw string filter... Filters::raw('(telephoneNumber=*)') ); # Create a search operation to be used based on the above filter $search = Operations::search($filter, 'cn'); # Create a paged search, 100 results at a time $paging = $ldap->paging($search, 100); while ($paging->hasEntries()) { $entries = $paging->getEntries(); var_dump(count($entries)); foreach ($entries as $entry) { echo "Entry: ".$entry->getDn().PHP_EOL; } }
CRUD操作
创建
use FreeDSx\Ldap\Entry\Entry; use FreeDSx\Ldap\Exception\OperationException; # Create a new LDAP entry object $entry = (new Entry('cn=foo,dc=domain,dc=local')) ->set('objectClass','top', 'group') ->set('sAMAccountName', 'foo'); # Create the entry with the LDAP client try { $ldap->create($entry); } catch (OperationException $e) { echo sprintf('Error adding entry (%s): %s', $e->getCode(), $e->getMessage()).PHP_EOL; }
读取
# Use the read() method of the LDAP client to search for a specific entry. # Optionally pass an array of attributes to select as the second argument. $entry = $ldap->read('cn=foo,dc=domain,dc=local'); # Entry will be null if it doesn't exist if ($entry) { echo $entry.PHP_EOL; var_dump($entry->toArray()); }
更新
use FreeDSx\Ldap\Exception\OperationException; # Search for an entry object to get its current attributes / values $entry = $ldap->read('cn=foo,dc=domain,dc=local'); # Add a value to an attribute if (!$entry->get('telephoneNumber')) { $entry->add('telephoneNumber', '555-5555'); } # Remove any values an attribute may have if ($entry->has('title')) { $entry->reset('title'); } # Delete a specific value for an attribute if ($entry->get('ipPhone')->has('12345')) { $entry->delete('ipPhone', '12345'); } # Set a value for an attribute. This replaces any value it may, or may not, have. $entry->set('description', 'Employee'); # Send the built up changes back to LDAP to update the entry via the LDAP client update method. try { $ldap->update($entry); } catch (OperationException $e) { echo sprintf('Error modifying entry (%s): %s', $e->getCode(), $e->getMessage()).PHP_EOL;; }
删除
use FreeDSx\Ldap\Exception\OperationException; # Search for the entry object to delete $entry = $ldap->read('cn=foo,dc=domain,dc=local'); # Pass the entry object to the delete method of the LDAP client if it was found. # You could also pass a DN object or a simple DN as a string. if ($entry) { try { $ldap->delete($entry); } catch (OperationException $e) { echo sprintf('Error deleting entry (%s): %s', $e->getCode(), $e->getMessage()).PHP_EOL;; } }