[英]Symfony2, How to secure all controllers in one place using custom function?
我已经定义了控制器,但我想像下面那样保护所有它们:
// In my Controller Class
public function chooseDateAction()
{
if($this->get('MY.roles_features')
->isGranted($this->container->get('request')->get('_route')))
{
// Do something
}
else
{
throw new AccessDeniedException();
}
return array( );
}
我必须设计自己的“ isGranted”功能,因为roles
是动态的。 顺便说一句,功能正常工作!
所以我的问题是我必须在所有Controllers
中都重复isGranted
函数,还是可以将其放在某个位置以减少代码冗余。
我知道我必须在我的安全性的某些高层中放置isGranted
,但是问题是如何以及在哪里?
尝试编写基本控制器,如果isGranted
方法通过,它将检查构造,否则抛出异常。 例如:
<?php
namespace Acme\DolanBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class BaseController extends Controller
{
public function __construct()
{
if(!$this->get('MY.roles_features')
->isGranted($this->container->get('request')->get('_route')))
{
throw new AccessDeniedException('Gooby pls');
}
}
}
然后,只需在其他控制器中扩展BaseController即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.