minkbear/adldap2-laravel

Adldap2 for Laravel 5。

安装: 174

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 2

分支: 184

类型:项目

v2.0.6 2016-04-29 08:33 UTC

README

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version License

描述

Adldap2 - Laravel 允许通过根 Adldap2 存储库 实现对活动目录的简单配置、访问和管理。

它包括

  • Adldap 合同 (Adldap\Contracts\AdldapInterface),通过 Laravel 的 IoC 进行依赖注入
  • 一个身份验证驱动器,允许用户通过活动目录轻松登录您的应用程序
  • Adldap 门面 (Adldap\Laravel\Facades\Adldap),从 IoC 中轻松检索 Adldap 实例
  • 支持多个 LDAP 连接

安装

快速入门 - 从零开始

将 Adldap2-Laravel 添加到您的 composer.json 文件

"adldap2/adldap2-laravel": "2.0.*",

然后运行 composer update

完成后,将服务提供者在您的 config/app.php 文件中

Adldap\Laravel\AdldapServiceProvider::class,

然后插入门面

'Adldap' => Adldap\Laravel\Facades\Adldap::class

通过运行发布配置文件

php artisan vendor:publish --tag="adldap"

现在您已经设置好了!

用法

您可以通过其门面执行 Adldap 上的所有方法,如下所示

// Finding a user.
$user = Adldap::getProvider('default')->search()->users()->find('john doe');

// Searching for a user.
$search = Adldap::getProvider('default')->search()->where('cn', '=', 'John Doe')->get();

// Authenticating.
if (Adldap::getProvider('default')->auth()->attempt($username, $password)) {
    // Passed!
}

或者您可以注入 Adldap 合同

use Adldap\Contracts\AdldapInterface;

class UserController extends Controller
{
    /**
     * @var Adldap
     */
    protected $adldap;
    
    /**
     * Constructor.
     *
     * @param AdldapInterface $adldap
     */
    public function __construct(AdldapInterface $adldap)
    {
        $this->adldap = $adldap;
    }
    
    /**
     * Displays the all LDAP users.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        $users = $this->adldap->getProvider('default')->search()->users()->get();
        
        return view('users.index', compact('users'));
    }
}

有关更多详细使用方法,请访问 Adldap2 存储库

身份验证驱动器

Adldap Laravel 身份验证驱动器允许您无缝验证活动目录用户,并在本地数据库中记录用户记录。这允许您像常规 Laravel 应用程序一样轻松地为用户附加信息。

注意:Adldap 身份验证驱动器实际上是从 Laravel 的 eloquent 身份验证驱动器扩展并利用的。

安装

Laravel 5.1

AdldapAuthServiceProvider 插入您的 config/app.php 文件

Adldap\Laravel\AdldapAuthServiceProvider::class,

发布身份验证配置

php artisan vendor:publish --tag="adldap"

config/auth.php 中将身份验证驱动器更改为 adldap

/*
|--------------------------------------------------------------------------
| Default Authentication Driver
|--------------------------------------------------------------------------
|
| This option controls the authentication driver that will be utilized.
| This driver manages the retrieval and authentication of the users
| attempting to get access to protected areas of your application.
|
| Supported: "database", "eloquent"
|
*/

'driver' => 'adldap',

Laravel 5.2

AdldapAuthServiceProvider 插入您的 config/app.php 文件

Adldap\Laravel\AdldapAuthServiceProvider::class,

发布身份验证配置

php artisan vendor:publish --tag="adldap"

打开您的 config/auth.php 配置文件并更改以下内容

更改 web 身份验证保护器内的 provider 条目

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'adldap',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
],

现在将 adldap 提供者添加到您的 providers 数组中

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
    'adldap' => [
        'driver' => 'adldap',
        'model' => App\User::class,
    ],
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

用法

用户名属性

在您的 config/adldap_auth.php 文件中有一个名为 username_attribute 的配置选项。数组的键表示登录表单的输入名称,值表示引用的 LDAP 属性。

此选项仅允许您将输入名称设置为您想要的任何名称,并允许以不同方式登录用户。

在您的登录表单中,将用户名表单输入名称更改为您配置的输入名称。

默认情况下,这设置为 email

<input type="text" name="email" />

<input type="password" name="password" />

如果您没有覆盖默认的 postLogin 方法,则需要将以下内容添加到您的 AuthController 中。

protected $username = 'email';

如果您想使用用户的 samaccountname 来登录,只需更改您的输入名称和身份验证配置即可

<input type="text" name="username" />

<input type="password" name="password" />

注意:如果您使用的是 username 输入字段,请确保您的用户数据库表中也有 username 字段。默认情况下,Laravel 的迁移使用 email 字段。

config/adldap_auth.php

'username_attribute' => ['username' => 'samaccountname'],

注意:实际的身份验证是通过您的 config/adldap_auth.php 文件中的 login_attribute 完成的。

登录

通过使用 Auth::attempt($credentials); 定期登录用户。当用户登录时,使用 Auth::user() 将返回你在 config/auth.php 中配置的 App\User 模型。

同步属性

在你的 config/adldap_auth.php 文件中有一个名为 sync_attributes 的配置选项。这是一个属性数组,其中键是 User 模型属性,值是活动目录用户的属性。

默认情况下,User 模型的 name 属性会同步到 AD 用户的 cn 属性。这意味着,在登录时,Laravel User 模型的 name 属性将被设置为活动目录的通用名 (cn) 属性,然后 保存

