@Security批注的自定义消息
我正在尝试对路线使用@Security
注释.像这样:
I'm trying to use @Security
annotations for my routes. Like this:
/**
* @return Response
* @Route("/action")
* @Security("has_role('ROLE_USER')")
* @Template()
*/
public function someAction()
{
return array();
}
当安全限制引发异常时,我收到消息Expression "has_role('ROLE_USER')" denied access
.
When the security restriction fires an exception, I get the message Expression "has_role('ROLE_USER')" denied access
.
这是不可以显示给最终用户的,所以我试图找到一种方法来定制用于注释的消息.
This is not acceptable to be shown to the end user, so I'm trying to find a way to customize the message for annotation.
简单的解决方法是不使用@Secutity
注释并编写如下代码:
Simple workaround is to not to use @Secutity
annotations and write code like these:
/**
* @return Response
* @Route("/action")
*
* @Template()
*/
public function someAction()
{
if (!$this->get('security.context')->isGranted('ROLE_USER')) {
throw new AccessDeniedException('You have to be logged in in order to use this feature');
}
return array();
}
但这不太方便,也不太可读.
But this is less convenient and less readable.
是否可以将自定义消息写入@Security
注释?
Is it possible to write custom message to @Security
annotations?
As soon as I realized that this is not possible, I have made a pull request to the Sensio FrameworkExtra Bundle
to make this possible.
此PR允许通过指定消息参数来自定义显示的消息,例如
This PR allows to customize displayed message by specifying the message parameter like
@Security("has_role('ROLE_USER')",message="You have to be logged in")