Cloud Firestore规则:基于角色的多用户访问这些规则有什么问题

时间:2020-04-18 08:40:03

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

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

function saveObject(){
    $KEY ='YOUR_FIREBASE_SERVER_KEY';
    $headers = array('Content-Type:application/json', 'Authorization:key='.$KEY);

    //$object = new StdClass();
    //$object->key = "value";

    // or array....

    $object = array('key' => 'value');

    $data = json_encode($object);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://YOURFIREBASEURL/YOUR_ROOT.json");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // <--- This line important
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    // <--- This line to
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($ch);

    if(curl_errno($ch)){
        return false;
    }

    curl_close($ch);

    return $response;
}

模拟器可以正常工作,但在真实设备上只能读取。标准用户和管理员都无法执行写操作。

1 个答案:

答案 0 :(得分:0)

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

还请注意,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;
}