toshy62/ldapobjectbundle

OpenLdapObject 的 Symfony2 扩展包

安装次数: 26

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

类型:symfony-bundle

1.1.5 2023-12-26 22:49 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:23:32 UTC


README

LdapObjectBundle : OpenLdapObject 的 Symfony2 扩展包

LdapObjectBundle 是一个 Symfony2 扩展包。它是一个用于检索、修改和持久化 LDAP 实体的接口。它需要一个额外的扩展包:OpenLdapObject 扩展包,这是一个使用 PHP 本地 LDAP 函数的简单 LDAP 连接器。它可以与 Symfony 版本 2、3 和 4 一起使用,并添加了复杂查询和条件 LDAP 的系统。

安装

步骤 1:下载扩展包

打开命令行控制台,进入您的项目目录,并执行以下命令以下载此扩展包的最新稳定版本

$ composer require openldapobject/ldapobjectbundle "~1"

此命令要求您全局安装了 Composer,如 Composer 文档中的“安装”章节所述。

或者,在您的 composer.json 中添加此扩展包,并运行此命令 composer update

...
    "require" : {
        ...
        "openldapobject/ldapobjectbundle": "~1.0",
        ...
    },
...

步骤 2:配置

app/config/parameters.ymlapp/config/parameters.yml.dist 中添加配置键,并为您自己的 openldap 进行配置

    ldap_hostname: ldap-test.univ.fr
    ldap_base_dn: 'dc=univ,dc=fr'
    ldap_dn: 'cn=login,ou=ldapusers,dc=univ,dc=fr'
    ldap_password: 'password'

app/config/config.yml 中添加扩展包的配置键

# Ldap
open_ldap_object_ldap_object:
    host:     "%ldap_hostname%"
    dn:       "%ldap_dn%"
    password: "%ldap_password%"
    base_dn:  "%ldap_base_dn%"

步骤 3:启用扩展包

然后,通过在您的项目的 app/AppKernel.php 文件中添加以下行来启用扩展包

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new OpenLdapObject\Bundle\LdapObjectBundle\OpenLdapObjectLdapObjectBundle(),
        );

        // ...
    }

    // ...
}

步骤 4:使用扩展包

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

对于账户

<?php

namespace AppBundle\Entity;

use OpenLdapObject\Entity;
use OpenLdapObject\Collection\EntityCollection;
use OpenLdapObject\Annotations as OLO;

/**
 * @OLO\DN(value="ou=accounts")
 * @OLO\Entity({"udlAccount"})
 */
class Account extends Entity {
    /**
     * @OLO\Column(type="string")
     * @OLO\Index
     */
    private $uid; 

    /**
     * @OLO\Column(type="array")
     */
    private $sn;

    /**
     * @OLO\Column(type="array")
     */
    private $udlAccountDisplaySn;
    
    /**
     * @OLO\Column(type="array")
     */
    private $givenName;
    
    /**
     * @OLO\Column(type="array")
     */
    private $udlAccountDisplayGivenName;

    /**
     * @OLO\Column(type="string")
     */
    private $mail;

    /**
     * @OLO\Column(type="string")
     */
    private $supannEntiteAffectationPrincipale;
    
    /**
     * @OLO\Column(type="string")
     */
    private $eduPersonPrimaryAffiliation;

    /**
     * @OLO\Column(type="array")
     */
    private $eduPersonAffiliation;
  
    /**
     * @OLO\Column(type="array")
     */
    private $supannEntiteAffectation;
       
    /**
     * @OLO\Column(type="string")
     */
    private $supannCivilite;
    
    /**
     * @OLO\Column(type="array")
     */
    private $memberOf;
    

    public function getUid() {
        return $this->uid;
    }

    public function setUid($value) {
        $this->uid = $value;
        return $this;
    }

    public function getSn() {
        return $this->sn;
    }

    public function setSn($sn) {
        $this->sn = $sn;
        return $this;
    }
            
    public function addSn($value) {
        $this->sn->add($value);
        return $this;
    }

    public function removeSn($value) {
        $this->sn->removeElement($value);
        return $this;
    }

    public function getGivenName() {
        return $this->givenName;
    }
    
    public function setGivenName($givenName) {
        $this->givenName = $givenName;
        return $this;
    }
    
