รักษาความปลอดภัยการเข้าถึงข้อมูลสำหรับผู้ใช้และกลุ่ม

แอปที่ใช้ร่วมกันได้หลายแอปอนุญาตให้ผู้ใช้อ่านและเขียนข้อมูลต่างๆ ได้ตามชุดสิทธิ์ เช่น ในแอปแก้ไขเอกสาร ผู้ใช้อาจต้องการอนุญาตให้ผู้ใช้บางรายอ่านและเขียนเอกสารของตนเองได้ ขณะเดียวกันก็บล็อกการเข้าถึงที่ไม่ต้องการ

โซลูชัน: การควบคุมสิทธิ์เข้าถึงตามบทบาท

คุณสามารถใช้ประโยชน์จากโมเดลข้อมูลของ Cloud Firestore รวมถึงกฎ ความปลอดภัยที่กำหนดเองเพื่อใช้การควบคุมสิทธิ์เข้าถึง ตามบทบาทในแอป

สมมติว่าคุณกำลังสร้างแอปพลิเคชันการเขียนร่วมกันที่ผู้ใช้สร้าง "เรื่องราว" และ "ความคิดเห็น" ได้โดยมีข้อกำหนดด้านความปลอดภัยดังนี้

  • เรื่องราวแต่ละเรื่องมีเจ้าของ 1 คนและแชร์กับ "ผู้เขียน" "ผู้แสดงความคิดเห็น" และ "ผู้อ่าน" ได้
  • ผู้อ่านจะเห็นได้เฉพาะเรื่องราวและความคิดเห็น โดยจะแก้ไขอะไรไม่ได้
  • ผู้แสดงความคิดเห็นมีสิทธิ์เข้าถึงทั้งหมดเหมือนผู้อ่าน และยังเพิ่มความคิดเห็นในเรื่องราวได้ด้วย
  • ผู้เขียนมีสิทธิ์เข้าถึงทั้งหมดเหมือนผู้แสดงความคิดเห็น และยังแก้ไขเนื้อหาเรื่องราวได้ด้วย
  • เจ้าของสามารถแก้ไขส่วนใดก็ได้ของเรื่องราว รวมถึงควบคุมสิทธิ์เข้าถึงของผู้ใช้รายอื่น

โครงสร้างข้อมูล

สมมติว่าแอปของคุณมีคอลเล็กชัน stories ซึ่งเอกสารแต่ละฉบับแสดงถึงเรื่องราว นอกจากนี้ เรื่องราวแต่ละเรื่องยังมีคอลเล็กชันย่อย comments ซึ่งเอกสารแต่ละฉบับเป็นความคิดเห็นเกี่ยวกับเรื่องราวนั้น

หากต้องการติดตามบทบาทการเข้าถึง ให้เพิ่มช่อง roles ซึ่งเป็นแผนผังของรหัสผู้ใช้กับบทบาท

/stories/{storyid}

{
  title: "A Great Story",
  content: "Once upon a time ...",
  roles: {
    alice: "owner",
    bob: "reader",
    david: "writer",
    jane: "commenter"
    // ...
  }
}

ความคิดเห็นมีเพียง 2 ช่อง ได้แก่ รหัสผู้ใช้ของผู้เขียนและเนื้อหาบางส่วน

/stories/{storyid}/comments/{commentid}

{
  user: "alice",
  content: "I think this is a great story!"
}

กฎ

เมื่อบันทึกบทบาทของผู้ใช้ไว้ในฐานข้อมูลแล้ว คุณต้องเขียนกฎความปลอดภัยเพื่อตรวจสอบบทบาทเหล่านั้น กฎเหล่านี้จะถือว่าแอปใช้ Firebase Auth เพื่อให้request.auth.uid ตัวแปรเป็นรหัสของผู้ใช้

ขั้นตอนที่ 1: เริ่มต้นด้วยไฟล์กฎพื้นฐาน ซึ่งรวมถึงกฎที่ว่างเปล่าสำหรับเรื่องราว และความคิดเห็น:

service cloud.firestore {
   match /databases/{database}/documents {
     match /stories/{story} {
         // TODO: Story rules go here...

         match /comments/{comment} {
            // TODO: Comment rules go here...
         }
     }
   }
}

ขั้นตอนที่ 2: เพิ่มกฎ write อย่างง่ายที่ให้สิทธิ์เจ้าของควบคุม เรื่องราวได้อย่างสมบูรณ์ ฟังก์ชันที่กำหนดไว้จะช่วยกำหนดบทบาทของผู้ใช้และตรวจสอบว่าเอกสารใหม่ถูกต้องหรือไม่

service cloud.firestore {
   match /databases/{database}/documents {
     match /stories/{story} {
        function isSignedIn() {
          return request.auth != null;
        }

        function getRole(rsc) {
          // Read from the "roles" map in the resource (rsc).
          return rsc.data.roles[request.auth.uid];
        }

        function isOneOfRoles(rsc, array) {
          // Determine if the user is one of an array of roles
          return isSignedIn() && (getRole(rsc) in array);
        }

        function isValidNewStory() {
          // Valid if story does not exist and the new story has the correct owner.
          return resource == null && isOneOfRoles(request.resource, ['owner']);
        }

        // Owners can read, write, and delete stories
        allow write: if isValidNewStory() || isOneOfRoles(resource, ['owner']);

         match /comments/{comment} {
            // ...
         }
     }
   }
}

ขั้นตอนที่ 3: เขียนกฎที่อนุญาตให้ผู้ใช้ที่มีบทบาทใดก็ได้อ่านเรื่องราวและ ความคิดเห็น การใช้ฟังก์ชันที่กำหนดไว้ในขั้นตอนก่อนหน้าจะช่วยให้กฎกระชับและอ่านง่าย

