chanmix51 / slapom
LDAP的小对象模型管理器
Requires
- php: >=5.3.0
- ext-ldap: *
This package is auto-updated.
Last update: 2024-09-07 19:15:01 UTC
README
SlapOM 是一个简单的 LDAP 对象模型管理器。它允许您使用 LDAP 协议从数据库 生成 和 填充 对象。它已经与 Microsoft Active Directory (tm) 进行了测试。
SlapOM 与 PHP 5.3 兼容,需要 php5-ldap 扩展模块。它使用 LDAP v3。
项目结构
├── documentation │ ├── lib │ ├── SlapOM │ └── Exception │ └── tests ├── bootstrap ├── config ├── fixtures └── SlapOM ├── Tests └── Units
生成和填充示例
以下是一个场景:您想从 LDAP 服务器检索您的 "person" objectClass,并将其实例的字段在 User PHP 类中可用,以显示用户名、电子邮件和组信息。
步骤 1 - 创建继承对象
第一步是创建代表 "person" objectClass 及其映射器的对象(即实体或 User)。
实体类(必须扩展 \SlapOM\Entity)
public class User extends \SlapOM\Entity { }
映射器类(必须扩展 \SlapOM\EntityMap,并且必须命名为 "{Entity class name}Map")
抽象类 EntityMap 包含一个抽象方法 configure(); 您必须重写此方法来设置所需参数。
public class UserMap extends \SlapOM\EntityMap { protected function configure() { /* Set up the required options */ $this->base_dn = 'dc=company,dc=com' $this->ldap_object_class = 'person'; $this->entity_class = 'User'; /* Set up the fields that you want to retrieve in your User class */ // Standard String fields $this->addAttribute('firstname'); $this->addAttribute('lastname'); $this->addAttribute('mail'); // Array field $this->addAttribute('objectclass', self::FIELD_MULTIVALUED); // Binary field $this->addAttribute('image', self::FIELD_BINARY); } }
步骤 2 - 使用它!
初始化连接
$connection = new SlapOM\Connection('localhost', 'cn=root', 'root');
实例化映射器类
$userMap = $connection->getMapFor('User');
将所有用户实体查询到 $result 数组中
$result = $userMap->find();
显示结果
<ul> <?php foreach ($result as $user): ?> <li> <?php printf('%s, %s (%s) is objectClass:', $user->getFirstname(), $user->getLastname(), $user->getMail()) ?> <ul> <?php foreach ($user->getObjectclass() as $group): ?> <li><?php printf('<li>%s</li>', $group) ?></li> </ul> <?php endforeach ?> </li> <?php endforeach ?> </ul>
* Amar, Aaccf (user.0@maildomain.net) is objectClass: - person - organizationalperson - inetorgperson - top * Atp, Aaren (user.1@maildomain.net) is objectClass: - person - organizationalperson - inetorgperson - top * Atpco, Aarika (user.2@maildomain.net) is objectClass: - person - organizationalperson - inetorgperson - top
查询数据库
当然,在大多数情况下,您可能不感兴趣从数据库中检索所有实体,而只是其中的一部分。这可以通过将 find()
方法的第一个参数设置为规范化 LDAP 查询字符串来实现,如下所示
$result = $userMap->find('(|(mail=*@maildomain.net)(name=user*))');
注意,getObjectClassFilter()
方法的返回值将附加到您的查询字符串。最终的查询字符串将是 (&(objectClass=user)(|(mail=*@maildomain.net)(name=user*)))
。
要管理更复杂的查询,您可以使用 BinaryFilter
类
$filter = \SlapOM\BinaryFilter::create("mail=*@maildomain.net") ->addOr("name=user*"); $result = $userMap->find((string) $filter);
如果您有记录的 DN,请使用 fetch()
方法获取相应的对象
$user = $userMap->fetch($dn);
投影操作符
默认情况下,查询返回填充的对象集合。这些实例默认使用其相应的映射类中声明的字段进行填充。可以通过使用 getSearchFields()
方法覆盖此行为。尽管在用户映射类中将用户密码声明为二进制字段是一个好主意,但在每次检索用户时从数据库中检索它则不是一个好主意。此方法是删除(或添加)搜索中字段的正确位置。
处理实体
SlapOM 是一个 OMM,因此实体对 LDAP 数据库及其结构一无所知:它们只是灵活的数据容器
$user['mail']; // $user->getMail(); $user->mail; // $user->getMail(); $user->getMail(); // $user->get('mail'); $user->get('mail'); // $mail, raw data from LDAP
如果您重写 getMail()
访问器,对 $user['mail']
和 $user->mail
的调用将反映您的重写。您不能重写通用的 get('mail')
,因为这是通过从数据库提取的原始数据访问的唯一方式。
修改实体的数据遵循相同的原则。要保存实体,只需调用映射类的 save()
函数,并传递您修改后的对象
$user['mail'] = 'newMail@maildomain.net'; // $user->setMail('newMail@maildomain.net'); $user->isModified(); // true $userMap->save($user); $user->isModified(); // false $user->isPersisted(); // true
测试
SlapOM 库的整个单元测试是用 Atoum (http://downloads.atoum.org/) 进行的。您可以使用以下命令运行测试套件
php /{wherever the atoum.phar is}/mageekguy.atoum.phar -d tests/SlapOM/Tests/Units/
或按类运行
php tests/SlapOM/Tests/Units/{File name}
在运行单元测试之前,您需要将 LDIF 固件(test/fixtures/ldap_datas.ldif)加载到您的 LDAP 测试服务器中,并编辑测试配置文件 tests/config/config.ini。