-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
54 lines (50 loc) · 1.93 KB
/
firestore.rules
File metadata and controls
54 lines (50 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
service cloud.firestore {
match /databases/{database}/documents {
//--> Custom Functions
function isAuthenticated() {
return request.auth != null;
}
function isAdmin() {
return isAuthenticated() && request.auth.token.isAdmin == true;
}
function isOwner(param) {
return isAuthenticated() && request.auth.uid == param;
}
// Documents /languages/*/sessions/* can be updated with { likes } data
function sessionLikeUpdate() {
return resource.__name__ == /databases/$(database)/documents/languages/$(languageId)/sessions/$(entityId)
&& request.resource.data.keys().join('') == 'likes';
}
//<-- Custom Functions
// Rules
// Restrict access to all non-/languages/** collections for Admin only.
match /{rootCollection}/{document=**} {
allow read, write: if isAdmin() || (rootCollection == 'languages');
}
// Core data
// 1) Admin can do Absolutely anything
// 2) All can edit 'likes' in /languages/{lang}/sessions/{id}
// Note: Match cannot be used on a document property. Therefore payload
// body has to be checked. Tried to use `hasOnly`, but didn't work ,
// see https://firebase.google.com/docs/reference/rules/rules.List.html#hasOnly
// hasOnly - bugged solution:
// request.resource.data.keys().hasOnly(['likes']) == true
match /languages/{languageId}/{entityCollection}/{entityId} {
allow read, write: if isAdmin() || sessionLikeUpdate()
}
// Anyone can read anything
match /languages/{langId=**} {
allow read;
}
// User data
// 1) owner can read and write
// 2) anyone can read `isPublic=true` agendas
match /userData/{userId} {
allow read, write: if isOwner(userId);
}
match /userPrograms/{userId} {
allow read: if isOwner(userId) || (resource != null && resource.data.isPublic == true);
allow write: if isOwner(userId);
}
}
}