假设我的系统中有三个用户Sam、John和Sarah。这三个人都有一个editor角色,这使他们有权访问create article、edit article、publish article和delete article。现在出于某些原因,我不希望Sam拥有delete article的权限,但仍然拥有editor的角色。
我如何使用这个spatie/ Laravel权限包在laravel中实现这一点?(假设我必须在某种程度上定期执行这样的操作,一段时间后,我可能会再次将Sam分配给back delete article。而且任何角色都有太多的权限,所以我不能手动完成。)
发布于 2020-02-06 19:00:38
您可以创建自定义策略。
在策略中,您可以为用户或用户组设置例外。
要创建和保存策略,请执行以下操作。(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;
}
}
}https://stackoverflow.com/questions/60092984
复制相似问题