Firestore安全规则:request.time“对象未定义”

时间:2018-08-21 00:56:21

标签: google-cloud-firestore firebase-security-rules

我正在尝试根据request.time上的示例创建基于AngularFirebase website的安全规则。

我的功能是

function isThrottled() {
    return request.time < resource.data.lastUpdate + duration.value(1, 'm')
}

我要尝试allow update: if isThrottled() == false

的地方

但是,当我尝试使用此规则更新文档时,由于未在对象上定义时间而失败。

  

错误:simulator.rules行[169]列[12]。物业时间是   在对象上未定义。

不是每个请求都附加一个timeTimeStamp吗?这与我初始化云功能或客户端应用程序有关吗?

以下屏幕截图:

enter image description here enter image description here

编辑

其余更新安全规则的代码段是:

service cloud.firestore {
  match /databases/{db}/documents {
    match /users/{userId} {
      match /username/{id} {
        allow update: if isSelf(userId)
                      && usernameAvailable(incomingData().username)
                      && incomingData().username is string
                      && incomingData().username.size() <= 25
                      && incomingFields().size() == 1
                      && isThrottled() == false;
      }
    }

    function incomingData() {
      return request.resource.data
    }
    function isThrottled() {
        return request.time < resource.data.lastUpdate + duration.value(1, 'm')
        }
    function incomingFields() {
        return incomingData().keys()
    }
    function isSelf(userId) {
        return userId == currentUser().uid;
    }
    function usernameAvailable(username) {
        return !exists(/databases/$(db)/documents/usernames/$(username));
    }


  }
}

username集合是每个user文档下的子集合(在users根集合中。每个username文档只有一个称为username的字段,用户可以更新)。

1 个答案:

答案 0 :(得分:1)

这可能对您的情况没有特别的帮助,但是在检查令牌对象的自定义声明时出现相同的错误。

在访问该字段之前,可以使用in检查对象上是否存在该属性。如果未定义代理,则此代码将产生错误:

allow write: if request.auth != null && request.auth.token.agent == true;

如果未定义代理,则此代码可以正常工作:

allow write: if request.auth != null && "agent" in request.auth.token && request.auth.token.agent == true;
相关问题