可以在这里添加更多属性,但请确保你的 users 数据库表中包含你输入的键。

同步属性回调

注意:此功能是在 v1.3.8 版本中引入的。

如果你需要从包含数组或对象的 Adldap 模型同步属性,你可以使用回调来返回特定值给 Laravel 模型的属性。例如

'sync_attributes' => [

    'name' => 'App\Handlers\LdapAttributeHandler@name',

],

LdapAttributeHandler

namespace App\Handlers;

use Adldap\Models\User;

class LdapAttributeHandler
{
    /**
     * Returns the common name of the AD User.
     *
     * @param User $user
     *
     * @return string
     */
    public function name(User $user)
    {
        return $user->getAccountName();
    }
}

注意:属性处理程序使用 app() 辅助函数构建。这意味着你可以在处理程序的构造函数中类型提示可能需要的任何应用程序依赖项。

绑定 Adldap 用户模型到 Laravel 用户模型

注意:在开始之前,启用此选项将在每个请求上对 AD 服务器上的已登录用户执行单个查询。Eloquent 已经为身份验证做了这件事,但这可能会导致加载时间略有增加(当然,这取决于你的 AD 服务器和网络速度)。

在你的 config/adldap_auth.php 文件中有一个名为 bind_user_to_model 的配置选项。将其设置为 true 将配置的 auth User 模型的 adldapUser 属性设置为 Adldap User 模型。例如

if (Auth::attempt($credentials)) {
    $user = Auth::user();
    
    var_dump($user); // Returns instance of App\User;
    
    var_dump($user->adldapUser); // Returns instance of Adldap\Models\User;
    
    // Retrieving the authenticated LDAP users groups
    $groups = $user->adldapUser->getGroups();
}

你必须将 Adldap\Laravel\Traits\AdldapUserModelTrait 特性插入到配置的 auth User 模型中,或者 将公共属性 adldapUser 添加到模型中。

<?php
// app/User.php

namespace App;

use Adldap\Laravel\Traits\AdldapUserModelTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword, AdldapUserModelTrait; // Insert trait here

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];
}

登录回退

注意:此功能是在 v1.3.9 版本中引入的。你需要重新发布 Adldap Auth 配置文件以获得此选项。

登录回退选项允许你在活动目录身份验证失败时使用 Eloquent 身份验证驱动程序以本地数据库用户身份登录。这在以下环境中非常有用:

  • 你可能有部分活动目录用户和通过网站本身注册的其他用户(用户不存在于你的 AD 中)。
  • 本地开发,其中你的 AD 服务器可能不可用

要启用它,只需在 adldap_auth.php 配置文件中将选项设置为 true。

'login_fallback' => false, // Set to true.

Windows 身份验证(SSO)中间件

注意:此功能是在 v1.4.3 版本中引入的。你需要重新发布 Adldap Auth 配置文件以获得此选项。

要求:此功能假设你已在 IIS 中启用了 Windows Authentication,或以某种方式在 Apache 中启用了它。Adldap 不会为你设置此功能。要启用 Windows 身份验证,请访问:[https://www.iis.net/configreference/system.webserver/security/authentication/windowsauthentication/providers/add](https://www.iis.net/configreference/system.webserver/security/authentication/windowsauthentication/providers/add)

SSO 身份验证允许你通过在服务器上启用 SSO 时预先填充的 $_SERVER['AUTH_USER'](或 $_SERVER['REMOTE_USER])来验证用户。这可以在你的 adldap_auth.php 配置文件中配置。

要使用中间件,将其插入到你的中间件堆栈中

protected $middlewareGroups = [
    'web' => [
        Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        Middleware\VerifyCsrfToken::class,
        \Adldap\Laravel\Middleware\WindowsAuthenticate::class, // Inserted here.
    ],
];

现在当你访问你的网站时,将创建一个用户账户(如果尚未存在),并使用一个随机的 16 位字符串密码自动登录。很酷吧?

登录限制过滤器

注意:此功能是在 v1.4.6 版本中引入的。你需要重新发布 Adldap Auth 配置文件以获得此选项。

在您的 config/adldap_auth.php 配置文件中,您现在可以插入一个原始LDAP过滤器来指定哪些用户可以登录认证。

此过滤器也适用于Windows认证中间件。

例如,为了允许仅包含电子邮件地址的用户登录,请插入以下过滤器:(mail=*)

 /*
 |--------------------------------------------------------------------------
 | Limitation Filter
 |--------------------------------------------------------------------------
 |
 | The limitation filter allows you to enter a raw filter to only allow
 | specific users / groups / ous to authenticate.
 |
 | This should be a standard LDAP filter.
 |
 */

 'limitation_filter' => '(mail=*)',

另一个例子,以下是限制属于特定组用户的登录方式

注意:这也会允许嵌套组用户登录。

'limitation_filter' => '(memberof:1.2.840.113556.1.4.1941:=CN=MyGroup,DC=example,DC=com)',

多个认证连接

注意:此功能在 v2.0.0 版本中引入。

为了动态切换连接,设置您的配置默认连接并尝试重新认证用户

$auth = false;

if (Auth::attempt($credentials)) {
    $auth = true; // Logged in successfully
} else {
    // Login failed, swap and try other connection.
    Config::set('adldap_auth.connection', 'other-connection');
    
    if (Auth::attempt($credentials)) {
        $auth = true; // Passed logging in with other connection.
    }
}

if ($auth === true) {
    return $this->handleUserWasAuthenticated($request, $throttles);
}

return 'Login incorrect!';