กฎความปลอดภัยของ Firebase สำหรับ Cloud Storage ใช้เพื่อกำหนดว่าใครมีสิทธิ์อ่านและเขียนไฟล์ที่จัดเก็บไว้ใน Cloud Storage รวมถึงวิธีจัดโครงสร้างไฟล์และข้อมูลเมตาที่มีอยู่ในไฟล์เหล่านั้น กฎความปลอดภัยของ Cloud Storage ประกอบด้วยกฎที่พิจารณา request
และ resource
เพื่ออนุญาตหรือปฏิเสธการดำเนินการที่ต้องการ เช่น การอัปโหลดไฟล์หรือเรียกข้อมูลเมตาของไฟล์ เอกสารอ้างอิงเหล่านี้ครอบคลุมประเภทของกฎ คุณสมบัติของ request
และ resource
ประเภทข้อมูลที่ใช้โดยกฎความปลอดภัยของ Cloud Storage และข้อผิดพลาดเกิดขึ้นได้อย่างไร
กฎ
rule
คือนิพจน์ที่ได้รับการประเมินเพื่อพิจารณาว่า request
ได้รับอนุญาตให้ดำเนินการตามที่ต้องการหรือไม่
ประเภท
อนุญาต
allow
กฎประกอบด้วยวิธีการ เช่น read
หรือ write
เช่นเดียวกับเงื่อนไขทางเลือก เมื่อกฎถูกดำเนินการ เงื่อนไขจะถูกประเมิน และหากเงื่อนไขประเมินเป็น true
ก็จะอนุญาตให้ใช้วิธีที่ต้องการได้ มิฉะนั้นวิธีการนี้จะถูกปฏิเสธ กฎ allow
ที่ไม่มีเงื่อนไขจะอนุญาตวิธีการที่ต้องการเสมอ
// Always allow method allow <method>; // Allow method if condition is true allow <method>: if <condition>;
ปัจจุบัน allow
เป็นกฎประเภทเดียวที่รองรับ
วิธีการขอ
อ่าน
วิธี read
ครอบคลุมคำขอทั้งหมดที่มีการอ่านข้อมูลไฟล์หรือข้อมูลเมตา รวมถึงการดาวน์โหลดไฟล์และการอ่านข้อมูลเมตาของไฟล์
// Always allow reads allow read; // Allow reads if condition evaluates to true allow read: if <condition>;
เขียน
วิธี write
ครอบคลุมคำขอทั้งหมดที่มีการเขียนข้อมูลไฟล์หรือข้อมูลเมตา รวมถึงการอัปโหลดไฟล์ การลบไฟล์ และการอัปเดตข้อมูลเมตาของไฟล์
// Always allow writes allow write; // Allow writes if condition evaluates to true allow write: if <condition>;
จับคู่
กฎจะถูกดำเนินการเมื่อ request
ของผู้ใช้ (เช่น การอัพโหลดไฟล์หรือดาวน์โหลดไฟล์) ตรงกับเส้นทางของไฟล์ที่ครอบคลุมโดยกฎ match
ประกอบด้วยเส้นทางและเนื้อหา ซึ่งต้องมีกฎ allow
อย่างน้อยหนึ่งกฎ หากไม่มีเส้นทางที่ตรงกัน คำขอจะถูกปฏิเสธ
คุณสามารถ match
เส้นทางที่มีชื่อเต็ม หรือคุณสามารถแทรกไวด์การ์ดเพื่อจับคู่เส้นทางทั้งหมดที่ตรงกับรูปแบบที่ต้องการได้
ส่วนเส้นทาง
single_segment
คุณสามารถใช้กลุ่มเส้นทางเดียวเพื่อสร้างกฎที่ตรงกับไฟล์ที่จัดเก็บไว้ใน Cloud Storage
// Allow read at "path" if condition evaluates to true match /path { allow read: if <condition>; }
อนุญาตให้ใช้หลายส่วนของเส้นทางและเส้นทางที่ซ้อนกันได้:
// Allow read at "path/to/object" if condition evaluates to true match /path { match /to { match /object { allow read: if <condition>; } } }
{single_segment_wildcard}
หากคุณต้องการใช้กฎกับไฟล์หลายไฟล์ในเส้นทางเดียวกัน คุณสามารถใช้ส่วนเส้นทางไวด์การ์ดเพื่อจับคู่ไฟล์ทั้งหมดในเส้นทางที่แน่นอนได้ ตัวแปรไวด์การ์ดถูกประกาศในเส้นทางโดยการล้อมตัวแปรด้วยเครื่องหมายปีกกา: {variable}
ตัวแปรนี้สามารถเข้าถึงได้ภายในคำสั่งการจับคู่ใน string
// Allow read at any path "/*", if condition evaluates to true match /{single_path} { // Matches "path", "to", or "object" but not "path/to/object" allow read: if <condition>; }
ส่วนเส้นทางหลายส่วนและเส้นทางที่ซ้อนกันอาจมีไวด์การ์ด:
// Allow read at any path "/path/*/newPath/*", if condition evaluates to true match /path/{first_wildcard} { match /newPath/{second_wildcard} { // Matches "path/to/newPath/newObject" or "path/from/newPath/oldObject" allow read: if <condition>; } }
{multi_segment_wildcard=**}
หากคุณต้องการจับคู่ส่วนของเส้นทางจำนวนเท่าใดก็ได้ที่หรือใต้เส้นทาง คุณสามารถใช้ไวลด์การ์ดแบบหลายกลุ่ม ซึ่งจะจับคู่คำขอทั้งหมดไปยังและด้านล่างของตำแหน่ง สิ่งนี้มีประโยชน์สำหรับการจัดเตรียมพื้นที่เก็บข้อมูลรูปแบบอิสระให้กับผู้ใช้ หรือการสร้างกฎที่ตรงกับส่วนของเส้นทางที่แตกต่างกันจำนวนมาก (เช่น การสร้างชุดไฟล์ที่สาธารณะสามารถอ่านได้ หรือต้องมีการรับรองความถูกต้องสำหรับการเขียนทั้งหมด)
เส้นทางไวลด์การ์ดแบบหลายส่วนได้รับการประกาศในลักษณะเดียวกับไวลด์การ์ดแบบส่วนเดียว โดยมีการเพิ่ม =**
ที่ส่วนท้ายของตัวแปร: {variable=**}
ตัวแปรไวด์การ์ดแบบหลายส่วนจะพร้อมใช้งานภายในคำสั่งการจับคู่เป็นออบเจ็ก path
// Allow read at any path "/**", if condition evaluates to true match /{multi_path=**} { // Matches anything at or below this, from "path", "path/to", "path/to/object", ... allow read: if <condition>; }
ขอ
ตัวแปร request
ถูกจัดเตรียมไว้ภายใต้เงื่อนไขเพื่อแสดงคำขอที่ถูกสร้างขึ้นในเส้นทางนั้น ตัวแปร request
มีคุณสมบัติจำนวนหนึ่งซึ่งสามารถใช้เพื่อตัดสินใจว่าจะอนุญาตคำขอที่เข้ามาหรือไม่
คุณสมบัติ
auth
เมื่อผู้ใช้ที่ได้รับการตรวจสอบสิทธิ์ดำเนินการร้องขอกับ Cloud Storage ตัวแปร auth
จะถูกเติมด้วย uid
ของผู้ใช้ ( request.auth.uid
) รวมถึงการอ้างสิทธิ์ของ Firebase Authentication JWT ( request.auth.token
)
request.auth.token
มีคีย์บางส่วนหรือทั้งหมดต่อไปนี้:
สนาม | คำอธิบาย |
---|---|
email | ที่อยู่อีเมลที่เชื่อมโยงกับบัญชี หากมี |
email_verified | true หากผู้ใช้ยืนยันว่าตนสามารถเข้าถึงที่อยู่ email ได้ ผู้ให้บริการบางรายจะตรวจสอบที่อยู่อีเมลของตนโดยอัตโนมัติ |
phone_number | หมายเลขโทรศัพท์ที่เชื่อมโยงกับบัญชี หากมี |
name | ชื่อที่แสดงของผู้ใช้ หากตั้งค่าไว้ |
sub | UID ของ Firebase ของผู้ใช้ นี่เป็นเรื่องพิเศษภายในโครงการ |
firebase.identities | พจนานุกรมข้อมูลระบุตัวตนทั้งหมดที่เกี่ยวข้องกับบัญชีผู้ใช้นี้ ปุ่มต่างๆ ของพจนานุกรมอาจเป็นปุ่มใดๆ ต่อไปนี้: email , phone , google.com , facebook.com , github.com , twitter.com ค่าของพจนานุกรมคืออาร์เรย์ของตัวระบุที่ไม่ซ้ำกันสำหรับผู้ให้บริการข้อมูลประจำตัวแต่ละรายที่เชื่อมโยงกับบัญชี ตัวอย่างเช่น auth.token.firebase.identities["google.com"][0] มี ID ผู้ใช้ Google แรกที่เชื่อมโยงกับบัญชี |
firebase.sign_in_provider | ผู้ให้บริการลงชื่อเข้าใช้ที่ใช้ในการรับโทเค็นนี้ สามารถเป็นหนึ่งในสตริงต่อไปนี้: custom , password , phone , anonymous , google.com , facebook.com , github.com , twitter.com |
firebase.tenant | TenantId ที่เชื่อมโยงกับบัญชี หากมี เช่น tenant2-m6tyz |
หากใช้การตรวจสอบสิทธิ์แบบกำหนดเอง request.auth.token
ยังมีการอ้างสิทธิ์แบบกำหนดเองที่ระบุโดยนักพัฒนาด้วย
เมื่อผู้ใช้ที่ไม่ได้รับการรับรองความถูกต้องดำเนินการร้องขอ request.auth
จะเป็น null
// Allow requests from authenticated users allow read, write: if request.auth != null;
path
ตัวแปร path
ประกอบด้วยเส้นทางที่ request
กำลังดำเนินการอยู่
// Allow a request if the first path segment equals "images" allow read, write: if request.path[0] == 'images';
resource
ตัวแปร resource
ประกอบด้วยข้อมูลเมตาของไฟล์ที่กำลังอัปโหลดหรือข้อมูลเมตาที่อัปเดตสำหรับไฟล์ที่มีอยู่ สิ่งนี้เกี่ยวข้องกับตัวแปร resource
ซึ่งมีข้อมูลเมตาของไฟล์ปัจจุบันในเส้นทางที่ร้องขอ ซึ่งตรงข้ามกับข้อมูลเมตาใหม่
// Allow a request if the new value is smaller than 5MB allow read, write: if request.resource.size < 5 * 1024 * 1024;
request.resource
มีคุณสมบัติต่อไปนี้จาก resource
:
คุณสมบัติ |
---|
name |
bucket |
metadata |
size |
contentType |
time
ตัวแปร time
มีการประทับเวลาที่แสดงถึงเวลาเซิร์ฟเวอร์ปัจจุบันที่มีการประเมินคำขอ คุณสามารถใช้สิ่งนี้เพื่อให้สิทธิ์ในการเข้าถึงไฟล์ตามเวลา เช่น อนุญาตให้อัปโหลดไฟล์จนถึงวันที่กำหนดเท่านั้น หรืออนุญาตให้อ่านไฟล์ได้ภายในหนึ่งชั่วโมงหลังจากอัปโหลดเท่านั้น
// Allow a read if the file was created less than one hour ago allow read: if request.time < resource.timeCreated + duration.value(1, 'h');
มีฟังก์ชันมากมายสำหรับเขียนกฎโดยใช้ การประทับเวลา และ ระยะเวลา
ทรัพยากร
ตัวแปร resource
ประกอบด้วยข้อมูลเมตาของไฟล์สำหรับไฟล์ใน Cloud Storage เช่น ชื่อไฟล์ ขนาด เวลาในการสร้าง และข้อมูลเมตาที่กำหนดเอง
คุณสมบัติ
name
สตริงที่มีชื่อเต็มของไฟล์ รวมถึงเส้นทางไปยังไฟล์
// Allow reads if the resource name is "path/to/object" allow read: if resource.name == 'path/to/object'
bucket
สตริงที่มีที่เก็บข้อมูล Google Cloud Storage ที่ไฟล์นี้จัดเก็บอยู่
// Allow reads of all resources in your bucket allow read: if resource.bucket == '<your-cloud-storage-bucket>'
generation
int ที่มี การสร้างออบเจ็กต์ Google Cloud Storage ของไฟล์ ใช้สำหรับการกำหนดเวอร์ชันออบเจ็กต์
// Allow reads if the resource matches a known object version allow read: if resource.generation == <known-generation>
metageneration
int ที่มี การสร้างเมตาอ็อบเจ็กต์ Google Cloud Storage ของไฟล์ ใช้สำหรับการกำหนดเวอร์ชันออบเจ็กต์
// Allow reads if the resource matches a known object metadata version allow read: if resource.metageneration == <known-generation>
size
int มีขนาดไฟล์เป็นไบต์
// Allow reads if the resource is less than 10 MB allow read: if resource.size < 10 * 1024 * 1024;
timeCreated
การประทับเวลาที่แสดงเมื่อไฟล์ถูกสร้างขึ้น
// Allow reads if the resource was created less than an hour ago allow read: if resource.timeCreated < request.time + duration.value(60, "m")
updated
การประทับเวลาที่แสดงเวลาที่ไฟล์ได้รับการอัปเดตครั้งล่าสุด
// Allow reads if the resource was updated less than an hour ago allow read: if resource.updated < request.time + duration.value(60, "m")
md5Hash
สตริงที่มี แฮช MD5 ของไฟล์
// Allow writes if the hash of the uploaded file is the same as the existing file allow write: if request.resource.md5Hash == resource.md5Hash;
crc32c
สตริงที่มี แฮช crc32c ของไฟล์
// Allow writes if the hash of the uploaded file is the same as the existing file allow write: if request.resource.crc32c == resource.crc32c;
etag
สตริงที่มี etag ของไฟล์
// Allow writes if the etag matches a known object etag allow write: if resource.etag == <known-generation>
contentDisposition
สตริงที่มีการจัดการเนื้อหาของไฟล์
// Allow reads if the content disposition matches a certain value allow read: if resource.contentDisposition == 'inlined';
contentEncoding
สตริงที่มีการเข้ารหัสเนื้อหาของไฟล์
// Allow reads if the content is encoded with gzip allow read: if resource.contentEncoding == 'gzip';
contentLanguage
สตริงที่มีภาษาเนื้อหาของไฟล์
// Allow reads if the content language is Japanese allow read: if resource.contentLanguage == 'ja';
contentType
สตริงที่มีประเภทเนื้อหาของไฟล์
// Allow reads if the content type is PNG. allow read: if resource.contentType == 'image/png';
metadata
Map<String, String>
ที่มีฟิลด์ข้อมูลเมตาที่นักพัฒนาจัดเตรียมไว้เพิ่มเติม
// Allow reads if a certain metadata field matches a desired value allow read: if resource.metadata.customProperty == 'customValue';
firestore.get และ firestore.exists
ฟังก์ชัน firestore.get()
และ firestore.exists()
ช่วยให้คุณเข้าถึงเอกสารใน Cloud Firestore เพื่อประเมินเกณฑ์การให้สิทธิ์ที่ซับซ้อน
ฟังก์ชัน firestore.get()
และ firestore.exists()
คาดหวังเส้นทางเอกสารที่ระบุอย่างครบถ้วน เมื่อใช้ตัวแปรเพื่อสร้างเส้นทางสำหรับ firestore.get()
และ firestore.exists()
คุณจะต้องหลีกเลี่ยงตัวแปรอย่างชัดเจนโดยใช้ไวยากรณ์ $(variable)
firestore.get
รับเนื้อหาของเอกสาร Cloud Firestore
service firebase.storage { match /b/{bucket}/o { match /users/{club}/files/{fileId} { allow read: if club in firestore.get(/databases/(default)/documents/users/$(request.auth.uid)).data.memberships } } }
firestore.มีอยู่
ตรวจสอบว่ามีเอกสาร Cloud Firestore อยู่หรือไม่
service firebase.storage { match /b/{bucket}/o { match /users/{userId}/photos/{fileId} { allow read: if firestore.exists(/databases/(default)/documents/users/$(userId)/friends/$(request.auth.uid)) } } }
บริการ
service
นี้เป็นการประกาศครั้งแรกในไฟล์ Cloud Storage Security Rules และระบุว่าบริการใดที่จะนำไปใช้กับกฎเหล่านี้
ชื่อ
name
ชื่อของกฎการบริการจะถูกนำไปใช้กับ ค่าปัจจุบันเพียงค่าเดียวคือ firebase.storage
// Specify the service name service firebase.storage { match /b/{bucket}/o { ... } }
ประเภทข้อมูล
ภาษากฎอนุญาตให้คุณตรวจสอบประเภทโดยใช้ตัวดำเนินการ is
// For example
a is null
a is string
null
ชนิดข้อมูล null
แสดงถึงค่าที่ไม่มีอยู่
allow read: if request.auth != null;
bool
ประเภท bool
แสดงถึงค่าบูลีน true
หรือ false
allow read: if true; // always succeeds allow write: if false; // always fails
การเปรียบเทียบ
ค่าบูลีนสามารถเปรียบเทียบได้โดยใช้ตัวดำเนินการ ==
!=
การดำเนินการบูลีน
การดำเนินการ | การแสดงออก |
---|---|
AND | x && y |
OR | x || y |
NOT | !x |
การดำเนินการลัดวงจรและสามารถส่งคืน true
, false
หรือ Error ได้
allow read: if true || false; // always succeeds, short circuits at true allow write: if false && true; // always fails, short circuits at false
int
และ float
ประเภท int
และ float
แสดงถึงตัวเลข Ints คือ: 0
, 1
, -2
ฯลฯ ในขณะที่ float คือ: 1.0
, -2.0
, 3.33
เป็นต้น
Ints คือค่าที่เซ็นชื่อแบบ 64 บิต และลอยเป็นค่าที่สอดคล้องกับมาตรฐาน IEEE 754 64 บิต ค่าประเภท int
จะถูกบังคับให้ float
เมื่อใช้ในการเปรียบเทียบและการดำเนินการทางคณิตศาสตร์ที่มีค่า float
การเปรียบเทียบ
Ints และ floats สามารถเปรียบเทียบและเรียงลำดับได้โดยใช้ตัวดำเนินการ ==
, !=
, >
, <
, >=
และ <=
เลขคณิต
Ints และ floats สามารถบวก ลบ คูณ หาร โมดูโลด และลบล้างได้:
การดำเนินการ | การแสดงออก |
---|---|
ส่วนที่เพิ่มเข้าไป | x + y |
การลบ | x - y |
การคูณ | x * y |
แผนก | x / y |
โมดูโล่ | x % y |
การปฏิเสธ | -x |
ฟังก์ชันทางคณิตศาสตร์
กฎความปลอดภัยของ Firebase สำหรับ Cloud Storage ยังมีฟังก์ชันตัวช่วยทางคณิตศาสตร์จำนวนหนึ่งเพื่อลดความซับซ้อนของนิพจน์:
การทำงาน | คำอธิบาย |
---|---|
math.ceil(x) | เพดานของค่าตัวเลข |
math.floor(x) | ชั้นของค่าตัวเลข |
math.round(x) | ปัดเศษค่าอินพุตให้เป็น int ที่ใกล้ที่สุด |
math.abs(x) | ค่าสัมบูรณ์ของอินพุต |
math.isInfinite(x) | ทดสอบว่าค่าเป็น ±∞ ส่งคืนค่า bool |
math.isNaN(x) | ทดสอบว่าค่าไม่ใช่ตัวเลข NaN หรือไม่ ให้คืนค่า bool |
string
การเปรียบเทียบ
สตริงสามารถเปรียบเทียบและเรียงลำดับโดยใช้พจนานุกรมโดยใช้ตัวดำเนินการ ==
, !=
, >
, <
, >=
และ <=
การต่อข้อมูล
สามารถต่อสตริงเข้าด้วยกันได้โดยใช้ตัวดำเนินการ +
// Concatenate a file name and extension 'file' + '.txt'
ดัชนีและช่วง
ตัวดำเนินการ index
string[]
ส่งคืนสตริงที่มีอักขระที่ดัชนีที่ระบุในสตริง
// Allow reads of files that begin with 'a' match /{fileName} { allow read: if fileName[0] == 'a'; }
ตัวดำเนินการ range
string[i:j]
ส่งคืนสตริงที่มีอักขระระหว่างดัชนีที่ระบุ ตั้งแต่ i
(รวม) จนถึง j
(ไม่รวม) หากไม่ได้ระบุ i
หรือ j
ค่าดีฟอลต์จะเป็น 0 และขนาดของสตริง ตามลำดับ แต่อย่างน้อยต้องระบุ i
หรือ j
เพื่อให้ช่วงถูกต้อง
// Allow reads of files that begin with 'abcdef' match /{fileName} { allow read: if fileName[0:6] == 'abcdef'; }
ตัวดำเนินการ index
และ range
จะทำให้เกิดข้อผิดพลาดหากดัชนีที่ให้มาเกินขอบเขตสตริง
size
ส่งกลับจำนวนอักขระในสตริง
// Allow files with names less than 10 characters match /{fileName} { allow write: if fileName.size() < 10; }
matches
ดำเนินการจับคู่นิพจน์ทั่วไป โดยจะส่งคืน true
หากสตริงตรงกับนิพจน์ทั่วไปที่กำหนด ใช้ ไวยากรณ์ของ Google RE2
// Allow writes to files which end in ".txt" match /{fileName} { allow write: if fileName.matches('.*\\.txt') }
split
แยกสตริงตามนิพจน์ทั่วไปที่ให้ไว้ และส่งคืน list
สตริง ใช้ ไวยากรณ์ของ Google RE2
// Allow files named "file.*" to be uploaded match /{fileName} { allow write: if fileName.split('.*\\..*')[0] == 'file' }
path
เส้นทางเป็นชื่อเหมือนไดเร็กทอรีที่มีการจับคู่รูปแบบที่เป็นทางเลือก การมีอยู่ของเครื่องหมายทับ /
หมายถึงจุดเริ่มต้นของส่วนของเส้นทาง
path
แปลงอาร์กิวเมนต์ string
เป็น path
// Allow reads on a specific file path match /{allFiles=**} { allow read: if allFiles == path('/path/to/file'); }
timestamp
การประทับเวลาอยู่ในรูปแบบ UTC โดยค่าที่เป็นไปได้เริ่มต้นที่ 0001-01-01T00.00.00Z และสิ้นสุดที่ 9999-12-31T23.59.59Z
การเปรียบเทียบ
การประทับเวลาสามารถเปรียบเทียบและเรียงลำดับได้โดยใช้ตัวดำเนินการ ==
, !=
, >
, <
, >=
และ <=
เลขคณิต
การประทับเวลารองรับการบวกและการลบระหว่างการประทับเวลาและระยะเวลาดังต่อไปนี้:
การแสดงออก | ผลลัพธ์ |
---|---|
timestamp + duration | timestamp |
duration + timestamp | timestamp |
timestamp - duration | timestamp |
timestamp - timestamp | duration |
duration + duration | duration |
duration - duration | duration |
date
ค่า timestamp
ที่มี year
month
และ day
เท่านั้น
// Allow reads on the same day that the resource was created. allow read: if request.time.date() == resource.timeCreated.date()
year
ค่าปีเป็น int ตั้งแต่ 1 ถึง 9999
// Allow reads on all requests made before 2017 allow read: if request.time.year() < 2017
month
ค่าเดือนเป็น int ตั้งแต่ 1 ถึง 12
// Allow reads on all requests made during the month of January allow read: if request.time.month() == 1;
day
วันปัจจุบันของเดือนเป็น int ตั้งแต่ 1 ถึง 31
// Allow reads on all requests made during the first day of each month allow read: if request.time.day() == 1;
time
ค่า duration
ที่ประกอบด้วยเวลาปัจจุบัน
// Allow reads on all requests made before 12PM allow read: if request.time.time() < duration.time(12, 0, 0, 0);
hours
ค่าชั่วโมงเป็น int ตั้งแต่ 0 ถึง 23
// Allow reads on all requests made before 12PM allow read: if request.time.hours() < 12;
minutes
ค่านาทีเป็น int ตั้งแต่ 0 ถึง 59
// Allow reads during even minutes of every hour allow read: if request.time.minutes() % 2 == 0;
seconds
ค่าวินาทีเป็น int ตั้งแต่ 0 ถึง 59
// Allow reads during the second half of each minute allow read: if request.time.seconds() > 29;
nanos
เศษส่วนวินาทีในนาโนเป็น int
// Allow reads during the first 0.1 seconds of each second allow read: if request.time.nanos() < 100000000;
dayOfWeek
วันในสัปดาห์ ตั้งแต่วันที่ 1 (วันจันทร์) ถึง 7 (วันอาทิตย์)
// Allow reads on weekdays (Monday to Friday) allow read: if request.time.dayOfWeek() < 6;
dayOfYear
วันของปีปัจจุบัน ตั้งแต่วันที่ 1 ถึง 366
// Allow reads every fourth day allow read: if request.time.dayOfYear() % 4 == 0;
toMillis
ส่งกลับจำนวนมิลลิวินาทีปัจจุบันนับตั้งแต่ยุค Unix
// Allow reads if the request is made before a specified time allow read: if request.time.toMillis() < <milliseconds>;
duration
ค่าระยะเวลาจะแสดงเป็นวินาทีบวกเศษส่วนวินาทีในหน่วยนาโนวินาที
การเปรียบเทียบ
สามารถเปรียบเทียบและเรียงลำดับระยะเวลาได้โดยใช้ตัวดำเนินการ ==
, !=
, >
, <
, >=
และ <=
เลขคณิต
ระยะเวลารองรับการบวกและการลบระหว่างการประทับเวลาและระยะเวลาดังนี้:
การแสดงออก | ผลลัพธ์ |
---|---|
timestamp + duration | timestamp |
duration + timestamp | timestamp |
timestamp - duration | timestamp |
timestamp - timestamp | duration |
duration + duration | duration |
duration - duration | duration |
seconds
จำนวนวินาทีในช่วงเวลาปัจจุบัน ต้องอยู่ระหว่าง -315,576,000,000 ถึง +315,576,000,000 รวม
nanos
จำนวนเศษส่วนวินาที (เป็นนาโนวินาที) ของระยะเวลาปัจจุบัน ต้องอยู่ระหว่าง -999,999,999 ถึง +999,999,999 สำหรับวินาทีที่ไม่ใช่ศูนย์และนาโนวินาทีที่ไม่ใช่ศูนย์ สัญญาณของทั้งสองจะต้องสอดคล้องกัน
duration.value
สามารถสร้างระยะเวลาได้โดยใช้ฟังก์ชัน duration.value(int magnitude, string units)
ซึ่งสร้างระยะเวลาจากขนาดและหน่วยที่กำหนด
// All of these durations represent one hour: duration.value(1, "h") duration.value(60, "m") duration.value(3600, "s")
unit
ที่เป็นไปได้คือ:
ระยะเวลา | unit |
---|---|
สัปดาห์ | w |
วัน | d |
ชั่วโมง | h |
นาที | m |
วินาที | s |
มิลลิวินาที | ms |
นาโนวินาที | ns |
duration.time
สามารถสร้างระยะเวลาได้โดยใช้ฟังก์ชัน duration.time(int hours, int minutes, int seconds, int nanoseconds)
ซึ่งสร้างระยะเวลาของชั่วโมง นาที วินาที และนาโนวินาทีที่กำหนด
// Create a four hour, three minute, two second, one nanosecond duration duration.time(4, 3, 2, 1)
list
รายการประกอบด้วยอาร์เรย์ของค่าที่เรียงลำดับกัน ซึ่งสามารถเป็นประเภท: null
, bool
, int
, float
, string
, path
, list
, map
, timestamp
หรือ duration
ให้ x
และ y
เป็น list
ประเภท และ i
และ j
เป็นประเภท int
การสร้าง
หากต้องการสร้างรายการ ให้เพิ่มค่าระหว่างวงเล็บ:
// Create a list of strings ['apples', 'grapes', 'bananas', 'cheese', 'goats']
การเปรียบเทียบ
รายการสามารถเปรียบเทียบได้โดยใช้ตัวดำเนินการ ==
!=
ความเท่าเทียมกันของสองรายการต้องการให้ค่าทั้งหมดเท่ากัน
ดัชนีและช่วง
ตัวดำเนินการ index
list[]
ส่งคืนรายการที่ดัชนีที่ระบุในรายการ
// Allow reads of all files that begin with 'a' match /{fileName} { allow read: if fileName[0] == 'a'; }
ตัวดำเนินการ range
list[i:j]
ส่งคืนรายการทั้งหมดในรายการระหว่างดัชนีที่ระบุ ตั้งแต่ i
(รวม) จนถึง j
(ไม่รวม) หากไม่ได้ระบุ i
หรือ j
ค่าดีฟอลต์จะเป็น 0 และขนาดของรายการ ตามลำดับ แต่อย่างน้อยต้องระบุ i
หรือ j
เพื่อให้ช่วงถูกต้อง
// Allow reads of all files that begin with 'abcdef' match /{fileName} { allow read: if fileName[0:6] == 'abcdef'; }
in
คืนค่า true
หากค่าที่ต้องการมีอยู่ในรายการ หรือคืนค่า false
หากไม่มี
// Allow read if a filename has the string 'txt' in it match /{fileName} { allow read: if 'txt' in fileName.split('\\.'); }
join
รวมรายการสตริงเป็นสตริงเดียว โดยคั่นด้วยสตริงที่กำหนด
// Allow reads if the joined array is 'file.txt' allow read: if ['file', 'txt'].join('.') == 'file.txt';
size
จำนวนรายการในรายการ
// Allow read if there are three items in our list allow read: if ['foo', 'bar', 'baz'].size() == 3;
hasAll
คืนค่า true
หากมีค่าทั้งหมดอยู่ในรายการ
// Allow read if one list has all items in the other list allow read: if ['file', 'txt'].hasAll(['file', 'txt']);
map
แผนที่ประกอบด้วยคู่คีย์/ค่า โดยที่คีย์คือสตริง และค่าอาจเป็นค่าใดก็ได้: null
, bool
, int
, float
, string
, path
, list
, map
, timestamp
หรือ duration
การสร้าง
หากต้องการสร้างแผนที่ ให้เพิ่มคู่คีย์/ค่าระหว่างเครื่องหมายปีกกา:
// Create a map of strings to strings { 'mercury': 'mars', 'rain': 'cloud', 'cats': 'dogs', }
การเปรียบเทียบ
สามารถเปรียบเทียบแผนที่ได้โดยใช้ตัวดำเนินการ ==
!=
ความเท่าเทียมกันของสองแผนที่จำเป็นต้องมีคีย์ทั้งหมดอยู่ในทั้งสองแผนที่และค่าทั้งหมดเท่ากัน
ดัชนี
เข้าถึงค่าในแผนที่ได้โดยใช้วงเล็บเหลี่ยมหรือเครื่องหมายจุด:
// Access custom metadata properties allow read: if resource.metadata.property == 'property' allow write: if resource.metadata['otherProperty'] == 'otherProperty'
หากไม่มีคีย์ ระบบจะส่งคืน error
in
คืนค่า true
หากมีคีย์ที่ต้องการอยู่ในแผนที่ หรือ false
หากไม่มี
// Allow reads if a property is present in the custom metadata allow read: if property in resource.metadata;
size
จำนวนกุญแจในแผนที่
// Allow reads if there's exactly one custom metadata key allow read: if resource.metadata.size() == 1;
keys
รายการกุญแจทั้งหมดในแผนที่
// Allow reads if the first metadata key is 'myKey' allow read: if resource.metadata.keys()[0] == 'myKey';
values
รายการค่าทั้งหมดในแผนที่ตามลำดับคีย์
// Allow reads if the first metadata value is 'myValue' allow read: if resource.metadata.values()[0] == 'myValue';
ข้อผิดพลาด
การประเมินข้อผิดพลาด
กฎความปลอดภัยของ Firebase สำหรับ Cloud Storage จะประเมินต่อไปเมื่อพบข้อผิดพลาด สิ่งนี้มีประโยชน์เนื่องจากมีเงื่อนไข &&
และ ||
นิพจน์อาจดูดซับข้อผิดพลาดหากเงื่อนไขจะลัดวงจรเป็น false
หรือ true
ตามลำดับ ตัวอย่างเช่น:
การแสดงออก | ผลลัพธ์ |
---|---|
error && true | error |
error && false | false |
error || true | true |
error || false | error |
ตำแหน่งทั่วไปที่ทำให้เกิดข้อผิดพลาด ได้แก่ การหารด้วยศูนย์ การเข้าถึงค่าในรายการหรือแผนที่ที่ไม่มีอยู่ และการส่งค่าประเภทที่ไม่ถูกต้องไปยังฟังก์ชัน
// Error if resource.size is zero allow read: if 1000000 / resource.size; // Error, key doesn't exist allow read: if resource.metadata.nonExistentKey == 'value'; // Error, no unit 'y' exists allow read: if request.time < resource.timeCreated + duration.value(1, 'y');