แอปการทำงานร่วมกันหลายแอปอนุญาตให้ผู้ใช้อ่านและเขียนข้อมูลต่างๆ ตามชุดสิทธิ์ ตัวอย่างเช่น ในแอปแก้ไขเอกสาร ผู้ใช้อาจต้องการอนุญาตให้ผู้ใช้บางรายอ่านและเขียนเอกสารของตนเอง ขณะเดียวกันก็บล็อกการเข้าถึงที่ไม่ต้องการ
โซลูชัน: การควบคุมการเข้าถึงตามบทบาท
คุณสามารถใช้ประโยชน์จากโมเดลข้อมูลของ 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;
}
}
}
}
ข้อจำกัด
โซลูชันที่แสดงด้านบนแสดงให้เห็นถึงการรักษาความปลอดภัยของข้อมูลผู้ใช้โดยใช้กฎความปลอดภัย แต่คุณควรทราบข้อจำกัดต่อไปนี้
- ระดับการเข้าถึง: ในตัวอย่างด้านบน บทบาทหลายบทบาท (ผู้เขียนและเจ้าของ) มีสิทธิ์เขียนในเอกสารเดียวกันแต่มีข้อจำกัดที่แตกต่างกัน การจัดการเอกสารที่ซับซ้อนมากขึ้นอาจเป็นเรื่องยาก และอาจ เป็นการดีกว่าที่จะแบ่งเอกสารเดียวออกเป็นหลายเอกสาร โดยแต่ละเอกสาร จะมีบทบาทเดียวเป็นเจ้าของ
- กลุ่มขนาดใหญ่: หากต้องการแชร์กับกลุ่มขนาดใหญ่หรือซับซ้อนมาก ให้พิจารณาระบบที่จัดเก็บบทบาทไว้ในคอลเล็กชันของตนเอง แทนที่จะเป็นฟิลด์ในเอกสารเป้าหมาย