【问题标题】:Loopback $owner doesn't works for findById环回 $owner 不适用于 findById
【发布时间】:2019-02-05 18:04:39
【问题描述】:

我想通过环回获取有关我的用户的一些信息。 为此,我创建了一个与 "accessToken" 模型相关的 "user" 模型,直到现在,/user 上的 POST、/user/login 上的 POST 和 /user/logout 上的 POST 正在工作。

我添加了/common/models/user.json

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "accessTokens": {
      "type": "hasMany",
      "model": "accessToken",
      "foreignKey": "userId"
    }
  },
  "acls": [
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW",
      "property": "logout"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW",
      "property": "findById"
    }
  ],
  "methods": {}
}

当我在/user/{id} 上执行 GET 操作时,我得到了:

{
  "error": {
    "statusCode": 401,
    "name": "Error",
    "message": "Autorisation requise",
    "code": "AUTHORIZATION_REQUIRED",
    "stack": "Error: Autorisation requise\n    at..."
  }
}

我想我不是很了解 acl/relation

【问题讨论】:

  • 您能否检查您的model-config.json 以确保内置的User 模型不是公开的?您可能正在路由到它而不是您的自定义用户模型。 findById 也映射到 READ accessType
  • 你能不能把 "accessType": "EXECUTE" 改成 "accessType": "*" 然后检查一下。
  • 您的环回应用程序中是否启用了令牌授权?如果是,您应该在您的/user/{id} 请求中提供访问令牌。

标签: node.js acl relation loopback apiconnect


【解决方案1】:

这可能是因为您只允许$ownerfindById

要限定 $owner,目标模型需要与 User 模型(或扩展 User 的模型)具有 belongsTo 关系,并且需要与目标模型实例的外键匹配的属性。 $owner 的检查仅针对路径上具有“:id”的远程方法执行,例如 GET /api/users/:id。

  1. 确保您提供的accessToken 是您正在寻找的userid 的所有者。
  2. 如果不确定,请尝试替换: "principalId": "$owner""principalId": "$authenticated",然后你就会知道这是不是你的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2017-07-25
    • 2013-01-15
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    相关资源
    最近更新 更多