簡體   English   中英

如何僅針對 spatie/laravel-permission 中的特定用戶從角色中刪除權限?

[英]How to remove permission from a role only for a particular user in spatie/laravel-permission?

假設我的系統中有三個用戶SamJohnSarah 這三個人都擁有editor的角色,賦予他們create articleedit articlepublish articledelete 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM