1

假设我的系统中有三个用户SamJohn并且Sarah. 这三个人都有一个角色,该角色editor允许他们访问create articleedit article和。现在由于某种原因,我不想获得权限但仍然具有.publish articledelete articleSamdelete articleeditor

如何使用这个 spatie/laravel-permission 包在 Laravel 中实现这一点?(假设我可能需要定期执行此类操作,并且在一段时间后我可能会delete article再次分配Sam. 并且任何角色都有太多权限,所以我无法手动执行。)

4

1 回答 1

0

您可以创建自定义策略。

在策略中,您为您的用户或用户组设置了例外。

创建和保存您的策略。(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;
        }
    }
}
于 2020-02-06T11:00:38.017 回答