dcatadmin通过用户表的status字段限制用户登录.

通过用户表status字段的值限制用户登录.

AuthController 继承自 BaseAuthController

重写AuthController 类的 postLogin 方法 (从父类BaseAuthController复制一份postLogin的方法)

if ($this->guard()->attempt($credentials, $remember)) {
            return $this->sendLoginResponse($request);
}

改造以上代码段

        //$credentials['mark'] = 1;//查询条件增加mark=1的用户
        if ($this->guard()->attempt($credentials, $remember)) {
            //判断用户数据 status字段的值是否为禁用状态
            if($this->guard()->user()->status == User::STATUS_LOCK){
                return $this->validationErrorsResponse([$this->username() => '账号未启用,请联系管理员']);
            }
            return $this->sendLoginResponse($request);
        }

这样就能通过用户表的status字段,判断用户是否允许登录

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 2

为什么不在登录前就验证完这些数据呢?直接 auth()->guard($guard_name)->login($user);

6个月前 评论
liaosp 6个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!