【问题标题】:How to restrict access to a model property in loopback如何在环回中限制对模型属性的访问
【发布时间】:2019-11-14 23:32:50
【问题描述】:

我目前正在处理我的 loopback3 (3.26.0) api 的访问控制,并且努力限制除所有者之外的所有人对属性的访问。

假设我有一个用户 -> 运动员关系。运动员拥有“secretProperty”。

我将 secretProperty 设置为 protected 并且我有一个

Athlete.afterRemote('**', function(ctx, modelInstance, next) { // i am restricting property access here, which works for direct .find()}

here所述

但我仍然有问题,当我查询时

GET /user/{id}/athlete

返回的数据包含我的 secretProperty,我的钩子永远不会被调用

如何限制对所有端点的属性的访问? (基本上将其设置为隐藏,但不适用于某些角色/用户)

这是我的模型定义: 运动员.json

{
  "name": "athlete",
  "plural": "athletes",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string",
      "required": false,
      "default": ""
    },
    "mySecretProperty": {
      "type": "number",
      "required": false,
      "default": 0
    }
  },
  "relations": {
    "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": ""
    }
  }
}

user.json

{
    "name": "user",
    "plural": "users",
    "base": "User",
    "idInjection": true,
    "options": {
        "validateUpsert": true
    },
    "validations": [],
    "relations": {
        "athlete": {
            "type": "hasOne",
            "model": "athlete",
            "foreignKey": ""
        }
    }
}

【问题讨论】:

  • 您好,您可以显示描述您的作者和书籍模型的文件吗?
  • 我添加了我的模型定义(并将模型名称更改为我的实际名称)

标签: javascript rest loopbackjs loopback


【解决方案1】:

尝试向 Athlete 模型添加属性:"protected": ["mySecretProperty"], 并且您在模型之间设置了关系,但没有设置它应该运行的字段,因此将不起作用。

https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#protected-properties

【讨论】:

  • 我已经尝试过受保护和隐藏的属性,但它们并没有涵盖我的用例。 mySecretProperty 应该对某些角色/用户可用,但对其他角色/用户不可用。此外,它们仅在我 {include: "athlete"} 他们但提到的 /user/
【解决方案2】:

Loopback 不提供属性级安全性,仅提供模型级安全性。为了提供财产级安全性,我们做了以下工作:

  1. 将属性标记为受保护
  2. 创建用户可以更新受保护属性的自定义端点
  3. 将自定义端点 (2) 的访问权限限制为可以访问该属性的用户或角色。

另一种选择是使用“保存前”挂钩,验证用户和要更新的属性。

他说这是一项手动任务,您必须自己编写代码,因为它不是内置于环回中的

【讨论】:

    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 2015-08-04
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    相关资源
    最近更新 更多