Download firestore.rules from Lashtw/idle: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/spaces/Lashtw/idle/resolve/main/firestore.rules
- Command line
-
hf download hf://spaces/Lashtw/idle/firestore.rules
-
curl -L -o firestore.rules https://huggingface.co/spaces/Lashtw/idle/resolve/main/firestore.rules
3.12 kB
| rules_version = '2'; | |
| service cloud.firestore { | |
| match /databases/{database}/documents { | |
| // 檢查是否為已登入使用者(包含學生與教師帳號) | |
| function isSignedIn() { | |
| return request.auth != null; | |
| } | |
| // 檢查是否為教師管理員(以 admins/{uid} 名冊為唯一權威) | |
| function isAdmin() { | |
| return isSignedIn() && exists(/databases/$(database)/documents/admins/$(request.auth.uid)); | |
| } | |
| // 1. 班級資料 (classes) | |
| // - 讀取:所有已登入師生皆可讀取班級狀態與活動進度 | |
| // - 建立/刪除:僅限教師管理員 | |
| // - 更新:教師管理員可修改全部設定;學生僅允許更新共拓/活動欄位(捐獻、首破、Boss、樂透、大市集) | |
| match /classes/{code} { | |
| allow read: if isSignedIn(); | |
| allow create, delete: if isAdmin(); | |
| allow update: if isAdmin() || ( | |
| isSignedIn() && | |
| request.resource.data.diff(resource.data).affectedKeys().hasOnly([ | |
| 'donated', | |
| 'firstConquers', | |
| 'worldBoss', | |
| 'lotteryCycles', | |
| 'bazaarCycles', | |
| 'updatedAt' | |
| ]) | |
| ); | |
| } | |
| // 2. 學生個人資料與戰報 (students) | |
| // - 讀取:所有已登入學生皆可讀取同班同學戰況(用於「群英金榜」排行榜) | |
| // - 建立/刪除:僅限教師管理員 | |
| // - 更新:教師管理員可管理任意學生;學生僅能更新自己的 uid 存檔,嚴禁動他人資料 | |
| match /students/{uid} { | |
| allow read: if isSignedIn(); | |
| allow create, delete: if isAdmin(); | |
| allow update: if isAdmin() || (isSignedIn() && request.auth.uid == uid); | |
| // 學生專屬禮物領取 (gifts) | |
| // - 僅教師可發放 (create) 或刪除 (delete) 禮物 | |
| // - 學生僅可讀取自己的禮物,並將其標記為已領取 (claimed = true) | |
| match /gifts/{gid} { | |
| allow read: if isAdmin() || (isSignedIn() && request.auth.uid == uid); | |
| allow create, delete: if isAdmin(); | |
| allow update: if isAdmin() || ( | |
| isSignedIn() && | |
| request.auth.uid == uid && | |
| request.resource.data.diff(resource.data).affectedKeys().hasOnly(['claimed', 'claimedAt']) && | |
| request.resource.data.claimed == true | |
| ); | |
| } | |
| } | |
| // 3. 測驗答題紀錄 (attempts) | |
| // - 學生僅能建立屬於自己 uid 的答題紀錄,不可修改或刪除 | |
| match /attempts/{id} { | |
| allow read: if isSignedIn(); | |
| allow create: if isSignedIn() && request.resource.data.uid == request.auth.uid; | |
| allow update, delete: if isAdmin(); | |
| } | |
| // 4. 教師發送禮物日誌 (giftLog) | |
| // - 純教師後台專用,學生完全禁止讀寫 | |
| match /giftLog/{id} { | |
| allow read, write: if isAdmin(); | |
| } | |
| // 5. 教師管理員名冊 (admins) | |
| // - 登入時允許檢查自己的 uid 是否為管理員;寫入嚴格限定既有管理員,徹底封死學生提權 | |
| match /admins/{uid} { | |
| allow read: if isSignedIn() && (request.auth.uid == uid || isAdmin()); | |
| allow write: if isAdmin(); | |
| } | |
| } | |
| } | |