<?php
namespace App\Security;
use App\Entity\SfGuardUser;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
/**
* 食材システム用権限管理
*
*/
class FoodSystemVoter extends Voter
{
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* 権限チェックを行う対象を確認
*
* コントローラ内で $this->isGranted("fc_system") や $this->denyAccessUnlessGranted("fc_system") が呼び出されたものが対象
* $this->isGranted("other_check") などの場合、このクラスはチェックの対象外となる
*/
protected function supports(string $attribute, $subject)
{
return $attribute === "fc_system";
}
/**
*
*
* @param string $attribute isGranted や denyAccessUnlessGranted の第一引数
* @param mixed $subject 上記メソッドの第二引数 権限チェックに必要な自由な値を入れてください
* s:スタッフ権限 k:管理者権限
*
*/
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
/** @var SfGuardUser $user */
//グループが必要ならこのように呼び出せます。
$group = $user->getGroup();
//管理者は強制的にtrueにしています
if ($this->security->isGranted("ROLE_ADMIN")) {
return true;
}
//TODO 権限チェック処理をここに実装。持ってない場合はfalse
switch ($subject) {
case 's':
if ($this->security->isGranted("ROLE_食材スタッフ")) {
return true;
}
break;
case 'k':
if ($this->security->isGranted("ROLE_食材管理")) {
return true;
}
break;
}
return false;
}
}