gorg/ldaporm-bundle

针对 Symfony2 的 Ldap ORM

安装次数: 1,195

依赖项: 0

建议者: 0

安全性: 0

星星: 10

关注者: 7

分支: 9

类型:symfony-bundle

dev-master 2015-09-27 16:17 UTC

This package is not auto-updated.

Last update: 2020-01-04 04:22:37 UTC


README

GorgLdapOrmBundle 是一个用于通过 PHP 的原生 LDAP 函数检索、修改和持久化 LDAP 实体的接口。

安装

将此包添加到您的项目中的 composer.json

1.1. 纯 GorgLdapOrmBundle

Symfony 2.1 使用 Composer 来组织依赖项

   {
       "require": {
           "gorg/ldaporm-bundle": "dev-master",
       }
   }

1.2. 声明使用 GorgLdapOrmBundle

// application/ApplicationKernel.php
public function registerBundles()
{
    return array(
        // ...
        new LK\TwigstringBundle\LKTwigstringBundle(),
        new Gorg\Bundle\LdapOrmBundle\GorgLdapOrmBundle(),
        // ...
    );
}

1.3. 配置 LDAP 参数

gorg_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

基本用法

要使用 GorgLdapOrmBundle,您必须向实体添加注解,如下例所示

namespace Gorg\Bundle\Application\Entity;

use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Attribute;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\ObjectClass;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Dn;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Sequence;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\ArrayField;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\DnPregMatch;

/**
 * Class for represent Account
 *
 * @author Mathieu GOULIN <mathieu.goulin@gadz.org>
 * @ObjectClass("Account")
 * @Dn("hruid={{ entity.uid }},{% for entite in entity.entities %}ou={{ entite }},{% endfor %}{{ baseDN }}")
 */
class Account
{
    /**
     * @Attribute("uid")
     */
    private $uid;

    /**
     * @Attribute("firstname")
     */
    private $firstname;

    /**
     * @Attribute("nom")
     */
    private $lastname;

    /**
     * @Attribute("alias")
     * @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 getHruid()
    {
        return $this->hruid;
    }

    public function setHruid($hruid)
    {
        $this->hruid=$hruid;
    }

    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 Sequence 对象的链接
  • 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('gorg_ldap_orm.entity_manager');
$em->persist($a);
$em->flush();

$repo = $em->getRepository('Gorg\Bundle\Application\Entity\Account');
$a = $repo->findOneByUid('john.doo');

高级用法

可以使用 @DnLinkArray 注解将字段映射到另一个 ldap 对象

namespace Gorg\Bundle\Application\Entity;

use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Attribute;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\ObjectClass;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Dn;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\Sequence;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\ArrayField;
use Gorg\Bundle\LdapOrmBundle\Annotation\Ldap\DnPregMatch;

/**
 * Class for represent Groups
 *
 * @author Mathieu GOULIN <mathieu.goulin@gadz.org>
 * @ObjectClass("Groups")
 * @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("memberuid")
     * @DnLinkArray("Gorg\Bundle\GramApiServerBundle\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('gorg_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('Gorg\Bundle\Application\Entity\Account');
$a = $repo->findOneByUid('john.doo');

/* Retreve all group of $a */
$groupRepository = $em->getRepository('Gorg\Bundle\Application\Entity\Group');
$groups = $groupRepository->findByMember($a);

包含的功能

  • 管理 ldap 实体
    • 持久化实体
    • 删除实体
    • 检索实体
  • 在 ldap 中通过属性查找
  • 通过引用在 ldap 中查找