Deployd:最安全,最优雅的方式来获取登录用户创建的特定集合中的所有对象?

时间:2013-11-17 21:37:48

标签: deployd

我认为这个标题几乎说明了一切......对Deployd来说是全新的,所以任何关于如何最好地解决这个问题的指示都会受到赞赏。

1 个答案:

答案 0 :(得分:5)

获取用户创建的集合中的对象(我假设您使用的是javascript库dpd.js):

// Get the current user:
var currentUser;
dpd.users.me(function(result, error) {
    currentUser = result;
});

// query your collection with your currentUser id as parameter
dpd.yourcollection.get({creator:currentUser.id}, function(result) {
    // Do something with the result
    console.log(result);
});

您的收藏集应具有属性“creator”,其中包含创建对象(*)的用户的ID。 然后,为了保护您的后端,请转到集合的ON_GET选项卡中的仪表板,并使用以下代码保护它:

 cancelUnless(isMe(this.creator), "You have to be the creator to view this item", 401);

有关cancellUnless()和isMe()的更多信息: http://docs.deployd.com/docs/collections/reference/event-api.md#s-cancelIf%28%29,%20cancelUnless%28%29-764

保护集合的良好做法是仅在用户被记录时才允许查询:

cancelUnless(me,"You have to be connected to view this item", 401);

用户集合应该特别安全(仅允许管理员或类似的ON_PUT)。

*:要自动将currentUserId存储在creator属性中,您还可以在仪表板的ON_POST事件中添加它:

this.creator = me.id;

此处有更多信息:http://docs.deployd.com/docs/collections/reference/event-api.md#s-me-764