    public function addGivenName($value) {
        $this->givenName->add($value);
        return $this;
    }

    public function removeGivenName($value) {
        $this->givenName->removeElement($value);
        return $this;
    }
    
    public function getUdlAccountDisplayGivenName() {
        return $this->udlAccountDisplayGivenName;
    }
    
    public function setUdlAccountDisplayGivenName($udlAccountDisplayGivenName) {
        $this->udlAccountDisplayGivenName = $udlAccountDisplayGivenName;
        return $this;
    }
    
    public function addUdlAccountDisplayGivenName($value) {
        $this->udlAccountDisplayGivenName->add($value);
        return $this;
    }

    public function removeUdlAccountDisplayGivenName($value) {
        $this->udlAccountDisplayGivenName->removeElement($value);
        return $this;
    }

    public function getUdlAccountDisplaySn() {
        return $this->udlAccountDisplaySn;
    }
    
    public function setUdlAccountDisplaySn($udlAccountDisplaySn) {
        $this->udlAccountDisplaySn = $udlAccountDisplaySn;
        return $this;
    }
    
    public function addUdlAccountDisplaySn($value) {
        $this->udlAccountDisplaySn->add($value);
        return $this;
    }
    
    public function removeUdlAccountDisplaySn($value) {
        $this->udlAccountDisplaySn->removeElement($value);
        return $this;
    }
    
    public function getMail() {
        return $this->mail;
    }

    public function setMail($value) {
        $this->mail = $value;
        return $this;
    }
    
    public function getSupannEntiteAffectationPrincipale() {
        return $this->supannEntiteAffectationPrincipale;
    }
    
    public function setSupannEntiteAffectationPrincipale($supannEntiteAffectationPrincipale) {
        $this->supannEntiteAffectationPrincipale = $supannEntiteAffectationPrincipale;
        return $this;
    }

    public function getEduPersonPrimaryAffiliation() {
        return $this->eduPersonPrimaryAffiliation;
    }

    public function setEduPersonPrimaryAffiliation($eduPersonPrimaryAffiliation) {
        $this->eduPersonPrimaryAffiliation = $eduPersonPrimaryAffiliation;
        return $this;
    }

    public function getEduPersonAffiliation() {
        return $this->eduPersonAffiliation;
    }
    
    public function setEduPersonAffiliation($eduPersonAffiliation) {
        $this->eduPersonAffiliation = $eduPersonAffiliation;
        return $this;
    }

    public function addEduPersonAffiliation($value) {
        $this->eduPersonAffiliation->add($value);
        return $this;
    }

    public function removeEduPersonAffiliation($value) {
        $this->eduPersonAffiliation->removeElement($value);
        return $this;
    }
    
    public function getSupannEntiteAffectation() {
        return $this->supannEntiteAffectation;
    }
    
    public function setSupannEntiteAffectation($supannEntiteAffectation) {
        $this->supannEntiteAffectation = $supannEntiteAffectation;
        return $this;
    }

    public function addSupannEntiteAffectation($supannEntiteAffectation) {
        $this->supannEntiteAffectation->add($supannEntiteAffectation);
        return $this;
    }

    public function removeSupannEntiteAffectation($supannEntiteAffectation) {
        $this->supannEntiteAffectation->removeElement($supannEntiteAffectation);
        return $this;
    }
    
    public function getSupannCivilite() {
       return $this->supannCivilite; 
    } 
    
    public function setSupannCivilite($value) {
        $this->supannCivilite = $value;
        return $this;
    }
    
    public function getMemberOf(){
       return $this->memberOf; 
    } 
    
    public function setMemberOf($memberOf) {
        $this->memberOf = $memberOf;
        return $this;
    }
    
    public function addMemberOf($memberOf) {
        $this->memberOf->add($memberOf);
        return $this;
    }

    public function removeMemberOf($memberOf) {
        $this->memberOf->removeElement($memberOf);
        return $this;
    }
}
  • 每个属性的名称都是 ldap 对象字段
  • 列:使用此注解来指定 ldap 对象字段(字符串或数组)的类型
  • 实体:使用此注解将 ldapObjectClass 属性分配给 PHP 实体类
  • Dn:使用此注解用 twig 语法构建 dn

之后,您可以使用实体,如下例所示

