新建UserProvider,如果继承EloquentUserProvider,注入model是必须的,或者去继承interface,自己实现一些方法

use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

class MyUserProvider extends EloquentUserProvider
{
    protected $model;

    public function __construct($model)
    {
        $this->model = $model;
    }
   

    public function validateCredentials(UserContract $user, array $credentials)
    {
        $plain = $credentials['password'];

        $secret = $user->getAuthPassword();

        if (password_verify($plain, $secret)){
            return true;
        }elseif ($this->think_ucenter_md5($plain) === $secret){
            $user->password = password_hash($plain, PASSWORD_DEFAULT);
            $user->save();
            return true;
        }
    }

    private function think_ucenter_md5($str)
    {
        return md5(sha1($str) . 'VvKl0QZBE7nao5xtXqGkWrMPchRbHdwmLF361izT');
    }


}

app\Providers\AuthServiceProvider.php 中进行注册

    public function boot(GateContract $gate)
    {
        $this->registerPolicies($gate);

        Auth::provider('my', function($app) {
            // 返回Illuminate\Contracts\Auth\UserProvider实例...
            return new MyUserProvider(User::class);
        });
    }

更改config/auth.php

    'providers' => [
        'users' => [
            'driver' => 'my',
            'model' => my\User\User::class,
        ],

 

posted on 2016-07-07 17:18  jzfan  阅读(1375)  评论(0编辑  收藏  举报