代码之家  ›  专栏  ›  技术社区  ›  fabOnReact

授权处理的非规范化方案

  •  0
  • fabOnReact  · 技术社区  · 6 年前

    这是定义每个用户范围的授权表。 我现在不知道 如何在最短的时间内确定数据范围 product 数量 并处理不同的角色。我相信我的 数据库架构设置错误 .

    C 创造 R 阅读 U D 删去

    enter image description here

    postgresql 9

    1) user 桌子 has_many 里面的条目 stores 桌子

    -----------------------------
    | id  | org_tag  |  email   |
    |---------------------------|
    |  1  |    a     | a@em.com |
    |---------------------------|
    |  2  |    b     | b@em.com |
    |---------------------------|
    |  3  |    c     | c@em.com |
    -----------------------------
    

    商店 表中包括一个 jsonb employees 这个 user_id tags

    这个 store 所有者可以添加 员工 authorization 补助金取决于 role .

    我根据数据过滤商店 标签 排列到 scope 处理数据和授权。

    ----------------------------------------------
    | id  | user_id  |  employees  |     tags     |
    |---------------------------------------------|
    |  1  |    1     |  see json1  |  ['a', 'b']  |
    |---------------------------------------------|
    |  1  |    1     |  see json2  |  ['a', 'c']  |
    -----------------------------------------------
    

    json1 价值是

    [{'email':'a@em.com','role':'owner'},{'email':'b@em.com','role':'trial_editor'}]
    

    这个 标签 对应于 user.org_tag

    这个 json2

    [{'email':'a@em.com','role':'owner'},{'email':'c@em.com','role':'manager'}]
    

    3) products 桌子

    这个 trial product editor 只能编辑分配给他的产品。 我可以观察这个问题 产品 标签 ,但我不知道如何处理不同的问题 roles

    例如 商店经理 Trial Product Editor 只能读取和更新。

    --------------------------------
    | id  | user_id  |    tags     |
    |------------------------------|
    |  1  |    1     | ['a', 'b']  |
    |------------------------------|
    |  1  |    1     | ['a', 'c']  |
    --------------------------------
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Gordon Linoff    6 年前

    你似乎对桌子太吝啬了。您似乎需要:

    • employees 每个员工一行
    • storeEmployees 每个员工每个门店一行(假设员工可以在多个门店)
    • storeEmployeeTags 每个门店/员工/标签一行

    使用数组实现一对多关系是一个合理的选择。您有多个这样的关系,因此我建议您更多地在表中而不是在数组中实现数据模型。

        2
  •  0
  •   fabOnReact    6 年前

    使用者

    ------------------------------------------------------
    | id |  org_tag   |     email      |    org_handle   | 
    ------------------------------------------------------
    |  1 |     a      |  a@email.com   |       xyz       | 
    ------------------------------------------------------ 
    |  2 |     b      |  b@email.com   |       xxx       |  
    ------------------------------------------------------
    |  1 |     c      |  c@email.com   |       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     |
    -----------------------------------
    

    这个

    [{'email':'a@em.com','role':'owner'},{'email':'b@em.com','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