$a = new Account();
$a->setUid('john.doo');
$a->setGivenname('John');
$a->setSn('Doo');
$em = $this->get('ldap_object.manager');
$em->persist($a);
$em->flush();

$repo = $em->getRepository('AppBundle\Entity\Account');
$a = $repo->find('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('ldap_object.manager');
$repo = $em->getRepository('AppBundle\Entity\Account');
$a = $repo->findByQuery($query);

对于组

<?php

namespace AppBundle\Entity;

use OpenLdapObject\Entity;
use OpenLdapObject\Annotations as OLO;

/**
 * @OLO\Dn(value="ou=groups")
 * @OLO\Entity({"udlGroupe"})
 */
class Group extends Entity {
    
    /**
     * @OLO\Column(type="string")
     * @OLO\Index
     */
    private $cn;
    
    /**
     * @OLO\Column(type="string")
     */
    private $description;
    
    /**
     * @OLO\Column(type="array")
     */
    private $member;

    public function getCn() {
        return $this->cn;   
    }
    
    public function setCn($cn) {
        $this->cn = $cn;
        return $this;
    }
    
    public function getDescription() {
        return $this->description;   
    }
    
    public function setDescription($description) {
        $this->description = $description;
        return $this;
    }
   
    public function getMember() {
        return $this->member;   
    }
    
    public function setMember($member) {
        $this->member = $member;
        return $this;
    }
    
    public function addMember($member) {
        $this->member->add($member);
        return $this;
    }

    public function removeMember($member) {
        $this->member->removeElement($member);
        return $this;
    }
}

之后,您可以使用实体,如下例所示

$g = new Group();
$g->setCn('APP:TEST');
$g->setDescription('APPTEST');
$g->addMember('uid=1940,ou=accounts,dc=univ,dc=fr');
$em = $this->get('ldap_object.manager');
$em->persist($g);
$em->flush();

$repo = $em->getRepository('AppBundle\Entity\Group');
$g = $repo->find('APP:TEST');

对于结构

<?php

namespace AppBundle\Entity;

use OpenLdapObject\Entity;
use OpenLdapObject\Annotations as OLO;

/**
 * @OLO\Dn(value="ou=structures")
 * @OLO\Entity({"udlStructure"})
 */
class Structure extends Entity {
    /**
     * @OLO\Column(type="string")
     * @OLO\Index
     */
    private $supannCodeEntite;
    
    /**
     * @OLO\Column(type="string")
     */
    private $ou;
    
    /**
     * @OLO\Column(type="string")
     */
    private $supannCodeEntiteParent;
    
    /**
     * @OLO\Column(type="string")
     */
    private $description;

    /**
     * 
     * @OLO\Column(type="string")
     */
    private $udlStructureLibelleCourt;

    public function getSupannCodeEntite() {
        return $this->supannCodeEntite;
    }

    public function setSupannCodeEntite($supannCodeEntite) {
        $this->supannCodeEntite = $supannCodeEntite;
        return $this;
    }
    
    public function getOu() {
        return $this->ou;
    }
     
    public function setOu($ou) {
        $this->ou = $ou;
        return $this;
    }
    
    public function getSupannCodeEntiteParent() {
        return $this->supannCodeEntiteParent;   
    }
    
    public function setSupannCodeEntiteParent($supannCodeEntite) {
        $this->supannCodeEntiteParent = $supannCodeEntite;
        return $this;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setDescription($description) {
        $this->description = $description;
        return $this;
    }
    
    public function getUdlStructureLibelleCourt() {
        return $this->udlStructureLibelleCourt;
    }
    
    public function setUdlStructureLibelleCourt($udlStructureLibelleCourt) {
        $this->udlStructureLibelleCourt = $udlStructureLibelleCourt;
        return $this;
    }
}

之后,您可以使用实体,如下例所示

$s = new Structure();
$s->setSupannCodeEntite('STTRUCMUCHE');
$s->setOu('STTRUCMUCHE);
$s->setSupannCodeEntiteParent('STUDL');
$s->setDescription('{acronyme1}STRUCTURE TRUC MUCHE');
$em = $this->get('ldap_object.manager');
$em->persist($s);
$em->flush();

$repo = $em->getRepository('AppBundle\Entity\Structure');
$s = $repo->find('STTRUCMUCHE');