【问题标题】:Loopback ACL if owner is not set如果未设置所有者,则环回 ACL
【发布时间】:2016-06-24 18:05:31
【问题描述】:

我有一个应用程序,您可以在其中开始下订单而无需经过身份验证,只需在付款的最后一步之前注册。这意味着我的模型订单首先在没有任何所有者的情况下创建和更新以保存数据,然后在流程结束时附加 userId。

我怎样才能创建一个允许:

如果没有所有者,每个人都执行 如果有所有者,则只有所有者才能执行其上的所有操作。 目前,有些订单的 userId 为 null,有些订单已设置,如果我想列出我的订单,例如已验证的用户,我有 401。

提前非常感谢

【问题讨论】:

    标签: node.js loopbackjs


    【解决方案1】:

    我成功创建了一个名为softOwner 的新自定义角色。如果有人需要,这里是代码:

    module.exports = function(app) {
      var Role = app.models.Role;
      Role.registerResolver('softOwner', function(role, context, cb) {
        function reject(err) {
          if(err) {
            return cb(err);
          }
          cb(null, false);
        }
    
        var userId = context.accessToken.userId;
        if (!userId) {
          return reject(); // do not allow anonymous users
        }
        context.model.findById(context.modelId, function(err, model) {
          if(err || !model) {
            reject(err);
          }
    
          if(model.userId) {
            // if there is an owner, check that it is the right owner
    
            (model.userId == context.accessToken.userId) ? cb(null, true) : reject();
          } else {
            // if there is no owner, authorize
            cb(null, true);
          }
        });
      });
    };
    

    然后将其添加到 ACL 中:

    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "softOwner",
      "permission": "ALLOW"
    },
    

    【讨论】:

    • 如何设置model.userId?您是否在模型中创建了一个属性,然后中间件在创建时分配它?或者环回是否使用 $owner 属性为您执行此操作?请看stackoverflow.com/questions/39970066/…
    • 你只需要设置一个与用户模型的属于关系并Loopback处理它!
    • 是的,但它的功能有限,比如您需要在请求中指定帖子 ID。获取所有记录不适用于权限,也不能批量更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 2015-11-16
    • 1970-01-01
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多