如何使用Symfony表达式语言在@Security批注中使用类常量? [英] How to use class constants in @Security annotation using the Symfony Expression Language?

查看:81
本文介绍了如何使用Symfony表达式语言在@Security批注中使用类常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony 3,并且创建了一个自定义的Voter类.

I am using Symfony 3 and I've created a custom Voter class.

我想使用 SensioFrameworkExtraBundle 标签.

I want to access it using the SensioFrameworkExtraBundle @Security tag.

这种作品.

如果我执行以下操作,它将很好地工作:

If I do the following it works perfectly:

 /**
 * @Rest\Get("organisation/{id}")
 * @Security("is_granted('OrgAdmin', id)")
 * @param int     $id
 * @param Request $request
 *
 * @return View
 */
public function getOrganisationAction($id, Request $request)
{

但是我不喜欢在应用程序中使用魔术字符串的想法,我宁愿使用类常量进行检查.

But I don't like the idea of using magic strings in the application and I would much rather use a class constant for the check.

类似这样的东西:

/**
 * @Rest\Get("organisation/{id}")
 * @Security("is_granted(AppBundle\OrgRoles::ROLE_ADMIN, id)")
 * @param int     $id
 * @param Request $request
 *
 * @return View
 */
public function getOrganisationAction($id, Request $request)
{

但是当我尝试该操作时,出现以下错误消息:

But when I try that I get the following error message:

Unexpected character \"\\\" around position 20 for expression `is_granted(AppBundle\\OrgRoles::ROLE_ADMIN, id)`.

在未转义的情况下为:

Unexpected character "\" around position 20 for expression `is_granted(AppBundle\OrgRoles::ROLE_ADMIN, id)`.

所以我对此感到困惑.

能做到吗?

有什么更好的方法建议吗?

Any suggestions on a better way to do this?

推荐答案

您可以使用表达式语言中可用的函数"rel =" noreferrer> constant() 函数组件:

@Security("is_granted(constant('\\Full\\Namespace\\To\\OrgRoles::ROLE_ADMIN'), id)")

这篇关于如何使用Symfony表达式语言在@Security批注中使用类常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