Symfony 选民和多重原因
Symfony voter and multiples reasons
我会用一个简单的情况来解释:
- 我有 "news" 个实体
- 我有 "new categories" 个实体
在管理中,我想查看是否可以删除新闻类别。
- 如果你没有 "ROLE_SUPERADMIN",你不能 ;
- 如果链接了新闻类别(=在类别中使用),你不能。
什么时候控制那个?
如果我使用 Symfony Voters :
class NewsCategoryVoter extends Voter {
....
private function canDelete(NewsCategory $newsCategory, User $user)
{
// Check ROLE and Count in NewsRepository if $newsCategory is used. I have not yet coded this.
return false;
}
我有一个问题:
- 我不明白他不能删除的原因。在 twig 和 is_granted('delete', category) 之后,最好是:
You can't delete because ...
你能帮帮我吗?
请记住,这种情况非常简单。在我的情况下,我有很多理由(> 10)拒绝删除或修改,几乎总是因为数据库中的关系
因为选民只是另一项服务,您可以添加任何属性,或其他您想要或需要的属性,以便能够存储关于为什么发生或未发生某事的某种原因。
public static $reason;
// in the voter, make grant/deny/abstain choices...
if ($user->hasRole('ROLE_SUPER_ADMIN')) {
self::$reason = "is super-admin";
$this->log->debug("FeatureVoter | {$user} is super-admin");
return VoterInterface::ACCESS_GRANTED;
}
// after is_granted()
echo VoterClass::$reason;
我已经登录了投票器,因此其他一些通知机制也同样简单。在这里,我只是在Voter中添加了一个静态变量,并且可以从外部读取它。您可以简单地制作一个数组,可以将其添加到(并在投票开始前清除),或者记录在可以检索的外部服务中发生或未发生某事的原因。
我会用一个简单的情况来解释:
- 我有 "news" 个实体
- 我有 "new categories" 个实体
在管理中,我想查看是否可以删除新闻类别。
- 如果你没有 "ROLE_SUPERADMIN",你不能 ;
- 如果链接了新闻类别(=在类别中使用),你不能。
什么时候控制那个?
如果我使用 Symfony Voters :
class NewsCategoryVoter extends Voter {
....
private function canDelete(NewsCategory $newsCategory, User $user)
{
// Check ROLE and Count in NewsRepository if $newsCategory is used. I have not yet coded this.
return false;
}
我有一个问题:
- 我不明白他不能删除的原因。在 twig 和 is_granted('delete', category) 之后,最好是:
You can't delete because ...
你能帮帮我吗?
请记住,这种情况非常简单。在我的情况下,我有很多理由(> 10)拒绝删除或修改,几乎总是因为数据库中的关系
因为选民只是另一项服务,您可以添加任何属性,或其他您想要或需要的属性,以便能够存储关于为什么发生或未发生某事的某种原因。
public static $reason;
// in the voter, make grant/deny/abstain choices...
if ($user->hasRole('ROLE_SUPER_ADMIN')) {
self::$reason = "is super-admin";
$this->log->debug("FeatureVoter | {$user} is super-admin");
return VoterInterface::ACCESS_GRANTED;
}
// after is_granted()
echo VoterClass::$reason;
我已经登录了投票器,因此其他一些通知机制也同样简单。在这里,我只是在Voter中添加了一个静态变量,并且可以从外部读取它。您可以简单地制作一个数组,可以将其添加到(并在投票开始前清除),或者记录在可以检索的外部服务中发生或未发生某事的原因。