weidacat/ldap

为Laravel添加Ldap搜索功能

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

This package is not auto-updated.

Last update: 2024-09-24 19:19:05 UTC


README

本包旨在提供一种像使用Eloquent查询数据库一样搜索Ldap目录的方法。

它还包含了一个AuthUserProvider,这样你就可以将其作为Laravel应用的AuthProvider进行挂载。为了实现这一点,我借鉴了Adldap2/Adldap2-Laravel这个优秀包的很大一部分功能:Adldap2/Adldap2-Laravel

我之所以没有在我的项目中使用Adldap2-Laravel,仅仅是因为它还不支持OpenLdap,所以如果你使用ActiveDirectory,可以去看看!

安装

从命令行安装

composer require "weidacat/ldap:dev-master"

在Laravel 5.5或更高版本中,此包将自动发现,你可以安全跳过以下两个步骤。

如果使用Laravel 5.4或更低版本,在更新composer后,将服务提供者添加到config/app中

Weidacat\Ldap\LdapServiceProvider::class,

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

'Ldap' => Weidacat\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目录。