gpt4 book ai didi

部署: Most secure,最优雅的方式来获取登录用户创建的特定集合中的所有对象?

转载 作者:行者123 更新时间:2023-12-02 21:43:14 26 4
gpt4 key购买 nike

我认为标题几乎说明了一切...对于 Deployd 来说是全新的,所以任何关于如何最好地实现这一点的指示都值得赞赏。

最佳答案

要获取用户创建的集合中的对象(我假设您使用的是 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);

有关 cancelUnless() 和 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 存储在创建者属性中,您还可以将其添加到仪表板中的 ON_POST 事件中:

this.creator = me.id;

更多信息请点击:http://docs.deployd.com/docs/collections/reference/event-api.md#s-me-764

关于部署: Most secure,最优雅的方式来获取登录用户创建的特定集合中的所有对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20036497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com