xavrsl/ldap

为 Laravel 添加 Ldap 搜索功能

v1.5.2 2016-05-24 13:16 UTC

This package is auto-updated.

Last update: 2024-09-06 08:21:51 UTC


README

本包试图提供一种通过 Eloquent 查询 Ldap 目录的方法,就像查询数据库一样。

它现在还附带了一个 AuthUserProvider,因此您可以将其作为 AuthProvider 集成到您的 Laravel 应用程序中。为了实现这一点,我借鉴了此优秀包的很大一部分: Adldap2/Adldap2-Laravel

我没有在项目中使用 Adldap2-Laravel 的唯一原因是因为它还不支持 OpenLdap,所以如果您使用 ActiveDirectory,请查看它!

安装

从命令行要求它

composer require xavrsl/ldap

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

Xavrsl\Ldap\LdapServiceProvider::class,

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

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

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

php artisan vendor:publish

现在您可以开始使用此包了!

用法

首先请记住设置所有配置参数。所有部分已在注释中进行了良好记录。您要检索的任何属性都必须在 'attributes' 数组中指定。这可能看起来有些奇怪,但构建此包的原因是我需要每两秒钟查询一个大用户列表的 Ldap 目录。因此我需要缓存。我能缓存所有字段唯一的方法是提供一个检索到的属性数组。

  • 返回组织成员的一个属性
// 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 目录工作。