Ola*_*lav 9 security symfony twig
我的应用程序显示了一个项目列表,项目详细信息页面和编辑这些项目的表单.这些是路线:
只有其所有者可以编辑项目.
我已经实现了一个选民,以防止非所有者访问/ project/42/edit.
现在,我还想隐藏项目详细信息页面中的"编辑项目"链接.这样做的方法是什么?理想情况下,在Twig,我想做点什么
{% if may_access(path('project_edit', { 'id': project.id })) %}
<a href="{{ path('project_edit', { 'id': project.id }) }}">edit project</a>
{% endif %}
我可以将此函数实现为Twig扩展,但可能已存在类似的功能.
Ola*_*lav 11
函数is_granted()实际上有第二个参数,允许我做我需要的:
{% if is_granted("MAY_EDIT", project) %}
<a href="{{ path('project_edit', { 'id': project.id }) }}">edit project</a>
{% endif %}
我将它与控制器操作中的检查结合使用:
public function editAction(Project $project)
{
if (!$this->get('security.context')->isGranted('MAY_EDIT', $project)) {
$this->flash('You are not allowed to edit this project');
return $this->show($project);
}
// ...
}
这实际上非常类似于nifr 在自定义字段中对Sonata User - Security的回答中使用的方法.我希望找到一种方法让自动调用选民并避免调用isGranted().
如果你想查看完整的代码,那就是我在github上发布的教程项目.
| 归档时间: |
|
| 查看次数: |
4823 次 |
| 最近记录: |