[英]Symfony voter and multiples reasons
我將用一個簡單的情況來解釋:
在管理中,我想檢查是否可以刪除新聞類別。
什么時候控制的?
如果我使用Symfony選民:
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;
}
我有個問題 :
您無法刪除,因為...
你能幫助我嗎 ?
請記住,這種情況非常簡單。 在我的情況下,我有很多原因(> 10)拒絕刪除或修改,幾乎總是由於數據庫中的關系
因為Voter只是另一項服務,所以您可以添加想要或需要的任何屬性或其他類/服務,以便能夠存儲某種原因的確存在或未發生的原因。
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中添加了一個靜態變量,並且可以從外部讀取它。 您可以輕松地將一個數組添加到(或在投票開始之前清除),或指出可檢索的外部服務中發生或未發生某事的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.