环回:具有 belongsTo 关系的模型的多个所有者

Loopback: Multiple owners for a model with belongsTo relation

我是环回的新手。我正在尝试学习和实施 ACL。

我有一个 "PersistedModel" 名为 'Page'。我使用两种不同的模型 'Employee' 和 'Customer',它们都基于内置的 'User' 模型。

关系: 一个页面属于 'Employee' 以及 'Customer'。两者都应该是页面的所有者。客户和员工都有很多页面。所以,我在关系中添加了以下内容:

    "customer": {
      "type": "belongsTo",
      "model": "Customer",
      "foreignKey": "customerId"
    },
    "employee": {
      "type": "belongsTo",
      "model": "Employee",
      "foreignKey": "employeeId"
    }

访问控制列表: 我只想要所有者的 'WRITE' 许可。所以,我在 acls 中添加了以下内容:

   {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    }

当我尝试补丁请求时,所有者客户的请求已成功执行。但是,所有者员工的请求得到 'Authorization error'.

我做错了什么?

环回只检查一个所有者关系 here

对于两个所有者,您需要编写自己的自定义角色并通过 role resolver

注册

最近更新了 LoopBack 文档,他们在通知中添加了这两行:(http://loopback.io/doc/en/lb3/Using-built-in-models.html#user-model)

LoopBack does not support multiple models based on the User model in a single application. That is, you cannot have more than one model derived from the built-in User model in a single app.

所以基本上,我不应该创建两个基于 'User' 模型的不同模型。 :(

正如 loopback 所说,您应该从 User 模型扩展模型,定义角色(从 Role 模型),并通过 RoleMapping 模型(它是 built-in 模型)将角色分配给任何用户或参与者。

例如:

  1. 从用户模型扩展模型并命名为 MyUser。
  2. 创建 'Customer' 和 'Employee' 角色。
  3. 添加关系:

    "customer": { "type": "belongsTo", "model": "MyUser", "foreignKey": "customerId" }, "employee": { "type": "belongsTo", "model": "MyUser", "foreignKey": "employeeId" }

  4. 在 MyUser 模型中,添加以下 ACL:

    { "accessType": "WRITE", "principalType": "ROLE", "principalId": "Customer", "permission": "ALLOW" }, { "accessType": "WRITE", "principalType": "ROLE", "principalId": "Employee", "permission": "ALLOW" }

根据this commit(loopback 3.13.0于2017年9月28日发布),您可以将{ownerRelations: true}设置为