douzhi8244 2014-09-23 16:15
浏览 25

Symfony2:控制对资源的访问

I'm working on an application where each user may have access to many Project(s) and where each Project may contain many Campaign(s) and so on and so forth. Then my URLs look like:

/project/{project}/member

/project/{project}/member/{member}

/project/{project}/campaign

/project/{project}/campaign/{campaign}

/project/{project}/campaign/{campaign}/plot

/project/{project}/campaign/{campaign}/plot/{plot}

I've read that @ParamConverter can handle these URLs to suit my needs.

--Problem--

I'm looking for a way to very that any user accessing a project is authorized to... without copy the code inside each controller. One user can be bounded to many projects, granted with (ROLE_MEMBER or ROLE_ADMIN).

  • 写回答

1条回答 默认 最新

  • duanrang2627 2014-09-23 17:19
    关注

    I would suggest making yourself a KernelEvents::CONTROLLER listener per http://symfony.com/doc/current/cookbook/service_container/event_listener.html

    Your listener will be called after the route is matched to a controller but before the controller's action method is called. The request argument will have your project/campaign etc variables set.

    I would then suggest letting this listener load in your project and campaign instead of using param converter. I just think it will be a bit cleaner. All you need to do is to retrieve them then store them back into your request object. They will then end up in your controller action method.

    Once you have your objects then you can use the security context to check the roles and just toss an AccessDenied exception if needed.

    I would actually move the permission checking stuff to one or more security voters http://symfony.com/doc/current/cookbook/security/voters_data_permission.html .

    This will give you more fine grained access control without resorting to a full blown access control list. It also means that you can do the role checking in other places such as templates if need be.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!