gpt4 book ai didi

database - 跨 4 个表的多对多关系

转载 作者:行者123 更新时间:2023-12-04 07:39:27 28 4
gpt4 key购买 nike

我正在实现一个基于角色的访问控制系统,它具有以下数据库表。

groups
---------
id (PK)
name
level

resources
---------
id (PK)
name

roles
---------
id (PK)
name

permissions
-----------
id (PK)
name
description


users
-----------
id (PK)
name
group_id(FK - references id on groups)
role_id(FK - references id on roles)
组与资源和角色之间存在多对多关系。所以我有以下连接表。
group_resource
---------------
group_id(FK - references id on groups)
resource_id(FK - references id on resources)

group_role
---------------
group_id(FK - references id on groups)
role_id(FK - references id on roles)
这是问题:
组内的任何给定角色都应仅具有分配给该组的资源的权限。
我不完全确定建模 roles 之间关系的最佳方法是什么? , permissions , 和 resourcesgroup_resource 的上下文中和 group_role关系。
任何建议将不胜感激。
谢谢。

最佳答案

这是一个可能的解决方案,具有一定程度的冗余。

groups (id (PK), name, level)
roles (group_id (FK for groups) ,num_role, name) with PK (group_id, num_role)
users (id (PK), name, group_id, num_role) with (group_id, num_role) FK for roles
resource_types (id (PK), name)
group_resources (resource_type_id (FK for resource_types), group_id (FK for groups) with PK both the attributes
permissions (resource_type_id (FK for resource_types), group_id, num_role, description) with (group_id, num_role) FK for roles
使用此解决方案,应用程序必须在插入权限期间检查资源是否出现在权限中指定的 group_id 中,通常使用触发器。
消除所有这些冗余的一种方法(但在我看来是一个不太令人满意的设计)是消除关系 group_resources ,因为所有信息都可以通过权限找到。

关于database - 跨 4 个表的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67569979/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com