【问题标题】:Firebase: prevent all but .push to a listFirebase:阻止除 .push 之外的所有内容到列表
【发布时间】:2017-03-28 03:23:13
【问题描述】:

我已经阅读了In firebase, can I set permissions to allow only PUSH operations to a given object? 并复制了它,但它不起作用。

我有一个匿名使用的网站,我在其中收集 cmets。我想存储要添加的新数据,但要防止编辑和删除现有数据。

Angular 2,带有 AngularFire2

export class FirebaseService {

  comments: FirebaseListObservable<Object>;
  locations: FirebaseListObservable<Object>;

  constructor(private af: AngularFire) {
      this.comments = this.af.database.list(environment.stem + '/comments');    
  }

  addComment(comment) {
    return this.comments.push(comment)
      .catch(err => console.error(err));
  }
}

我希望这条规则会这样做,因为在我推送的情况下将没有现有数据?

{
  "rules": {
    ".read": true,
    ".write": "!data.exists()"
  }
}

将写入规则设置为true 使其工作,但比我想要的要宽松。

这里是数据库这条路径中现有 json 的开头(这里的键来自我正在迁移的服务)

{
  "0763a8d3e2407697" : {
    "comment" : "Thanks for your comments and feedback, Simon",
    "date" : "Sat Jan 03 2015 08:46:17 GMT+0100 (CET)"
  },...

【问题讨论】:

  • 您能否添加您想要允许的操作的代码您想要拒绝的操作的代码?另外请在该位置添加现有的 JSON(作为文本,请不要截图)。
  • @FrankvanPuffelen 查看更新的问题

标签: typescript firebase firebase-realtime-database firebase-security angularfire2


【解决方案1】:

根据您当前的规则,只要数据库中存在任何数据,数据库服务器就会拒绝写入。

但是您的用例似乎只想拒绝覆盖特定现有评论的写入。对于该用例,您应该将 .write 规则放在树的下方:

{
  "rules": {
    ".read": true,
    "$stemid": {
      "comments": {
        "$commentid": {
          ".write": "!data.exists() && newData.exists()"
        }
      }
    }
  }
}

这些规则use $ variables 确保它们适用于所有词干和所有 cmets。

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 1970-01-01
    • 2021-08-26
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多