在数据库中存储应用程序权限

问题描述 投票:12回答:5

我正在为我们公司开发一个应用程序,最终将有很多方法可以将用户限制在特定的部分/模块中。尽管应用程序仍然很小,但我想转向一种新的存储权限的方法,随着应用程序的增长,该方法将易于维护和查询。

[当前,在我们的MySQL数据库中,有一个名为“ user”的表,该表存储用户的ID,用户名和密码。在另一个名为“ user_acl”的表中,如下所示:

user_acl_id
acl_root
acl_news_read
acl_news_write
acl_news_modify
acl_reports_read
acl_reports_write
acl_reports_modify
acl_users_read
acl_users_write
acl_users_modify

我们目前只有3个模块,但是随着时间的流逝,将会创建更多的模块,并且需要为每个模块添加权限。

不是为每个权限创建一列,还有其他方法或存储此信息吗?

php mysql arrays permissions store
5个回答
34
投票

我会这样做。

table name: permission
columns: id, permission_name

然后我可以使用多对多关系表为用户分配多个权限

table name: user_permission
columns: permission_id, user_id

此设计将允许我添加所需数量的权限,并将其分配给所需数量的用户。

虽然上述设计符合您的要求,但我有自己的方法在应用程序中实现ACL。我在这里发布。

我的ACL实现方法是这样的:

  1. 将为用户分配角色(管理员,来宾,员工,公共)
  2. 一个角色将被分配一个或多个权限(user_write,user_modify,report_read等)
  3. 对用户的权限将从他/她所担任的角色继承而来>
  4. 除了从角色继承的权限外,还可以为用户分配手动权限。
  5. 为此,我提出了以下数据库设计。

role
I store the role name here 
+----------+
| Field    |
+----------+
| id       |
| roleName |
+----------+

permission:
I store the permission name and key here 
Permission name is for displaying to user.
Permission key is for determining the permission.
+----------------+
| Field          |
+----------------+
| id             |
| permissionName |
| permissionKey  |
+----------------+

role_permission
I assign permission to role here 
+---------------+
| Field         |
+---------------+
| id            |
| role_id       |
| permission_id |
+---------------+

user_role
I assign role to the user here 
+---------------+
| Field         |
+---------------+
| id            |
| user_id       |
| role_id       |
+---------------+

user_permission
I store the manual permission I may allow for the user here 
+---------------+
| Field         |
+---------------+
| id            |
| user_id       |
| permission_id |
+---------------+

这使我可以更好地控制ACL。我可以允许超级管理员自己分配权限,依此类推。正如我所说的,这只是为了给您这个想法。


0
投票

我相信您应该以jSON格式获得这些权限。


0
投票

[就像易卜拉欣说的那样,专门为您的权限创建一个新表。为用户分配一个代表其权限级别的数值,例如1 =读取,2 =写入/读取,3 =修改/写入/读取。然后在您的代码中,在允许用户执行特定任务之前,检查适当的权限级别。如果它们没有所需的值(修改为3或编写为> = 2),则您阻止该功能。


0
投票

我认为您应该拥有最喜欢的桌子:


0
投票

[在一本非常著名的MySQL性能书High Performance MySQL

© www.soinside.com 2019 - 2024. All rights reserved.