volicon/ldap

将 Ldap 搜索功能添加到 Laravel

v1.4.2 2014-09-19 15:39 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:51:52 UTC


README

此 composer 包试图提供一种方法,通过 Ldap 目录进行搜索,就像使用 Eloquent 查询数据库一样。

安装

在 composer.json 文件中声明对此包的依赖

"require": {
	"xavrsl/ldap": "v1.4"
},

然后,运行 composer update 以获取代码。

将服务提供者添加到 config/app

'Xavrsl\Ldap\LdapServiceProvider',

将外观添加到别名数组中(也在 config/app 中)

'Ldap' => 'Xavrsl\Ldap\Facades\Ldap',

然后您需要发布和自定义配置文件,以指示您的 ldap 服务器位置,并设置您的 dn、属性等。

php artisan config:publish xavrsl/ldap

现在您可以使用此包了!

用法

首先请记住设置所有配置参数。所有部分都已通过注释进行了良好的文档说明。您想要检索的任何属性都必须在 'attributes' 数组中指定。

  • 返回组织中的一个成员的属性
// First possibility, with find/where methods and get
Ldap::find('people')->where('uid', 8162)->get('displayname');

// Second possibility, using an alias for the get method
Ldap::find('people')->where('uid', 8162)->displayname;

// Third possibility, attribute in camelCase format
Ldap::find('people')->where('uid', 8162)->displayName;

// If default attribute is set to 'uid' in conf, you can use the short method
Ldap::people(8162)->displayname;

所有这些可能性都应返回相同的字符串(我们的用户显示名)

Bobby Blake
  • 为组织中的一个成员返回多个属性
// Let's directly use the short method
Ldap::people(8162)->get('displayname, mail');

// May as well use an array instead of a string
Ldap::people(8162)->get(['displayName', 'mail']);

这将返回

array(1) [
    '8162' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
]

如果您将配置中的键更改为类似 'login' 的属性,例如,则得到

array(1) [
    'bobblake' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
]

注意:您不需要在配置中的 'attributes' 数组中添加 'key' 属性的值。包会为您处理。

  • 从组织的多个成员中返回多个属性
// Let's use the short method again
Ldap::people('8162, 128')->get('displayname, mail');

// Same thing using arrays
Ldap::people(['8162', '128'])->get(['displayName', 'mail']);

// Longer syntax
Ldap::find('people')->where('uid', ['8162', '128'])->get(['displayName', 'mail']);

// Base your search on another attribute
Ldap::find('people')->where('login', ['bobblake', 'johndoe'])->get(['displayName', 'mail']);

这将返回

array(2) [
    '108' => array(2) [
        'displayname' => string (8) "John Doe"
        'mail' => string (20) "john.doe@domain.org"
    ]
    '8162' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
]

您还可以返回在 'attributes' 配置属性中设置的您所有属性

// The long way
Ldap::find('people')->where('login', ['bobblake', 'johndoe'])->get();

// The short way
Ldap::people('108, 8162')->get();
  • 基于通配符查询 Ldap 目录
// The long way
Ldap::find('people')->where('login', 'bob*')->get(['displayName', 'mail']);

// The short way (assuming you have set the 'filter' attribute to 'login' in config)
Ldap::people('bob*')->get(['displayName', 'mail']);

// Also works with multiple wildcards
Ldap::people('bob*, john*')->get(['displayName', 'mail']);

您将得到类似这样的结果

array(2) [
    '108' => array(2) [
        'displayname' => string (8) "John Doe"
        'mail' => string (20) "john.doe@domain.org"
    ]
    '4021' => array(2) [
        'displayname' => string (10) "John Smith"
        'mail' => string (22) "john.smith@domain.org"
    ]
    '8162' => array(2) [
        'displayname' => string (11) "Bobby Blake"
        'mail' => string (22) "bobby.blake@domain.org"
    ]
    '9520' => array(2) [
        'displayname' => string (12) "Bob McCormac"
        'mail' => string (24) "bobby.mccormac@domain.org"
    ]
]

您应该明白了!

  • 对 Ldap 目录进行身份验证
// Depending on the filter attribute you've set in the config
Ldap::auth('bobblake', 'm7V3ryStr0ngP@ssw0rd!')

将简单地返回 TRUEFALSE

注意:不要忘记在配置中设置用于用户身份验证的 dn 属性。

待办事项

还有很多工作要做,以使此包完整。以下是可以期待的未来功能列表

  • 从 Ldap 创建/更新属性。目前,此包只能读取 Ldap。
  • 查询多个组织单位(Ldap 分支或 OU。例如:人员、组、邮件等)。这应该很快就会实现...
  • 使用 Active Directory 和 Open Ldap。目前,只有 Open Ldap 目录工作。