drsc10888 2017-05-21 09:28
浏览 91
已采纳

Laravel控制器具有角色

I have an application which will be a SaaS and is utilizing user roles. Of course, controllers will need to forward different data depending on user roles or permissions, however I think this approach may lead me to huge controllers and I was wondering if there is a smarter way to do this? For example my user create method:

public function create()
{
    if (Auth::user()->isAdmin()) {
        $clinics = Clinic::pluck('name', 'id');
        $roles = Role::pluck('display_name', 'id');
    }
    else{
        $clinics = Clinic::where('id', Auth::user()->clinic_id)->get()->pluck('name', 'id');
        $roles = Role::where('name', '!=', 'admin')->get()->pluck('display_name', 'id');
    }

    $states = State::pluck('name', 'id');
    $cities = City::pluck('name', 'id');

    return view('users.create', compact('user', 'clinics', 'states', 'cities', 'roles'));
}

Which is okay now when I only implemented admin and non-admin user, but when roles get complicated, is there a cleaner way to assemble this?

  • 写回答

1条回答 默认 最新

  • duanji2014 2017-05-21 09:52
    关注

    I suggest you to take a look to the Scopes of the Laravel Documentation. You can attach the scopes to your models to achieve the same results.

    This solution will not help you deleting code complexity (that is moved in models) but will help you remove code duplication because you will encounter the same "if" multiple times during the development of your application...

    A local scope for your clinics could be like this one

    class Clinic extens Model {
        [...]
        public function scopeCanSee($query)
        {
            $user = Auth::user();
            if(!$user->isAdmin())
                return $query->where('id', $user->clinic_id);
            return $query;
        }
    }
    

    and in your controller you can then filter the results in this way

    public function create()
    {
        $clinics = Clinic::canSee()->pluck('name', 'id');
        [...]
    
        $states = State::pluck('name', 'id');
        $cities = City::pluck('name', 'id');
    
        return view('users.create', compact('user', 'clinics', 'states', 'cities', 'roles'));
    }
    

    Global Scopes

    Another way is to use the Global Scopes (but I haven't tested them)

    class Role extends Model
    {
        protected static function boot()
        {
            parent::boot();
            static::addGlobalScope(new RolesScope);
        }
    }
    class Clinic extends Model
    {
        protected static function boot()
        {
            parent::boot();
            static::addGlobalScope(new ClinicsScope);
        }
    }
    

    and scopes similar to

    class ClinicsScope implements Scope
    {
        public function apply(Builder $builder, Model $model)
        {
            $user = Auth::user();
            $builder->where('id', $user->clinic_id);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