2016-06-24 38 views
0

我有一个应用程序,您可以在没有经过认证的情况下开始下订单,并在付款的最后一步之前注册。这意味着我的模型订单首先在没有任何所有者的情况下创建和更新以保存数据,然后在过程结束时附加userId。如果没有设置所有者,则使用环回ACL

如何创建一个ACL允许:

大家来执行,如果没有业主 如果有一个老板,只有所有者可以在其上运行的一切。 目前,有些订单有userId为空,而其他一些订单已设置,如果我想列出我的订单(例如,已认证的用户),我有401。

感谢很多提前

回答

0

我完成了创建新的自定义角色的成功,被称为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" 
}, 
+0

如何设置model.userId?您是否在模型中创建了属性,然后在创建时将其分配给中间件?或者是使用$ owner属性为你做这个事情吗?请参阅http://stackoverflow.com/questions/39970066/loopback-io-model-acl-principalid-owner – steve76

+0

你只需要设置一个属于与用户模型的关系,并且Loopback处理它! – Ghislaindj

+0

是的,但功能有限,就像你需要在请求中指定帖子ID一样。 GET所有记录都不适用于权限,无论是批量更新。 – steve76

相关问题