简体   繁体   中英

Restricting data access based on user role in laravel

I've the following scenario: I've employee role, which can add and edit only thier own data. Also there is manager role which can view the data of all employees. The data is stored in database. Where should I put the validation in this case.

You can use gates to check if a user has rights to do certain actions. Based on these you can fetch the data that is allowed for this user.

You can create a gate like so:

Gate::define('access-all-records', function ($user) {
    return $user->isManager; // Or any other way to find this out
});

And fetch data like this:

if (Gate::allows('access-all-records')) 
{
    // Fetch all records
}
else
{
    // Fetch data for this user
}

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