l3 / ldap-orm-bundle
针对 Symfony2/3/4 的 Ldap ORM
1.0.0
2019-02-25 07:44 UTC
Requires
- php: >=5.4
- ext-ldap: *
- symfony/class-loader: ~2.1
- symfony/dependency-injection: ~2.1
- symfony/framework-bundle: ~2.1
- symfony/http-kernel: ~2.1
- symfony/monolog-bundle: ~2.1
- symfony/twig-bundle: ~2.1
- symfony/yaml: ~2.1
This package is auto-updated.
Last update: 2024-09-11 13:53:33 UTC
README
L3LdapOrmBundle 是 GorgLdapOrmBundle 的分支,用于不依赖 "r1pp3rj4ck/TwigstringBundle",并且与 Symfony 版本 2、3 和 4 一起使用,同时添加了复杂查询和条件 LDAP 系统。它是一个用于检索、修改和持久化 LDAP 实体的接口,使用 PHP 的原生 LDAP 函数。
安装
1.1. 纯 L3LdapOrmBundle
使用以下命令安装 Bundle
composer require l3/ldap-orm-bundle:~1.0
1.2. 声明使用 L3LdapOrmBundle
<?php
// app/AppKernel.php
// ...
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new L3\Bundle\LdapOrmBundle\L3LdapOrmBundle(),
);
// ...
}
// ...
}
1.3. 配置 LDAP 参数
对于 Symfony2 或 Symfony3,在您的配置文件(parameters.yml 和 parameters.yml.dist)中添加 l3_ldap_orm 参数
l3_ldap_orm: connection: uri: ldap://ldap.exemple.com use_tls: false bind_dn: cn=admin,dc=exemple,dc=com password: exemplePassword ldap: base_dn: dc=exemple,dc=com password_type: sha1
Sha1 是默认的哈希方法。在某些情况下(如 OpenLdap),密码在服务器端进行哈希处理,如果是这种情况,则将 password_type
更改为 plaintext
。
基本用法
要使用 L3LdapOrmBundle,您需要向实体添加注解,如下例所示
namespace AppBundle\Entity; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\Attribute; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\ObjectClass; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\Dn; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\Sequence; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\ArrayField; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\DnPregMatch; /** * Class for represent Account * * @ObjectClass("udlAccount") * @Dn("uid={{ entity.uid }},{% for entite in entity.entities %}ou={{ entite }},{% endfor %}{{ baseDN }}") */ class Account { /** * @Attribute("uid") */ private $uid; /** * @Attribute("givenName") */ private $firstname; /** * @Attribute("sn") */ private $lastname; /** * @Attribute("udlAliasLogin") * @ArrayField() */ private $alias; /** * @DnPregMatch("/ou=([a-zA-Z0-9\.]+)/") */ private $entities = array("accounts"); public function getFirstname() { return $this->firstname; } public function setFirstname($firstname) { $this->firstname=$firstname; } public function getLastname() { return $this->lastname; } public function setLastname($lastname) { $this->lastname = $lastname; } public function getUid() { return $this->uid; } public function setUid($uid) { $this->uid=$uid; } public function getPassword() { return $this->password; } /** * For password use sha1 php function in base16 encoding */ public function setPassword($password) { $this->password = $password; } public function getAlias() { return $this->alias; } public function setAlias($alias) { $this->alias = $alias; } public function setEntities($entities) { $this->entities = $entities; } public function getEntities() { return $this->entities; } }
- 属性:使用此注解将类变量映射到 ldap 对象字段
- ObjectClass:使用此注解将 ldapObjectClass 属性分配给 PHP 实体类
- Dn:使用此注解使用 twig 语法构建 dn
- 序列:使用此注解定义与 ldap 序列对象的链接
- ArrayField:此注解定义一个属性为多值数组
- DnPregMatch:此注解使用正则表达式在 DN 上计算属性值
之后,您可以使用实体,如下例所示
$a = new Account(); $a->setUid('john.doo'); $a->setFirstname('John'); $a->setLastname('Doo'); $a->setAlias(array('jdoo','j.doo')); $em = $this->get('l3_ldap_orm.entity_manager'); $em->persist($a); $em->flush(); $repo = $em->getRepository('AppBundle\Entity\Account'); $a = $repo->findOneByUid('john.doo');
您还可以设置复杂的 ldap 请求
$conditions = Array(); $not = true; $conditions[] = new Condition('sn', 'Hetru'); $conditions[] = new Condition('sn', 'Bomy', $not); $query = new Query(Query::CAND); $query->cAnd($conditions); $em = $this->get('l3_ldap_orm.entity_manager'); $repo = $em->getRepository('AppBundle\Entity\Account'); $a = $repo->findByQuery($query);
高级用法
可以使用 @DnLinkArray 注解将字段与另一个 ldap 对象映射
namespace AppBundle\Entity; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\Attribute; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\ObjectClass; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\Dn; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\Sequence; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\ArrayField; use L3\Bundle\LdapOrmBundle\Annotation\Ldap\DnPregMatch; /** * Class for represent Groups * * @ObjectClass("groupOfNames") * @Dn("cn={{ entity.name }},{% for entite in entity.entities %}ou={{ entite }},{% endfor %}{{ baseDN }}") */ class Group { /** * @Attribute("cn") */ private $name; /** * @Attribute("gidnumber") * @Sequence("cn=gidSequence,ou=sequences,ou=gram,{{ baseDN }}") */ private $id; /** * @Attribute("member") * @DnLinkArray("AppBundle\Entity\Account") */ private $members; /** * @DnPregMatch("/ou=([a-zA-Z0-9\.]+)/") */ private $entities = array('groups'); /** * Set the name of the group * * @param string $name the name of the group */ public function setName($name) { $this->name = $name; } /** * Set members of the groups * * @param string $members the name of the group */ public function setMembers($members) { $this->members = $members; } /** * Set the id of the group * * @param integer $id the id of the group */ public function setId($id) { $this->id = $id; } /** * Add a member to the group * * @param Account $member the mmeber to add */ public function addMember($member) { $this->members[] = $member; } /** * Remove a member to the group * * @param Account $member the mmeber to remove */ public function removeMember($member) { foreach($this->members as $key => $memberAccount) { if($memberAccount->compare($member) == 0) { $this->members[$key] = null; } } $members = array_filter($this->members); $this->members = $members; } /** * Return the Entities of group * * @param array $entities */ public function setEntities($entities) { $this->entities = $entities; } /** * Return the name of the group * * @return string name of the group */ public function getName() { return $this->name; } /** * Return the id of the group * * @return integer id of the group */ public function getId() { return $this->id; } /** * Return the members of the group * * @return array of object Accounts representing the of the group */ public function getMembers() { return $this->members; } /** * Return the name of the group * * @return string name of the group */ public function getEntities() { return $this->entities; } }
之后,您可以使用实体,如下例所示
$a = new Account(); $a->setUid('john.doo'); $a->setFirstname('John'); $a->setLastname('Doo'); $a->setAlias(array('jdoo','j.doo')); $em = $this->get('l3_ldap_orm.entity_manager'); $em->persist($a); $em->flush(); $g = new Group(); $g->setName('Administrators'); $g->addMember($a); $em->persist($g); $em->flush(); $repo = $em->getRepository('AppBundle\Entity\Account'); $a = $repo->findOneByUid('john.doo'); /* Retreve all group of $a */ $groupRepository = $em->getRepository('AppBundle\Entity\Group'); $groups = $groupRepository->findByMember($a);
包含的功能
- 管理 ldap 实体
- 持久化实体
- 删除实体
- 检索实体
- 通过属性在 ldap 中查找
- 通过引用在 ldap 中查找