添加和撤销对具有特定角色的特定用户的权限

Adding and revoking permission to a specific user with a specific role

我正在使用此包 https://github.com/spatie/laravel-permission 来获取角色和权限。在我的应用程序中,我有几个角色,例如:

  1. 用户
  2. 雇主
  3. 店主
  4. 供应商

根据信任级别,我希望担任特定角色的每个人都只能访问特定权限。对于 ID 为 7 的角色 user 的用户,我希望他或她拥有权限 editor、'shopping` 而没有其他权限。

ID 为 65 的另一位用户具有相同角色 user 可以拥有 editor,'shopping,'edit profile,'view maps' 权限。

当我查看文档时 https://docs.spatie.be/laravel-permission/v3/basic-usage/role-permissions/

是否可以为特定角色的用户授予与同一角色的其他用户不同的权限?

您可以为此使用 Direct Permissions

来自文档:

A permission can be given to any user:

$user->givePermissionTo('edit articles');

// You can also give multiple permission at once
$user->givePermissionTo('edit articles', 'delete articles');

// You may also pass an array
$user->givePermissionTo(['edit articles', 'delete articles']);

A permission can be revoked from a user:

$user->revokePermissionTo('edit articles');

//Or revoke & add new permissions in one go:    
$user->syncPermissions(['edit articles', 'delete articles']);

你的情况:

// assign the role to the user
$user->assignRole('user');

// assign the permissions to the user
$user->givePermissionTo(['editor', 'shopping', 'edit profile', 'view maps']);

像这样。?

您可以管理的特定用户角色和权限

$user = User::find(7);

$user->assignRole("user"); // assign role to that user

$user->givePermissionTo(['editor','shopping']); // for giving permission to user

$user->revokePermissionTo(['editor','shopping']); // for revoke permission to user

$user = User::find(65);

$user->assignRole("user"); // assign role to that user

$user->givePermissionTo(['editor','shopping','edit profile']); // for giving permission to user

$user->revokePermissionTo(['editor','shopping','edit profile']); // for revoke permission to user