[英]When should I use the deny access functions in Symfony 4.4 controllers?
我想知道 Symfony 中的安全性是如何工作的。我在security.yaml
中有这行代码:
# Admins can go to /account-management
- { path: '^/account-managent', roles: [ROLE_ADMIN] }
这拒绝访问除具有管理员角色的用户之外的所有人访问 /account-management 以及之后的任何内容。
现在我有一个帐户管理 controller。但我想知道我是否需要使用拒绝访问 function,如$this->denyAccessUnlessGranted('ROLE_ADMIN');
或$this->isGranted('ROLE_ADMIN')
。
Controller 内联评论:
/**
* @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 是如何工作的呢? 什么时候应该使用拒绝访问功能?
让我们尝试改变 POV。
考虑一个涉及更多类型用户的系统,例如管理员、后台操作员(两者都必须经过身份验证)以及可以匿名访问前端或经过身份验证以获取私人内容的简单用户。
您可以定义一个区域(让我们称之为“已验证”),管理员和 BO 操作员都可以在其中访问,但他们有不同的职责。
简单用户可以访问网站、注册、登录以访问他们的个人数据、编辑个人资料等。
BOO他们可以添加内容、批准用户注册等。
管理员所有 BOO 内容加上删除简单用户注册
安全.yaml
# Read https://symfony.com/doc/current/security.html#hierarchical-roles
role_hierarchy:
ROLE_ADMIN: ROLE_BACKOFFICE
access_control:
- { path: '^/authenticated', roles: [ROLE_BACKOFFICE] }
/**
* @Route("/authenticated") // It's protected by the firewall defined in security.yaml
*/
class AccountManagementController extends AbstractController
{
/**
* @Route("/{id}", name="account_management_delete", methods={"POST"})
*/
public function deleteUser()
{
// As backoffice operators due to role hierarchy can access you should restrict the permissions to Admins
$this->denyAccessUnlessGranted('ROLE_ADMIN');
# code...
}
public function handleUserData()
{
// No control except the firewall needed as BOO can already access
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.