简体   繁体   English

如何在数据库中存储Symfony2“access_control”信息?

[英]How to store Symfony2 “access_control” information in Database?

I am using Symfony2 and I have users and roles already stored in my DB. 我正在使用Symfony2,我的用户和角色已存储在我的数据库中。 If I set something like below in security.yml it works great: 如果我在security.yml设置类似下面的内容,那么效果很好:

access_control:
   - { path: ^/admin, role: ROLE_ADMIN}
   - { path: ^/users, role: ROLE_MANAGER}

But I would like to store this access_control information in the database, so the user from my system can change the permissions itself by using the administrative interface. 但我想将此access_control信息存储在数据库中,因此我系统中的用户可以使用管理界面更改权限本身。

I looked at ACL and FOSUserBundle but could not find a solution for this. 我查看了ACL和FOSUserBundle但无法找到解决方案。 I found that I could check permissions with something like if ($user->hasRole($role)) but I would need to do this in every controller. 我发现我可以使用if ($user->hasRole($role))类的东西检查权限,但我需要在每个控制器中执行此操作。

Is there a way to define a dynamic "access_control" feature? 有没有办法定义动态“access_control”功能? Maybe something like redirecting the access_control to some class that could return true or false. 也许就像将access_control重定向到某个可能返回true或false的类。 Any solution? 有解决方案吗

The best way would be to setup a specific role (eg DB_ROLE_CHECK ) that you set on your actions/services that you want validated against your database stored roles. 最好的方法是设置您要根据数据库存储角色验证的操作/服务上设置的特定角色(例如DB_ROLE_CHECK )。

Then you would create a security voter that hooks into DB_ROLE_CHECK and validates your request against your database entries. 然后,您将创建一个挂钩到DB_ROLE_CHECK的安全选民,并根据您的数据库条目验证您的请求。

See: 看到:

The access map is built here . 访问映射是在这里构建的。

If no access_control is found, nothing is done. 如果没有找到access_control,则不执行任何操作。

Now, define your own implementation of the AccessMapInterface and override the parameter security.access_map.class with your class. 现在,定义您自己的AccessMapInterface实现,并使用您的类覆盖参数security.access_map.class

You will probably need a factory to build your AccessMap. 您可能需要一个工厂来构建AccessMap。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM