用户角色/权限的架构

时间:2012-05-17 12:29:59

标签: mysql sql database database-design

我目前正在创建一个应用程序,用户可以从网站控制Minecraft服务器(它将与他们在服务器上安装的插件进行交互)。我试图想出一个合适的设计,允许每个服务器定义自己的用户权限组。最后,将有一个预定义的选择列表(我的插件将知道如何处理的东西),供每个服务器选择并分配给他们创建的组。我的目标是拥有它,因此本网站上的1个用户可能在服务器面板中拥有多个组,并且用户可以被分配到多个服务器面板。

这是我目前提出的数据库布局:

Table: Roles     (will hold the predefined roles)
  Columns:
    serverTypeId (to distinguish between roles allowed for the 2 versions of minecraft)
    roleId       (the role's unique id, primary key)
    roleName     (some name to distinguish the role's purpose)

Table: ServerGroup (will hold the custom per server groups)
  Columns:
    serverId         (the server's unique id)
    groupId    (the server role's unique id, primary key)
    groupName  (name given to group, like Staff, Admin, etc)
    roleList         (a csv list of roleId's from the Roles table)

Table: ServerPermissions
  Columns:
    serverId       (the server's unique id)
    serverGroupId  (the server's groupId that this user belongs to)
    userId         (the user's id that is unique to the whole website)

有更好的方法吗?这是我能够提出的最佳方式。我不是最精通SQL的,我正在寻找改进的方法。

1 个答案:

答案 0 :(得分:1)

这看起来像一个相当坚固的设计,有一点需要注意。 ServerGroup表中的roleList列是列中的重复组,应该避免使用。不要将所有的roleID塞进一个列,而是将其分解为一个单独的表,该表将作为Roles和ServerGroup之间的联结。

Table: ServerGroupRoles (will list roles for each ServerGroup)
  Columns:
    groupID (foreign key to ServerGroup)
    roleID (foreign key to Roles)
  Primary Key: groupID, roleID

对于ServerGroup拥有的每个角色,ServerGroupRoles中都会有一条记录。这种方法对重复组的好处是易于搜索和维护。检查ServerGroup是否存在特定角色的查询可能是使用相等性查找索引,而不是使用LIKE扫描索引。在维护方面,从ServerGroup中删除角色是一个简单的DELETE / WHERE语句,而不是一个不必要的昂贵的字符串操作。

如果您的Web应用程序要求将权限作为CSV字符串处理,那么从针对ServerGroupRoles的查询构建字符串将是微不足道的。但是,如果应用程序发生变化,您将不会遇到重复组造成的低效问题。

相关问题