carnegielearning/ldap-orm-bundle

另一个针对Symfony2的LDAP ORM

2.0.0-rc2 2016-11-16 20:49 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:36:36 UTC


README

一个提供LDAP ORM的Symfony扩展包。

此代码基于Ucsf Ldap Orm,而Ucsf Ldap Orm又基于Mathieu GoulinGorgLdapOrmBundle

我们永远感激他们为我们提供了一个优秀的基础,使我们能够在Carnegie Learning继续工作。最初,我们是从UcsfLdapOrm分叉的,但我们发现了很多改进,并希望回馈社区。

到目前为止,有哪些变化和/或新增功能

  • 添加了LdapEntity类。这是一个Symfony实体,代表top LDAP对象类。
  • 添加了许多LdapEntity的子类来描述从topInetOrgPerson的对象类。
  • 添加了Repository::filterByComplex(),这使得实体管理器/仓库能够使用自定义构造的复杂布尔逻辑进行过滤。(有关详细信息,请参阅代码注释API文档。)
  • 完全移除了对TWIG的依赖。

安装

需要PHP5.5+和Symphony 2.8+

  • 使用composer添加
    • composer require carnegielearning/ldap-orm-bundle
  • 将扩展包添加到AppKernel.php中
    • new CarnegieLearning\LdapOrmBundle\CarnegieLearningLdapOrmBundle()
  • 使用composer安装
    • $ composer update carnegielearning/ldap-orm-bundle

测试要求

建议您使用Grunt运行单元测试。这将允许启动一个内存中的LDAP服务器,并便于文件监视。

  • 安装Node.js和npm
  • 将Grunt安装到全局命名空间。有关帮助,请参阅Grunt文档
    • npm install -g grunt-cli
  • 运行npm的安装。
    • npm install
  • 请确保开发环境中存在java二进制文件。有关更多信息,请参阅LDAP SDK
  • 运行Grunt。这将运行一个文件监视器,监视源文件夹/文件以及测试文件夹/文件中的更改。要退出,请确保使用ctrl-c,因为这将会终止内存中的LDAP服务器
    • grunt
  • 还可以仅运行单元测试,而不包括文件监视器部分。这可以通过以下方式完成
    • grunt test

文档

使用LdapOrm进行开发

在config.yml中配置LDAP服务

some_ldap_server:
    connection:
        uri: ldaps://ldap.example.com
        use_tls: true
        bind_dn: cn=admin,dc=example,dc=com
        password: shhhItsASecret
        password_type: plaintext
  • uri: 连接到LDAP服务所需的URI。
  • use_tls: 'true'或'false',以决定是否使用TLS连接
  • bind_dn: 绑定到LDAP服务的DN
  • password: 与给定bind DN关联的密码
  • password_type: sha1plaintext。当URI为LDAPS时,我使用plaintext。

LDAP实体管理器和服务的依赖注入

services:
    myldap_entity_manager:
        class: Ucsf\LdapOrmBundle\Ldap\LdapEntityManager
        arguments: ["@logger", "@annotation_reader", "%some_ldap_server%"]
    comexample_person_service:
        class: MyBundle\ComExamplePersonService
        arguments: [ @myldap_entity_manager ]

创建实体(通常用于表示对象类)

/**
 * Represents a ComExamplePerson object class, which is a subclass of InetOrgPerson
 * 
 * @ObjectClass("comExamplePerson")
 * @SearchDn("ou=people,dc=example,dc=come")
 * @Dn("uid={{ entity.uid }},ou=people,dc=example,dc=com")
 */
class ComExamplePerson extends InetOrgPerson
{
    /**
     * @Attribute("comExampleFavoriteIceCreamFlavor")
     * @Must()
     * @ArrayField()
     * 
     * The @Attribute annotation relates the $comExampleFavoriteIceCreamFlavor member variable to the
     * 'comExampleFavoriteIceCreamFlavor' attribute within the ComExamplePerson object class in LDAP. 
     * You don't have to name the PHP variable the same as your attribute name, but it helps to be
     * consistent in this way.
     *
     * The @Must annotation requires this attribute to not be empty when persisting back to LDAP. If 
     * a @Must requirement is not satisfied, attempting to persist the entry will throw
     * a MissingMustAttributeException.
     *
     * The @ArrayField aannotation tells the LDAP Entity Manager, repositories and services to treat
     * this attribute as a multi-value LDAP field. This is unfortunately backwards from LDAP's default
     * to multi-value an attribute. Baring miracles (i.e. finding the time), this will probably not be "fixed".
     *
     */
    protected $comExampleFavoriteIceCreamFlavor;
    
    ...
    
    public function getComExampleFavoriteIceCreamFlavor() {
        return $this->comExampleFavoriteIceCreamFlavor;
    }
    
    public function setComExampleFavoriteIceCreamFlavor($comExampleFavoriteIceCreamFlavor) {
        $this->comExampleFavoriteIceCreamFlavor = $comExampleFavoriteIceCreamFlavor;
    }
    
    ...
}


#### Coding the Service

class ComExamplePersonService {

protected $comExamplePersonRepository;

public function __construct(LdapEntityManager $entityManager) {
    // Make a repo for ComExamplePerson entities
    $this->comExamplePersonRepository = $entityManager->getRepository(ComExamplePerson::class);
    // Make a another repo for SomethignElse entities (just another example...)
    $this->somethingElseRepository = $entityManager->getRepository(SomethingElse::class);
    ...
}
        
public function getPersonByUid($uid, $includeAddress = false, $attributes = null) {
    $person = $this->comExampePersonRepository->findByUid($uid, $attributes);
    ...
    return $person;
}

#### A Controller... to Round it Out

class PeopleController extends Controller {

    /**
     * @Route("/person/detail/{uid}")
     * @Template()
     */
    public function detailAction(Request $request, $uid)
    {
        $comExamplePersonService = $this->get('comexample_person_service');
        $person = $comExamplePersonService->getPersonByUid($uid);
        ...
        return array('person' => $person);
    }

## To do

1. ~~Remove need for generic LDAP config~~
2. ~~Configuration documentation~~
3. ~~Development example~~
4. Rewrite test suite (In progress...)
5. Remove deprecated search results iterator