![](/img/trans.png)
[英]Laravel redirect based on role - spatie/laravel-permission
[英]How to remove permission from a role only for a particular user in spatie/laravel-permission?
假設我的系統中有三個用戶Sam
、 John
和Sarah
。 這三個人都擁有editor
的角色,賦予他們create article
、 edit article
、 publish article
和delete article
權限。 現在出於某種原因,我不希望Sam
擁有delete article
權限但仍然具有editor
的角色。
如何使用這個 spatie/laravel-permission 包在 Laravel 中實現這一點? (假設我可能必須定期執行此類操作,並且一段時間后我可能會再次將delete article
分配給Sam
。而且任何角色的權限都太多,因此我無法手動執行此操作.)
您可以創建自定義策略。
在策略中,您為您的用戶或用戶組設置例外。
創建和保存您的策略。( https://laravel.com/docs/6.x/authorization#creating-policies )
示例策略
<?php
namespace App\Policies;
use App\User;
use App\Article;
use Illuminate\Auth\Access\HandlesAuthorization;
class ArticlePolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can delete the article.
*
* @param \App\User $user
* @param \App\Post $post
* @return mixed
*/
public function delete(User $user, Article $article)
{
// retrieve the user as you wish.
// I just did a check with his name but it is preferable to have a more
// advanced identification.
if ($user->name = 'SAM')) {
return false;
}
if ($user->can('delete article')) {
return true;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.