dongyan9838 2019-05-12 20:53
浏览 262

Mongo聚合嵌套数组,查找

I am creating a web application using MongoDB and Go, which includes role based access control. I am storing the information regarding this in 2 collections, permissions and roles.

This is how these two collections look like. Permissions

{
    "operation": "create",
    "resource": "project"
}
{
    "operation": "read",
    "resource": "project"
}
{
    "operation": "update",
    "resource": "project"
}
{
    "operation": "delete",
    "resource": "project"
}
{
    "operation": "create",
    "resource": "user"
}

resource is something on which an operation is performed. So if there is some operation which cannot be performed on some resource, then, I needn't store it. For example user can only be created hence only create user need to be stored. Currently there are only 4 operations(create, read, update, delete) in the scope of the application, but could increase, like upload could come into picture.

Roles

{
    "role": "admin",
    "display_name": "Administrator",
    "permissions": [
      {
        "operation": "create",
        "resource": "project"
      },
      {
        "operation": "read",
        "resource": "project"
      },
      {
        "operation": "update",
        "resource": "project"
      },
      {
        "operation": "delete",
        "resource": "project"
      }
    ]
}

Roles contain role, the name of role to be displayed on UI and the set of permissions that role has.

I need to send this information to UI using a REST API in a specific format, which would describe whether a specific role can perform an operation on a resource or not using the checked flag and whether a specific operation on a resource is editable or not by using the flag isEditable. For example the permissions collection doesn't contain an operation delete on resource user, so it should not be editable, hence flag is set to false. Similarly user can be created, hence it is set to true.

 {
    display_name: "System Administrator",
    role: "admin",
    permissions: [
      {
        resource: "user",
        privilages: {
          create: { checked: false, isEditable: true },
          delete: { checked: false, isEditable: false },
          update: { checked: false, isEditable: false },
          read: { checked: false, isEditable: false }
        }
      },
      {
        resource: "project",
        privilages: {
          create: { checked: true, isEditable: true },
          delete: { checked: true, isEditable: true },
          update: { checked: true, isEditable: true },
          read: { checked: true, isEditable: true }
        }
      }
   ]
}

Is it possible to perform this using mongo aggregations? Or do I need to make modifications in my schema, If yes, then what modifications should I make.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 ELGamal和paillier计算效率谁快?
    • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
    • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
    • ¥15 Arcgis相交分析无法绘制一个或多个图形
    • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
    • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
    • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
    • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
    • ¥30 3天&7天&&15天&销量如何统计同一行
    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码