【发布时间】:2021-11-19 08:41:15
【问题描述】:
我想知道 Symfony 中的安全性是如何工作的。我在security.yaml这行代码:
# Admins can go to /account-management
- { path: '^/account-managent', roles: [ROLE_ADMIN] }
这拒绝对所有人的访问,除了具有管理员角色的用户进入 /account-management 和之后的任何内容。
现在我有一个帐户管理控制器。但我想知道我是否需要使用像 $this->denyAccessUnlessGranted('ROLE_ADMIN'); 或 $this->isGranted('ROLE_ADMIN') 这样的拒绝访问功能。
带有内联 cmets 的控制器:
/**
* @Route("/account-management") // Is this class completely protected by security.yaml? Or does it work function specific?
*/
class AccountManagementController extends AbstractController
{
/**
* @Route("/{id}", name="account_management_delete", methods={"POST"})
*/
public function deleteUser()
{
// I should not need this here right? Since I already have this configured in my security.yaml.
// $this->denyAccessUnlessGranted('ROLE_ADMIN');
# code...
}
public function handleUserData()
{
// Do I need this here? Since there is no route thing connected with this?
$this->denyAccessUnlessGranted('ROLE_ADMIN');
# code...
}
}
那么 security.yaml 是如何工作的呢?什么时候应该使用拒绝访问功能?
【问题讨论】:
标签: php symfony security controller symfony-4.4