【问题标题】:Cloud Firestore Rules : What is wrong with these rules for a multi-user access based on rolesCloud Firestore 规则:这些基于角色的多用户访问规则有什么问题
【发布时间】:2020-04-18 08:40:03
【问题描述】:

我有一个事件集合和用户集合。有2种用户。管理员和标准。标准用户有一个管理员作为他们的父级。对于事件集合中的每个事件,如果他是该事件的管理员或该事件的标准用户,我需要授予读取、写入、更新或删除的权限。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

   function isAdminForEvent() {
        return request.auth.uid == resource.data.uid;
   }
   function isStandardUserForEvent() {
   return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.parent_uid == resource.data.uid;
   }
    match /events/{eventID}/{document=**} {
      allow read,create,update,delete: if isAdminForEvent() || isStandardUserForEvent();
    }

    match /users/{userID} {
        allow read, write: if true;
    }
  }
}

模拟器运行良好,但在真实设备上,只允许读取。标准用户和管理员都不能进行写操作。

【问题讨论】:

  • 请编辑您的问题以包含您遇到问题的最少代码。请务必记录所有相关变量(例如 UID 和 parent_uid 的值),并将该记录的输出也包含在您的问题中。

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


【解决方案1】:

请务必注意,如果您的规则导致异常,则整个操作将被拒绝。

另请注意,resource.data 是写入之前的数据,request.resource.data 是写入之后的数据。

function isStandardUserForEvent() {
  return exists(/databases/$(database)/documents/users/$(request.auth.uid))
      && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.parent_uid == resource.data.uid;
}

match /events/{eventID}/{document=**} {
  allow read,update,delete: if isAdminForEvent() || isStandardUserForEvent();
  allow create: if request.auth.uid == request.resource.data.uid;
}

【讨论】:

  • 谢谢,但是在将我的规则更改为您提供的规则后,我无法看到“我的活动”页面。它会在我登录后立即将我注销。另外,作为一方请注意,events 集合有 2 个子集合 - 客人和交易,除了描述事件的常用字段。
猜你喜欢
  • 2021-06-21
  • 2020-11-28
  • 1970-01-01
  • 2019-06-15
  • 2021-06-13
  • 2020-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多