service cloud.firestore {
   match /databases/{database}/documents {
     match /stories/{story} {
        function isSignedIn() {
          return request.auth != null;
        }

        function getRole(rsc) {
          return rsc.data.roles[request.auth.uid];
        }

        function isOneOfRoles(rsc, array) {
          return isSignedIn() && (getRole(rsc) in array);
        }

        function isValidNewStory() {
          return resource == null
            && request.resource.data.roles[request.auth.uid] == 'owner';
        }

        allow write: if isValidNewStory() || isOneOfRoles(resource, ['owner']);

        // Any role can read stories.
        allow read: if isOneOfRoles(resource, ['owner', 'writer', 'commenter', 'reader']);

        match /comments/{comment} {
          // Any role can read comments.
          allow read: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
                                      ['owner', 'writer', 'commenter', 'reader']);
        }
     }
   }
}

ขั้นตอนที่ 4: อนุญาตให้ผู้เขียน ผู้แสดงความคิดเห็น และเจ้าของเรื่องราวโพสต์ความคิดเห็น โปรดทราบว่ากฎนี้ยังตรวจสอบว่า owner ความคิดเห็นตรงกับ ผู้ใช้ที่ส่งคำขอ ซึ่งจะป้องกันไม่ให้ผู้ใช้เขียนความคิดเห็นทับความคิดเห็นของผู้ใช้รายอื่น

service cloud.firestore {
   match /databases/{database}/documents {
     match /stories/{story} {
        function isSignedIn() {
          return request.auth != null;
        }

        function getRole(rsc) {
          return rsc.data.roles[request.auth.uid];
        }

        function isOneOfRoles(rsc, array) {
          return isSignedIn() && (getRole(rsc) in array);
        }

        function isValidNewStory() {
          return resource == null
            && request.resource.data.roles[request.auth.uid] == 'owner';
        }

        allow write: if isValidNewStory() || isOneOfRoles(resource, ['owner'])
        allow read: if isOneOfRoles(resource, ['owner', 'writer', 'commenter', 'reader']);

        match /comments/{comment} {
          allow read: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
                                      ['owner', 'writer', 'commenter', 'reader']);

          // Owners, writers, and commenters can create comments. The
          // user id in the comment document must match the requesting
          // user's id.
          //
          // Note: we have to use get() here to retrieve the story
          // document so that we can check the user's role.
          allow create: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
                                        ['owner', 'writer', 'commenter'])
                        && request.resource.data.user == request.auth.uid;
        }
     }
   }
}

ขั้นตอนที่ 5: ให้สิทธิ์ผู้เขียนในการแก้ไขเนื้อหาเรื่องราว แต่ไม่อนุญาตให้แก้ไขบทบาทของเรื่องราว หรือเปลี่ยนพร็อพเพอร์ตี้อื่นๆ ของเอกสาร ซึ่งต้องแยกกฎ write ของเรื่องราวออกเป็นกฎแยกต่างหากสำหรับ create, update และ delete เนื่องจากผู้เขียนจะอัปเดตเรื่องราวได้เท่านั้น

service cloud.firestore {
   match /databases/{database}/documents {
     match /stories/{story} {
        function isSignedIn() {
          return request.auth != null;
        }

        function getRole(rsc) {
          return rsc.data.roles[request.auth.uid];
        }

        function isOneOfRoles(rsc, array) {
          return isSignedIn() && (getRole(rsc) in array);
        }

        function isValidNewStory() {
          return request.resource.data.roles[request.auth.uid] == 'owner';
        }

        function onlyContentChanged() {
          // Ensure that title and roles are unchanged and that no new
          // fields are added to the document.
          return request.resource.data.title == resource.data.title
            && request.resource.data.roles == resource.data.roles
            && request.resource.data.keys() == resource.data.keys();
        }

        // Split writing into creation, deletion, and updating. Only an
        // owner can create or delete a story but a writer can update
        // story content.
        allow create: if isValidNewStory();
        allow delete: if isOneOfRoles(resource, ['owner']);
        allow update: if isOneOfRoles(resource, ['owner'])
                      || (isOneOfRoles(resource, ['writer']) && onlyContentChanged());
        allow read: if isOneOfRoles(resource, ['owner', 'writer', 'commenter', 'reader']);

        match /comments/{comment} {
          allow read: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
                                      ['owner', 'writer', 'commenter', 'reader']);
          allow create: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
                                        ['owner', 'writer', 'commenter'])
                        && request.resource.data.user == request.auth.uid;
        }
     }
   }
}

ข้อจำกัด

โซลูชันที่แสดงด้านบนสาธิตการรักษาความปลอดภัยข้อมูลผู้ใช้โดยใช้กฎความปลอดภัย แต่คุณควรทราบข้อจำกัดต่อไปนี้

  • ความละเอียด: ในตัวอย่างด้านบน บทบาทหลายบทบาท (ผู้เขียนและเจ้าของ) มีสิทธิ์เขียนเอกสารเดียวกันแต่มีข้อจำกัดที่แตกต่างกัน ซึ่งอาจจัดการได้ยากหากเอกสารมีความซับซ้อนมากขึ้น และอาจแยกเอกสารเดียวออกเป็นเอกสารหลายฉบับที่แต่ละฉบับเป็นเจ้าของโดยบทบาทเดียวจะดีกว่า
  • กลุ่มขนาดใหญ่: หากคุณต้องแชร์กับกลุ่มขนาดใหญ่หรือซับซ้อนมาก ให้พิจารณาระบบที่จัดเก็บบทบาทไว้ในคอลเล็กชันของตัวเองแทนที่จะเป็นช่องในเอกสารเป้าหมาย