请随意评论和分享的任何反馈,以帮助我找到一个解决方案。谢谢 !
这是授权表定义每个用户的范围。我目前不知道如何在product
级范围的数据和处理不同的角色。我相信我的数据库架构是错误的设置。
C
创建,R
阅读,U
更新,D
删除
我有PostgreSQL的9以下数据库结构
1)user
表里面has_many
表stores
条目。
-----------------------------
| id | org_tag | email |
|---------------------------|
| 1 | a | [email protected] |
|---------------------------|
| 2 | b | [email protected] |
|---------------------------|
| 3 | c | [email protected] |
-----------------------------
2)stores
表包括jsonb
柱employees
,所述user_id
和tags
阵列列
该store
所有者可以employees
增加,这将有取决于authorization
特殊role
授权店。
I滤波器基于tags
阵列scope
数据并处理所述授权的存储区。
----------------------------------------------
| id | user_id | employees | tags |
|---------------------------------------------|
| 1 | 1 | see json1 | ['a', 'b'] |
|---------------------------------------------|
| 1 | 1 | see json2 | ['a', 'c'] |
-----------------------------------------------
该json1
值
[{'email':'[email protected]','role':'owner'},{'email':'[email protected]','role':'trial_editor'}]
该tags
对应user.org_tag
该json2
值
[{'email':'[email protected]','role':'owner'},{'email':'[email protected]','role':'manager'}]
3)products
表
该trial product editor
只能编辑,他被分配到产品。我能范围product
s通过tags
,但我不知道如何处理不同roles
对于防爆。商店经理可以创建产品,而Trial Product Editor
只能读取和更新。
--------------------------------
| id | user_id | tags |
|------------------------------|
| 1 | 1 | ['a', 'b'] |
|------------------------------|
| 1 | 1 | ['a', 'c'] |
--------------------------------
你似乎是有桌子太吝啬。你似乎需要:
employees
storeEmployees
(假设员工可以在多于一个的存储)storeEmployeeTags
实现一个多到多的使用关系阵列可以是一个合理的选择。你有多个这样的关系,所以我建议你实现数据模型更比列的表。
用户
------------------------------------------------------
| id | org_tag | email | org_handle |
------------------------------------------------------
| 1 | a | [email protected] | xyz |
------------------------------------------------------
| 2 | b | [email protected] | xxx |
------------------------------------------------------
| 1 | c | [email protected] | abc |
------------------------------------------------------
角色
------------------------------------------------
| id | store_id | user_id | position |
------------------------------------------------
| 1 | 1 | 1 | owner |
------------------------------------------------
| 2 | 1 | 2 | manager |
------------------------------------------------
| 1 | 2 | 2 | trial_editor |
------------------------------------------------
商店
-----------------------------------
| id | employees | org_handle |
-----------------------------------
| 1 | see json1 | abc |
-----------------------------------
| 2 | see json2 | xyz |
-----------------------------------
员工jsonb是:
[{'email':'[email protected]','role':'owner'},{'email':'[email protected]','role':'trial_editor'}]
产品
--------------------------------
| id | store_id | role_id |
--------------------------------
| 1 | 1 | 1 |
--------------------------------
| 2 | 2 | 2 |
--------------------------------
Organization has_many :users
User has_many :stores, through: roles
User has_many :role
User belongs_to :organization
Role belongs_to :user
Role belongs_to :store
Role has_many :products
Store has_many :users, through: roles
Store has_many :roles
Store has_many :products
Product belongs_to :store
Product belongs_to :role