lawiet / yii2-ldap
Yii2 的 LDAP 扩展
0.2
2017-09-06 19:40 UTC
Requires
- php: >=5.3.0
- ext-ldap: *
- tiesa/ldap: @dev
This package is auto-updated.
Last update: 2024-08-29 04:19:52 UTC
README
使用 LDAP 与 Yii2 一起使用的组件
安装
安装此扩展的首选方式是通过 composer。
运行
php composer.phar require lawiet/yii2-ldap "dev-master"
或者在您的 composer.json 文件的 require 部分添加
"minimum-stability": "dev",
"prefer-stable": true,
require: {
...
"lawiet/yii2-ldap": "@dev"
...
}
to
Ldap 配置
<?php ....
'params' => [
'ldap' => [
'hostname' => '127.0.0.1',
'port' => 389,
//'security' => 'SSL',
'bind_dn' => false,
'bind_password' => false,
'username' => 'admin',
'password' => 'admin',
'base_dn' => 'dc=example,dc=org',
'filter' => '(&(objectClass=*))',
'user_options' => [
'base_dn' => 'cn=user,dc=example,dc=org',
'filter' => '(&(objectClass=inetOrgPerson))',
],
'group_options' => [
'base_dn' => 'cn=group,dc=example,dc=org',
'filter' => '(&(objectClass=*))',
],
'options' => [
LDAP_OPT_NETWORK_TIMEOUT => 30,
LDAP_OPT_PROTOCOL_VERSION => 3,
LDAP_OPT_REFERRALS => 0,
],
]
]
...
?>
搜索 LDAP
最基础的搜索以及最复杂的搜索都通过一个唯一的 API 处理。这就是 ldap_read 或 ldap_list 或 ldap_search 的困境的终结
<?php
// ... $manager connection & binding
$results = $manager->search(Search::SCOPE_ALL, 'ou=comp,dc=example,dc=com', '(objectclass=*)');
// 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()));
}
}
SCOPE_ALL 将允许您搜索整个子树,包括您提供的具有区分名称的基本节点。其他选项包括
- SCOPE_BASE: 只搜索与给定的区分名称匹配的一个节点
- SCOPE_ONE: 将搜索与给定的区分名称匹配的节点下的节点
为了方便起见,该组件还提供了一个直接方法,在您知道其区分名称时检索一个节点
<?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
更多信息:tiesa/ldap