Tra*_*Ret 10 authentication laravel
在我的Laravel应用程序中,用户可以禁用(而不是删除)他们的帐户以从网站上消失.但是,如果他们尝试再次登录,则应自动激活他们的帐户,他们应该成功登录.
这是通过users表中的"active"列和User模型中的全局范围完成的:
protected static function boot() {
parent::boot();
static::addGlobalScope('active', function(Builder $builder) {
$builder->where('active', 1);
});
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是那些非活动帐户无法再次登录,因为AuthController找不到它们(超出范围).
我需要实现的目标:
我现在的想法是使用withoutGlobalScope定位用户,手动验证密码,将"active"列更改为1,然后继续常规登录.
在我的authController中使用postLogin方法:
$user = User::withoutGlobalScope('active')
->where('username', $request->username)
->first();
if($user != null) {
if (Hash::check($request->username, $user->password))
{
// Set active column to 1
}
}
return $this->login($request);
Run Code Online (Sandbox Code Playgroud)
所以问题是如何在不改变Laravel主代码的情况下使AuthController忽略全局范围,因此它将保留更新?
谢谢.
创建一个如下GlobalUserProvider扩展的类EloquentUserProvider
class GlobalUserProvider extends EloquentUserProvider {
public function createModel() {
$model = parent::createModel();
return $model->withoutGlobalScope('active');
}
}
Run Code Online (Sandbox Code Playgroud)
在以下位置注册您的新用户提供商AuthServiceProvider:
Auth::provider('globalUserProvider', function ($app, array $config) {
return new GlobalUserProvider($this->app->make('hash'), $config['model']);
});
Run Code Online (Sandbox Code Playgroud)
最后,您应该在配置文件中将用户提供程序驱动程序更改为 globalUserProvider auth.php。
'providers' => [
'users' => [
'driver' => 'globalUserProvider',
'model' => App\Models\User::class
]
]
Run Code Online (Sandbox Code Playgroud)