首页 文章

根据用户登录laravel背包获取数据

提问于
浏览
1

我正在使用背包在我的项目中创建管理面板 . 我有两种类型的用户,一种是Superadmin,第二种是admin . 我只想给superadmin授予权限,因为他可以列出,添加,编辑数据库中的所有行 .

但是管理员只能编辑,删除和列出他自己创建的那些行 .

所以请帮助我,我是laravel背包的新手?

1 回答

  • 0

    过滤您在实体的CrudController的 setup() 方法中显示的结果,然后禁止访问更新/销毁方法 .

    你的结果看起来像这样:

    public function setup() 
    {
        // take care of LIST operations
        if (\Auth::user()->hasRole('admin')) {
           $this->crud->addClause('where', 'author_id', '=', \Auth::user()->id);
        }
    }
    

    此外,您需要在 update()destroy() 方法中放置支票,以禁止管理员删除其他人的条目 .

    // place this both inside your update() and inside your destroy() method 
    if (\Auth::user()->hasRole('admin') && $this->crud->entry->author_id!=\Auth::user()->id) {
       abort(405);
    }
    

    希望能帮助到你 .

相关问题