在laravel 5中对来自两个以上表的用户进行身份验证

时间:2017-09-19 05:21:22

标签: php laravel authentication laravel-5.2

据我所知,Auth::attempt用于验证来自users表的用户,但我想验证来自managers表的另一个用户和来自admins表的管理员。我知道已经存在laravel-multiauth插件。但我们是否可以创建自己的AuthServiceProvider来验证来自多个表的用户..?

4 个答案:

答案 0 :(得分:12)

您可以设置多个身份验证保护程序,每个保护程序都有不同的提供程序。提供者定义要使用的表或模型。

config/auth.php中,您按照以下方式设置了providers,并为每个提供商设置了相应的guards

'providers' => [
    'users'  => [
        'driver' => 'eloquent',
        'model'  => App\User::class,
    ],
    'managers'  => [
        'driver' => 'eloquent',
        'model'  => App\Manager::class,
    ],
    'admins'  => [
        'driver' => 'eloquent',
        'model'  => App\Admin::class,
    ]
]

然后你可以这样验证:

Auth::attempt($credentials) // use default guard for simple users
Auth::guard('manager')->attempt($credentials)
Auth::guard('admin')->attempt($credentials)

查看文档here

答案 1 :(得分:9)

首先在Illuminate\Foundation\Auth中创建管理员可验证的

    <?php

namespace Illuminate\Foundation\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

    class Admin extends Model implements
        AuthenticatableContract,
        AuthorizableContract,
        CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;
    }

然后通过扩展Authenticatable管理模型来创建管理模型: -

  <?php
namespace App;
use Illuminate\Foundation\Auth\Admin as Authenticatable;

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

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

之后,您需要修改config/auth.php,如下所示 添加提供商数组

'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ], 

并添加警卫数组。

 'user' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
 'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

现在从用户

进行身份验证
 if (Auth::guard('user')->attempt(['email' => $email, 'password' => $password])) {
        $details = Auth::guard('user')->user();
        $user = $details['original'];
        return $user;
    } else {
        return 'auth fail';
    }

并从管理

进行身份验证
 if (Auth::guard('admin')->attempt(['email' => $email, 'password' => $password])) {
        $details = Auth::guard('admin')->user();
        $user = $details['original'];
        return $user;
    } else {
        return 'auth fail';
    }

答案 2 :(得分:7)

如果你愿意,试试我的想法吧。我希望不同的table具有不同的users。因为如果在其他表中有相同的user,它将无效。

  1. 选择您的优先级表(例如用户)
  2. 添加条件
    • if(Auth::user(attempt(...))
    • elseif(Auth::manager(attempt(...))
    • elseif(Auth::admins(attempt(...)))
  3. 注意:此处的优先级表为users,如果该表中不存在该用户,则会尝试managers表,如果仍然不存在,则会检查admins表,否则(使用else)会返回错误消息。

    其他选项:

    其他选项是使用此程序包sarav/laravel-multiauth。你可以关注这个帖子。 How to use authentication for multiple tables in Laravel 5了解更多信息。

    更多参考资料:

    https://laracasts.com/discuss/channels/general-discussion/using-laravel-auth-for-multiple-tables?page=1

    Can anyone explain Laravel 5.2 Multi Auth with example

    https://laracasts.com/discuss/channels/laravel/52-auth-multiple-tables?page=1

答案 3 :(得分:3)

为经理人表和管理员表创建模型。此模型应扩展config/auth.php

'managers' => [ 'driver' => 'eloquent', 'model' => App\Manager::class, ],

添加到providers数组:

'web_manager' => [
    'driver' => 'session',
    'provider' => 'managers',
 ],

添加到警卫阵列:

LoginController

然后。在php artisan make:auth中(使用Illuminate\Foundation\Auth\AuthenticatesUsers为管理员创建一个)使用特征protected $redirectTo = 'redirect_path_after_manager_login'; protected function guard() { return Auth::guard('web_manager'); } 和 覆盖保护和重定向属性。

Auth::guard('web_manager')->user();

经理模型已通过身份验证,您可以获得经过验证的经理对象side_effect