zen/ldap-bundle

此包的最新版本(2.1.0)没有可用的许可证信息。

从 imag/ldap-bundle 衍生的适用于 Symfony 2 的 LDAP Bundle

2.1.0 2012-07-31 07:38 UTC

This package is not auto-updated.

Last update: 2024-09-11 10:09:00 UTC


README

LdapBundle 提供了一个不依赖 apache mod_ldap 的 LDAP 认证系统。它使用 php-ldap 扩展以表单的形式对用户进行认证。LdapBundle 还可用于授权。它检索 LDAP 用户的角色。

联系方式

您可以在 freenode irc 上尝试联系我;频道 #symfony-fr;昵称:aways

安装

  1. 下载 LdapBundle
  2. 配置自动加载器
  3. 启用 Bundle
  4. 配置 LdapBundle security.yml
  5. 导入 LdapBundle security.yml
  6. 导入 LdapBundle 路由
  7. 实现注销
  8. 订阅 PRE_BIND 事件

获取 Bundle

Composer

修改项目根目录下的 composer.json

// {root}/composer.json

{
    [...],
    "require": {
        [...],
        "imag/ldap-bundle": "dev-master"
    }
}

启用 Bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
    // ...
    new IMAG\LdapBundle\IMAGLdapBundle(),
    );
}

配置 security.yml

# src/IMAG/LdapBundle/Resources/config/security.yml

security:
  firewalls:
    restricted_area:
      pattern:          ^/
      anonymous:        ~
      provider:         ldap
      imag_ldap:        ~
      # alternative configuration
      # imag_ldap:
      #   login_path:   /ninja/login
      logout:
        path:           /logout
        target:         /

  providers:
    ldap:
      id: imag_ldap.security.user.provider
                
  encoders:
    IMAG\LdapBundle\User\LdapUser: plaintext

  access_control:
    - { path: ^/login,          roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/,               roles: IS_AUTHENTICATED_FULLY }

imag_ldap:
  client:
    host: your.host.foo
    port: 389
#    version: 3 # Optional
#    username: foo # Optional
#    password: bar # Optional

  user:
    base_dn: ou=people,dc=host,dc=foo
#    filter: (&(foo=bar)(ObjectClass=Person)) #Optional
    name_attribute: uid
  role:
    base_dn: ou=group, dc=host, dc=foo
#    filter: (ou=group) #Optional
    name_attribute: cn
    user_attribute: member
    user_id: [ dn or username ]

您需要配置 imag_ldap 部分下的参数。

注意

如果未设置,可选参数将使用默认值。您可以禁用此功能;只需将参数设置为 NULL。

imag_ldap:
  # ...
  role:
   # ...
   filter: NULL

导入 security.yml

# app/config/config.yml

imports:
  - { resource: ../../src/IMAG/LdapBundle/Resources/config/security.yml }

导入路由

# app/config/routing.yml

imag_ldap:
  resource: "@IMAGLdapBundle/Resources/config/routing.yml"

实现注销

只需创建一个带有注销目标的链接。

<a href="{{ path('logout') }}">logout</a>

注意:您可以参考官方的 Symfony 文档:[https://symfony.ac.cn/doc/2.0/book/security.html#logging-out](https://symfony.ac.cn/doc/2.0/book/security.html#logging-out)

订阅 PRE_BIND 事件

现在您可以在用户在 Ldap 上认证之前执行自己的逻辑。如果您想中断认证,只需返回一个异常。

订阅

<tag name="kernel.event_listener" event="imag_ldap.security.authentication.pre_bind" method="onPreBind" />

示例

<?php
use IMAG\LdapBundle\Event\LdapUserEvent,

public function onPreBind(LdapUserEvent $event)
{
    $user = $event->getUser();
    $config = $this->appContext->getConfig();

    $ldapConf = $config['ldap'];

    if (!in_array($user->getUsername(), $ldapConf['allowed'])) {
        throw new \Exception('Not allowed ldap user');
    }

    $user->addRole('ROLE_LDAP');
}