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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致