【问题标题】:Laravel Authentication - Email in different tableLaravel 身份验证 - 不同表中的电子邮件
【发布时间】:2019-04-14 22:41:24
【问题描述】:

我有两张桌子:

人:

id   name   email   phone   person_type_id

用户

id   person_id  password role_id etc...

您能告诉我如何让 Laravel (5.8) 内置的身份验证系统在验证用户身份时使用 persons 表中的 email 字段吗?仅供参考,守卫提供者的价值必须是用户

【问题讨论】:

  • 您可以在 auth 配置文件中更改默认用户模型,但我仍然认为它不会起作用,因为它需要电子邮件和密码在同一模型中。唯一的另一种方法是创建一个自定义守卫来覆盖 Larvels 默认值

标签: php laravel laravel-5 laravel-authentication


【解决方案1】:

您可以创建自定义用户提供程序(继承自 Illuminate\Auth\EloquentUserProvider)并覆盖 retrieveByCredentials 方法。

Laravel Docs - The User Provider Contract

【讨论】:

    【解决方案2】:

    @Thomas 的建议帮助我解决了这个问题。如果它可以帮助任何人,这是我的代码:

    我创建了 CustomUserProvider.php

    <?php
    
    namespace App\Providers;
    use Illuminate\Support\Str;
    use Illuminate\Auth\EloquentUserProvider;
    use Illuminate\Contracts\Hashing\Hasher as HasherContract;
    
    class CustomUserProvider extends EloquentUserProvider { 
    
    private $method_to_email_model;
    
    public function __construct(HasherContract $hasher, $model, $method_to_email_model)
    {
    
        parent::__construct($hasher, $model);
    
        $this->method_to_email_model = $method_to_email_model;
    }
    
    public function retrieveByCredentials(array $credentials)
    {
    
        if (empty($credentials) ||
           (count($credentials) === 1 &&
            array_key_exists('password', $credentials))) {
            return;
        }
    
        $query = $this->createModel()->newQuery();
    
        foreach ($credentials as $key => $value) 
        {
            if (Str::contains($key, 'password')) {
                continue;
            }
    
            if (is_array($value) || $value instanceof Arrayable) {
    
                $query->with([$this->method_to_email_model => function($q) use($key, $value){
                    $q->whereIn($key, $value);
                }]);
    
            } else {
    
    
                $query->with([$this->method_to_email_model => function($q) use($key, $value){
                    $q->where($key, $value);
                }]);
            }
        }
    
        return $query->first();
     }   
    
    }
    

    然后在 App\Providers\AuthServiceProvider.php 文件中,在 boot 函数里面:

    Auth::provider('custom_user_provider', function ($app, array $config) {
        return new CustomUserProvider($app['hash'], $config['model'], $config['method_to_email_model']);
    });
    

    config/auth.php 里面

     'providers' => [ 
        'users' => [
            'driver'                => 'custom_user_provider',
            'model'                 => App\User::class,
            'method_to_email_model' => 'person',
        ],
      ],
    

    终于在 App\User.php(用户模型)中

    protected $appends = [
        'email', 'first_name', 'last_name'
     ];
    
    public function person()
    {
        return $this->belongsTo(Person::class, 'person_id', 'id');
    }
    
    public function getEmailAttribute()
    {
        return $this->person->getAttribute('email');
    }
    
    public function getFirstNameAttribute()
    {
        return $this->person->getAttribute('first_name');
    }
    
    public function getLastNameAttribute()
    {
        return $this->person->getAttribute('last_name');
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 1970-01-01
      • 2023-03-18
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多