-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
83 lines (74 loc) · 3.88 KB
/
Copy pathfirestore.rules
File metadata and controls
83 lines (74 loc) · 3.88 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Default deny all access
match /{document=**} {
allow read, write: if false;
}
// Helper functions
function isSignedIn() {
return request.auth != null;
}
function isValidId(id) {
return id is string && id.size() > 0 && id.size() <= 128 && id.matches('^[a-zA-Z0-9_\\-]+$');
}
function incoming() {
return request.resource.data;
}
function existing() {
return resource.data;
}
function isValidUserProfile(data) {
return data.keys().hasAll(['userId', 'email', 'createdAt', 'updatedAt']) &&
data.userId is string && data.userId == request.auth.uid &&
data.email is string && data.email.size() <= 200 &&
(!('displayName' in data) || (data.displayName is string && data.displayName.size() <= 100)) &&
(!('photoURL' in data) || (data.photoURL is string && data.photoURL.size() <= 500)) &&
data.createdAt is timestamp &&
data.updatedAt is timestamp;
}
function isValidTaskItem(data) {
return data.keys().hasAll(['title', 'userId', 'status', 'category', 'createdAt', 'updatedAt']) &&
data.title is string && data.title.size() > 0 && data.title.size() <= 200 &&
data.userId is string && data.userId == request.auth.uid &&
data.status is string && (data.status == 'todo' || data.status == 'in_progress' || data.status == 'completed') &&
data.category is string && (data.category == 'work' || data.category == 'personal' || data.category == 'general' || data.category == 'urgent') &&
(!('description' in data) || (data.description is string && data.description.size() <= 1000)) &&
data.createdAt is timestamp &&
data.updatedAt is timestamp;
}
// User Profile matching
match /users/{userId} {
allow get: if isSignedIn() && isValidId(userId) && request.auth.uid == userId;
allow create: if isSignedIn() && isValidId(userId) && request.auth.uid == userId &&
isValidUserProfile(incoming()) &&
incoming().createdAt == request.time &&
incoming().updatedAt == request.time;
allow update: if isSignedIn() && isValidId(userId) && request.auth.uid == userId &&
isValidUserProfile(incoming()) &&
incoming().userId == existing().userId &&
incoming().createdAt == existing().createdAt &&
incoming().updatedAt == request.time &&
incoming().diff(existing()).affectedKeys().hasOnly(['displayName', 'photoURL', 'updatedAt']);
allow delete: if isSignedIn() && isValidId(userId) && request.auth.uid == userId;
}
// Task Item matching
match /tasks/{taskId} {
allow get: if isSignedIn() && isValidId(taskId) && existing().userId == request.auth.uid;
allow list: if isSignedIn() && resource.data.userId == request.auth.uid;
allow create: if isSignedIn() && isValidId(taskId) &&
isValidTaskItem(incoming()) &&
incoming().userId == request.auth.uid &&
incoming().createdAt == request.time &&
incoming().updatedAt == request.time;
allow update: if isSignedIn() && isValidId(taskId) &&
isValidTaskItem(incoming()) &&
existing().userId == request.auth.uid &&
incoming().userId == existing().userId &&
incoming().createdAt == existing().createdAt &&
incoming().updatedAt == request.time &&
incoming().diff(existing()).affectedKeys().hasOnly(['title', 'description', 'category', 'status', 'updatedAt']);
allow delete: if isSignedIn() && isValidId(taskId) && existing().userId == request.auth.uid;
}
}
}