我想控制對我的控制器中某些方法的相同用戶訪問。目前,我使用這個:
$this->denyAccessUnlessGranted('ACCESS', $this->Player($toolRepository));
但是我被迫使用這一行并將 ToolRepository 注入到每個方法中......
最簡單的方法是什么?我看到有 IsGranted 注釋,但我的主題需要是一個學說物件來控制我的 Vote 訪問。
/**
* @Route("/player")
*/
class PlayerController extends AbstractController
{
/**
* @Route("/", name="player")
* @throws Exception
*/
public function Player(ToolRepository $toolRepository): \App\Entity\Tool
{
$playerTool = 'TestTool2';
$tool = $toolRepository->findOneBy(array('libelle' => $playerTool));
if (!$tool) {
throw new Exception('Tool : ' . $playerTool . 'not found!');
}
return $tool;
}
/**
* @Route("/main", name="player")
* @IsGranted ("ACCESS", subject="tool")
* @throws Exception
*/
public function mainPlayer(PlayerRepository $playerRepository, ToolRepository $toolRepository): Response
{
$this->denyAccessUnlessGranted('ACCESS', $this->Player($toolRepository));
$players = $playerRepository->findAll();
return $this->render('player/player_mainpage.html.twig', ['players'=>$players]);
}
}
uj5u.com熱心網友回復:
我認為這個資源應該回答你:Symfony 選民。
您將把您的安全邏輯放在您的自定義投票者中,該投票者將在您的控制器(或您想要控制訪問的每個方法)isGranted()功能的每個函式中呼叫。
Player()對于初學者來說,呼叫你的函式是一種更簡單的方法,你可以保持這樣,但你不應該把它放在 Controller 中,而是使用 Service 。
編輯:
您可以將 ToolRepository 存盤為 Controller 私有屬性并將其注入一個__construct()方法中,這樣您就不必在每個方法中注入 ToolRepository
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/403721.html
標籤:
上一篇:將symfony3.4遷移到4.4:DoctrineMappingException不是有效的物體或映射的超類問題
