【问题标题】:denormalized scheme for authorization handling授权处理的非规范化方案
【发布时间】:2023-03-18 14:52:01
【问题描述】:

请随时发表评论并分享任何反馈,以帮助我找到解决方案。谢谢!

这是定义每个用户范围的授权表。 我目前不知道如何在product 级别确定数据范围并处理不同的角色。我认为我的数据库架构设置错误

C 创建,R 读取,U 更新,D 删除

我使用 postgresql 9

具有以下数据库结构

1) user table has_many stores table 中的条目。

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

2) stores 表包含 jsonbemployeesuser_idtags 数组列

store 所有者可以将employees 添加到商店,这将根据role 获得特殊的authorization 授权。

我根据tags数组将store过滤成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'}]

tags 对应于user.org_tag

json2 的值为

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

3) products

trial product editor 只能编辑分配给他的产品。 我可以通过tags 确定products 的范围,但我不知道如何处理不同的roles

例如。 Store Manager 可以创建产品,而Trial Product Editor 只能读取和更新。

--------------------------------
| id  | user_id  |    tags     |
|------------------------------|
|  1  |    1     | ['a', 'b']  |
|------------------------------|
|  1  |    1     | ['a', 'c']  |
--------------------------------

【问题讨论】:

    标签: sql ruby-on-rails database postgresql authorization


    【解决方案1】:

    你似乎对表格太吝啬了。你似乎需要:

    • employees 每位员工一排
    • storeEmployees 每位员工每家商店一行(假设员工可以在不止一家商店)
    • storeEmployeeTags 每个商店/员工/标签一行

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

    【讨论】:

    • 非常感谢。我将尝试在 StackOverflow 上更加活跃,并为我收到的帮助做出贡献
    【解决方案2】:

    用户

    ------------------------------------------------------
    | 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     |
    -----------------------------------
    

    员工 jsonb是:

    [{'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
    

    【讨论】:

      猜你喜欢
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2012-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多