ACL troubles with loopback.io

≯℡__Kan透↙ 提交于 2019-12-10 11:04:24

问题


I'm currently evaluating loopback.io for developing the API portion of a new project, and I'm having problems with setting the correct ACL entries.

What I wish to accomplish is given an auth token, the GET endpoints should only return objects owned by the user. For example, a request to /Shows?access_token=xxxxxx should return only objects owned by the user.

Below is my shows.json file, and my User model is named Podcaster. Any help would be appreciated.

{
  "name": "Show",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "episodes": {
      "type": "hasMany",
      "model": "Episode",
      "foreignKey": ""
    },
    "podcaster": {
      "type": "belongsTo",
      "model": "Podcaster",
      "foreignKey": ""
    }
  },
  "acls": [
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$authenticated",
      "permission": "ALLOW",
      "property": "create"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$owner",
      "permission": "ALLOW"
    },
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    }
  ],
  "methods": {}
}

回答1:


It's not related to ACL's.

You want to change the business logic of the method. So the best practice is that you create a new method for getting shows owning by current user.

If you want to work your current owner ACl, you need to create a relation between user and show, and set ownerId in the show model.

  {
      "name": "Show",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
        "title": {
          "type": "string",
          "required": true
        },
        "description": {
          "type": "string"
        },
        "description": {
          "type": "string"
        }
        "ownerId": {
          "type": "object"
        }

      },
      "validations": [],
      "relations": {
        "owner": {
          "type": "belongsTo",
          "model": "user",
          "foreignKey": "ownerId"
        },
....


来源:https://stackoverflow.com/questions/39372950/acl-troubles-with-loopback-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!