t3chn0crat/laravel-ldap-connector

在 Laravel 中使用 LDAP 进行身份验证和数据提取

0.2-Alpha 2015-05-30 20:09 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:33:17 UTC


README

travis build

此包是 dsdevbe 包的分支(https://github.com/dsdevbe/ldap-connector)。

此包允许您使用 Laravel 5.0.x 认证并从 LDAP 获取数据。

它使用 adLDAP 库 在 Laravel 和 LDAP 之间创建桥梁。adLDAP 需要 PHP 5 和 LDAP 及 SSL 库

安装

  1. 通过 Composer 为 Laravel v5.0 安装此包

    composer require T3chn0crat/laravel-ldap-connector:dev-master
  2. 将 Laravel 配置中的身份验证驱动程序更改为使用 ldap 驱动程序。您可以在以下文件中找到此设置 config/auth.php

    'driver' => 'ldap',
  3. config/auth.php 还必须设置一个有效的模型。该模型必须包括

    public function getAuthIdentifier()
    {
        if (isset($this->ldap)) {
            return $this->ldap->samaccountname;
        }
    }
  4. 在 Laravel 配置文件夹中创建一个新的配置文件 ldap.php,位于 app/config/ldap.php,并根据需要进行修改。有关配置的更多详细信息,您可以始终查阅 adLDAP 文档

所有这些都是必需的

return [
	'account_suffix'=>  "@domain.local",
    // Load balancing domain controllers, but only one is requried
	'domain_controllers'=>  [
        "192.168.0.1", 
        "dc02.domain.local"
    ],
	'base_dn'   =>  'DC=domain,DC=local',
    // AD attributes to get http://msdn.microsoft.com/en-us/library/windows/desktop/ms675090%28v=vs.85%29.aspx
    'fields' => [
        'company',
        'department',
        'displayname',
        'homephone',
        'mail',
        'memberof',
        'mobile',
        'primarygroupid',
        'samaccountname',
        'telephonenumber',
        'title',
    ]
];
  1. 完成此操作后,您就到达了最后一步,并且需要添加一个服务提供程序。打开 config/app.php,并将新项添加到 providers 数组中。

    'T3chn0crat\LdapConnector\LdapConnectorServiceProvider'

用法

身份验证

LDAP 插件是 AUTH 类的扩展,其行为将与 Eloquent 驱动程序的正常使用相同。

if (Auth::attempt(array('username' => $email, 'password' => $password)))
{
    return Redirect::intended('dashboard');
}

您可以在 Laravel Auth 文档 中找到更多关于使用 Auth:: 函数的示例。

获取 LDAP 字段

所有 LDAP 字段都存储在 Auth::user()->ldap 对象的公共属性中。

Email: {{ Auth::user()->ldap->mail }}
Department {{ Auth::user()->ldap->department }}

LdapUserObject 方法

  1. isMemberOf($group)

将测试用户是否是传递的组的成员。返回一个布尔值

if (Auth::user()->ldap->isMemberOf('Git Hub Users')) { return 'yes'; }

获取所有用户

您可以使用 LdapService 对象和 getAllUsersWithFields 返回 Laravel Collection 的 LdapUserObjects。

$ldap = App::make('T3chn0crat\LdapConnector\LdapService', [Config::get('ldap')]);
$collection = $ldap->getAllUsersWithFields();

现在您可以应用所有集合函数。结果将是一个 LdapUserObjects 集合

$test = $collection->where('mail', 'test@foo.com');
$department = $test->department;