Dipen Chand
Dipen Chand

Reputation: 145

Spatie Permission: Multiple Roles on Multiple Pages in Laravel

I've multiple pages, where a user can have multiple roles. But I cannot get pagewise roles of a user in Spatie Permission. How can I achieve Spatie Permission like below:

Page  |  Role   | User |
------------------------
  A   |  Admin  |  1   |
  B   |  Member |  1   |
  A   |  Cashier|  2   |

Upvotes: 1

Views: 1262

Answers (1)

Vlad Vladimir Hercules
Vlad Vladimir Hercules

Reputation: 1859

With Spatie you set roles and permissions, after that within the page, you need to check for relevant permissions, that is what it's meant for. However, you can always extend Spatie with your own set of custom permission handling.

i.e. you can create a config file with a list of routes that each user has access to and then add a helper method.

config file:

return [
    ROLE_ADMIN => [
        'route-name1',
        'route-name2',
        'route-name3',
    ],
    ROLE_MEMBER => ['route-name2'],
    ROLE_CASHIER => [],
];

permission method:

hasPageAccess(string $role, string $routeName): bool  
{
    $permissions =  Arr::get(config('my-page-permissions'), $role, []);

    return !!array_search($routeName, $permissions);
}

Upvotes: 2

Related Questions