简体   繁体   中英

Get data based on user login in laravel backpack

I am using backpack to created admin panel in my project. I have two types of user one is Superadmin and second is admin. I just wanted to give the permissions to superadmin as he can list,add,edit all the rows from database..

but the admin can only edit,delete and list those rows created by himself..

So please help me, I am new in laravel backpack??

Filter the results you show in the setup() method of your entity's CrudController, then disallow access to update/destroy methods.

You result could look something like this:

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

Additionally, you need to place checks inside your update() and destroy() methods, to not allow an admin to delete someone else's entry.

// 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);
}

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM