【问题标题】:How to check if user's status is active in laravel 5.x?如何检查用户的状态是否在 laravel 5.x 中处于活动状态?
【发布时间】: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


【解决方案1】:

这是正确的做法:http://laravel.io/forum/11-04-2014-laravel-5-how-do-i-create-a-custom-auth-in-laravel-5?page=1#reply-29759

你的方法不起作用,因为 Laravel 默认的 Auth::attempt 不会检查 is_active 条件。

【讨论】:

    猜你喜欢
    • 2014-05-24
    • 2017-12-30
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 2020-01-11
    • 2019-12-10
    相关资源
    最近更新 更多