十月CMS:如何使用角色范围扩展后端用户 [英] October CMS: How to extend the backend user with role scope

查看:111
本文介绍了十月CMS:如何使用角色范围扩展后端用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够扩展Backend\Models\User类并添加范围查询方法以仅检索超级用户:

I've been able to extend the Backend\Models\User class and add a scoped query method to retrieve only super users:

public function boot()
{
    User::extend(function($model) {
        $model->addDynamicMethod('scopeIsSuperUser', function($query) {
            return $query->where('is_superuser', 1);
        });
    });
}

如何为特定组中的用户提供范围方法?就像我只希望角色为"BookManager"的用户一样.可以使用已经在Backend\Models\User类上定义的$groups关系吗?

How can I have a scope method for users who are in a specific group? Like I only want users whose role is "BookManager". Is it possible to use the $groups relation already defined on the Backend\Models\User class?

public $belongsToMany = [
    'groups' => ['Backend\Models\UserGroup', 'table' => 'backend_users_groups']
];

推荐答案

这应该做到

User::extend(function($model) {
    $model->addDynamicMethod('scopeIsBookManager', function($query) {
        return $query->whereHas('groups', function ($query) {
            $query->where('code', 'BookManager');
        });
    });
});

这篇关于十月CMS:如何使用角色范围扩展后端用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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