【发布时间】:2016-04-26 14:33:44
【问题描述】:
我在 laravel 5.2 提供的 AuthController 中设置了一些不同的登录逻辑。代码是
protected function authenticated(Request $request)
{
$email = $request->input('email');
$password = $request->input('password');
if (Auth::attempt(['email' => $email, 'password' => $password, 'is_active' => 1])) {
return redirect()->intended('admin/dashboard');
}
return $this->sendFailedLoginResponse($request);
}
代码运行良好。但是当用户不活动时的问题,它仍然以用户身份登录。那么如何停止使用is_active = 0 对用户进行身份验证呢?
更新:用户迁移
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->tinyInteger('is_active', 0);
$table->rememberToken();
$table->timestamps();
});
【问题讨论】:
-
你能发布你的用户迁移吗?您尝试使用的逻辑是正确的,因此可能是列类型或类似问题的问题。
-
如果你在你的 User 模型上使用 [软删除][1],Laravel 会自动为你做这件事。您只需将
is_active转换为deleted_at列[1]:laravel.com/docs/5.2/eloquent#soft-deleting -
@LionelShrestha 我已经编辑了您的问题,以包含您上面评论中的相关代码。以后最好在问题中添加代码而不是 cmets。
-
根据经验,用于这些类型字段的最佳字段类型是布尔值。 Eloquent 负责将 0 和 1 转换为 false 和 true。
$table->boolean('is_active')->default(false); -
虽然我同意@PatrickStephan,但您的逻辑应该起作用。 Specifying Additional Conditions 的 Laravel 文档也使用了您的方法。我还检查了
Illuminate\Auth\SessionGuard.php中的attempt方法并按照流程,这种方法似乎应该有效。您是否尝试过清除缓存然后重试?这个方法确实调用了lastAttempted方法,可能那里有陈旧的数据?
标签: php laravel laravel-authorization