arvindh93/cakephp-ldap-utility

此软件包的最新版本(v1.0)没有提供许可证信息。

CakePHP 的 LDAP 实用程序插件

v1.0 2018-02-21 08:43 UTC

This package is not auto-updated.

Last update: 2024-09-19 07:45:10 UTC


README

要求

  • CakePHP 3.1+

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方式是

composer require arvindh93/Cakephp-ldap

用法

在您的应用 config/bootstrap.php 文件中添加

// In config/bootstrap.php
Plugin::load('LdapUtility');

或使用 cake 的控制台

./bin/cake plugin load LdapUtility

配置

创建 LDAP 处理器实例的基本配置

	$config = [
		'host' => 'ldap.example.com',
        'port' => 389,
        'baseDn' => 'dc=example,dc=com',
        'startTLS' => true,
        'hideErrors' => true,
        'commonBindDn' => 'cn=readonly.user,ou=people,dc=example,dc=com',
        'commonBindPassword' => 'secret'
	]
	$ldapHandler = new LdapUtility\Ldap($config);

在控制器中设置 LDAP 身份验证配置

    // In your controller, for e.g. src/Api/AppController.php
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Auth', [
            'storage' => 'Memory',
            'authenticate', [
                'LdapUtility.Ldap' => [
					'host' => 'ldap.example.com',
			        'port' => 389,
			        'baseDn' => 'dc=example,dc=com',
			        'startTLS' => true,
			        'hideErrors' => true,
			        'commonBindDn' => 'cn=readonly.user,ou=people,dc=example,dc=com',
			        'commonBindPassword' => 'secret',
			        'fields' => [
			            'username' => 'cn',
			            'suffix' => 'ou=people,dc=test,dc=com'
			        ]
				]
            ],

            'unauthorizedRedirect' => false,
            'checkAuthIn' => 'Controller.initialize',
        ]);
    }

用法

创建用于搜索/读取操作的查询对象:搜索 - $ldapHandler->search() 读取 - $ldapHandler->read()

查询对象的操作:select() - 接受要从 ldap 条目集中获取的属性数组、setBaseDn() - 接受 baseDn 字符串,默认为配置 - baseDn、where() - 接受过滤器字符串、first() - 执行查询并获取第一个条目详情作为数组、all() - 执行查询并获取所有可能的条目作为数组

示例

搜索以 cn 开头的条目

	$ldapHandler->search()
		->setBaseDn('ou=people,dc=example,dc=com')
		->select(['cn', 'sn', 'mail'])
		->where('cn=test*')
		->all()

搜索以 cn 开头并获取第一个条目

	$ldapHandler->search()
		->setBaseDn('ou=people,dc=example,dc=com')
		->select(['cn', 'sn', 'mail'])
		->where('cn=test*')
		->first()

读取具有 cn=test.user 的特定条目

	$ldapHandler->read()
		->setBaseDn('cn=test.user,ou=people,dc=example,dc=com')
		->select(['cn', 'sn', 'mail'])
		->where('cn=test.user')
		->first()