Laravel全球范围认证 [英] Laravel Global Scope Auth

查看:68
本文介绍了Laravel全球范围认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户模型中创建了一个anonimus全局范围,如下所示,以便仅在前端获得公共用户:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('is_public', function(Builder $builder) {
        $builder->where('is_public', '=', 1);
    });
}

但是...当我需要在后端执行登录时,我当然需要检查非公共用户,因此我需要排除全局范围.

这是否可能使用laravel的默认AuthController?

非常感谢!

解决方案

您只需要创建两个模型-一个没有全局范围的模型(即AuthUser),另一个具有扩展第一个模型的全局模型(即User). /p>

然后,您可以使用AuthUser进行身份验证,并在其他任何地方使用User

.

I've created an anonimus global scope in the users model as below in order to get only public users in the frontend:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope('is_public', function(Builder $builder) {
        $builder->where('is_public', '=', 1);
    });
}

But... when i need to perform the login in the backend i need of course to check for not-public users so i need to exclude the global scope.

Is this possibile using the default AuthController of laravel?

Many Thanks!!

解决方案

You just need to create two Models - one without global scope (i.e. AuthUser) and another with the global scope that extends the first one (i.e. User).

Then you can use AuthUser for authentication And User everywhere else.

这篇关于Laravel全球范围认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