动态选择Laravel身份验证驱动程序

时间:2019-06-26 17:47:40

标签: php laravel laravel-5 laravel-authentication

目前,我的应用中有两个用户级别。一个标准用户,使用Laravel的内置数据库身份验证;和一个管理用户,该用户通过Adldap2使用LDAP身份验证。

config/auth.php

...
"guards" => [
    "web" => [
        "driver" => "session",
        "provider" => "users",
    ],
    "admin" => [
        "driver" => "session",
        "provider" => "admins",
    ],

"providers" => [
    "users" => [
        "driver" => "eloquent",
        "model" => App\User::class,
    ],
    "admins" => [
        "driver" => "ldap",
        "model" => App\Admin::class,
    ],
],
...

此设置完全没有问题。

现在,有可能来自公司外部的管理用户。这些用户将不会通过LDAP进行身份验证,而是通过新admins表中的数据库记录进行身份验证。我可以设置第三个提供程序,但是显然进入应用程序并在检查了权限的任何地方重写都不会很有趣,所以我希望有另一种方法。

假设我们有LDAP用户使用LDAP域登录,例如username@corp.internal,是否可以根据提供的用户名将驱动程序从“ ldap”更改为“ eleoquent”?

1 个答案:

答案 0 :(得分:1)

我很确定,有了https://github.com/Adldap2/Adldap2-Laravel,您可以做自己想做的事,这是我过去做的。

此库具有以下选项:

'login_fallback' => env('ADLDAP_LOGIN_FALLBACK', true), // While default is false

该选项可让您:

"/*
|--------------------------------------------------------------------------
| Login Fallback
|--------------------------------------------------------------------------
|
| The login fallback option allows you to login as a user located on the
| local database if active directory authentication fails.
|
| Set this to true if you would like to enable it.
|
| This option must be true or false and is only
| applicable to the DatabaseUserProvider.
|
*/"

这就是您所需要的:如果用户在LDAP上没有凭据,请在本地数据库中搜索该用户。

关于优先级顺序的整个主题在这里:

https://github.com/Adldap2/Adldap2-Laravel/issues/221