-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.rules
197 lines (158 loc) · 8.36 KB
/
firestore.rules
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow write;
}
match /flybis/public/{tagId} {
allow read;
}
match /flybis/public/{tagId}/{childId} {
allow read;
}
match /lives {
allow read;
}
match /lives/{userId} {
allow read;
allow write: if userExists(request.auth.uid) && request.auth.uid == userId;
}
match /bells/{userId}/bells {
allow read: if userExists(request.auth.uid) && request.auth.uid == userId;
}
match /bells/{userId}/bells/{bellId} {
allow read: if userExists(request.auth.uid) && request.auth.uid == userId;
allow create: if userExists(request.auth.uid) && request.auth.uid != userId && request.auth.uid == request.resource.data.sender && request.resource.data.receiver == userId && (request.resource.data.mode == 'like' || request.resource.data.mode == 'follow' || request.resource.data.mode == 'friend' || request.resource.data.mode == 'comment' || request.resource.data.mode == 'message') && (request.resource.data.type == 'default' || request.resource.data.type == 'text' || request.resource.data.type == 'image' || request.resource.data.type == 'video' || request.resource.data.type == 'gif');
allow delete: if userExists(request.auth.uid) && request.auth.uid != userId && request.auth.uid == resource.data.sender;
}
match /comments/{userId}/posts/{postId}/comments/{commentId} {
allow read;
allow write: if userExists(request.auth.uid) && request.auth.uid != null && request.auth.uid == request.resource.data.userId && isUuid(commentId);
}
match /followers/{userId}/followers/{followerId} {
allow read;
allow write: if userExists(request.auth.uid) && request.auth.uid != null && request.auth.uid == followerId;
}
match /followings/{userId}/followings/{followingId} {
allow read;
allow write: if userExists(request.auth.uid) && request.auth.uid == userId && userId != followingId;
}
match /friends/{userId}/requests/{requesterId} {
allow read;
allow create: if userExists(request.auth.uid) && request.auth.uid != userId && request.auth.uid == requesterId && userId != requesterId;
allow delete: if userExists(request.auth.uid) && (request.auth.uid == userId || request.auth.uid == requesterId) && userId != requesterId;
}
match /friends/{userId}/friends/{friendId} {
allow read;
allow create: if userExists(request.auth.uid) && (request.auth.uid == userId || (request.auth.uid == friendId && isFriendRequested(userId, friendId))) && userId != friendId;
allow delete: if userExists(request.auth.uid) && (request.auth.uid == userId || (request.auth.uid == friendId && isFriend(userId, friendId))) && userId != friendId;
}
match /chats/{chatId} {
allow read: if userExists(request.auth.uid) && (request.auth.uid == chatId.split('-')[0] || request.auth.uid == chatId.split('-')[1]);
allow create: if userExists(request.auth.uid) && (request.auth.uid == chatId.split('-')[0] || request.auth.uid == chatId.split('-')[1]) && (request.resource.data.chatId == chatId); //&& (!request.resource.data.keys().hasAny(['key']));
allow update: if userExists(request.auth.uid) && (request.auth.uid == chatId.split('-')[0] || request.auth.uid == chatId.split('-')[1]) && (request.resource.data.chatId == chatId) && (request.resource.data.chatKey == resource.data.chatKey);
allow list: if userExists(request.auth.uid) && onLimit(10);
}
match /chats/{chatId}/messages/{messageId} {
allow read: if userExists(request.auth.uid) && (request.auth.uid == chatId.split('-')[0] || request.auth.uid == chatId.split('-')[1]);
allow write: if userExists(request.auth.uid) && (request.auth.uid == chatId.split('-')[0] || request.auth.uid == chatId.split('-')[1]) && request.auth.uid == request.resource.data.userId && request.resource.data.messageContent.size() <= 5000 && isUuid(messageId);
allow list: if userExists(request.auth.uid) && onLimit(10);
}
match /posts/{userId} {
allow read;
allow write: if userExists(request.auth.uid) && request.auth.uid == userId;
allow list: if userExists(request.auth.uid) && onLimit(10);
}
match /posts/{userId}/posts/{postId} {
allow read;
allow create: if userExists(request.auth.uid) && request.auth.uid == userId && isUuid(postId);
allow update: if userExists(request.auth.uid) && request.auth.uid == userId && isUuid(postId);
allow list: if userExists(request.auth.uid) && onLimit(10);
}
match /posts/{userId}/posts/{postId}/likes/{senderId} {
allow read: if userExists(request.auth.uid) && request.auth.uid == senderId;
allow create: if userExists(request.auth.uid) && request.auth.uid == senderId && !dislikeExists(userId, postId, senderId);
allow delete: if userExists(request.auth.uid) && request.auth.uid == senderId;
}
match /posts/{userId}/posts/{postId}/dislikes/{senderId} {
allow read: if userExists(request.auth.uid) && request.auth.uid == senderId;
allow create: if userExists(request.auth.uid) && request.auth.uid == senderId && !likeExists(userId, postId, senderId);
allow delete: if userExists(request.auth.uid) && request.auth.uid == senderId;
}
match /status/{userId} {
allow read;
allow write: if request.auth.uid == userId;
}
match /timelines/{userId} {
allow read: if userExists(request.auth.uid) && request.auth.uid == userId;
}
match /timelines/{userId}/posts {
allow read: if userExists(request.auth.uid) && request.auth.uid == userId;
}
match /timelines/{userId}/posts/{postId} {
allow read: if userExists(request.auth.uid) && request.auth.uid == userId; //&& isUuid(postId);
}
match /usernames/{username} {
allow get;
allow create: if userExists(request.auth.uid) && request.auth.uid != null && request.resource.data.uid == request.auth.uid && isValidUsername(username);
}
match /users/{userId} {
allow read;
allow create: if request.auth.uid != null && request.resource.data.uid == request.auth.uid && isUsernameAvailable(request.resource.data.username);
allow update: if (request.auth.uid == userId && request.resource.data.uid == request.auth.uid && request.resource.data.username == resource.data.username) && userExists(request.auth.uid); //|| (request.auth.uid == userId && isUsernameAvailable(request.resource.data.username));
}
match /users/{userId}/tokens/{tokenId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId;
}
// Stripe
match /customers/{uid} {
allow read: if request.auth.uid == uid;
match /checkout_sessions/{id} {
allow read, write: if request.auth.uid == uid;
}
match /subscriptions/{id} {
allow read: if request.auth.uid == uid;
}
}
match /products/{id} {
allow read: if true;
match /prices/{id} {
allow read: if true;
}
match /tax_rates/{id} {
allow read: if true;
}
}
function isFriendRequested(userId, friendId) {
return exists(/databases/$(database)/documents/friends/$(friendId)/requests/$(userId));
}
function isFriend(userId, friendId) {
return exists(/databases/$(database)/documents/friends/$(userId)/friends/$(friendId));
}
function isValidUsername(username){
return username.matches('^([a-zA-Z0-9_.]){5,15}$');
}
function isUsernameAvailable(username){
return isValidUsername(username) && !exists(/databases/$(database)/documents/usernames/$(username));
}
function userExists(uid) {
return exists(/databases/$(database)/documents/users/$(uid));
}
function likeExists(userId, postId, senderId) {
return exists(/databases/$(database)/documents/posts/$(userId)/posts/$(postId)/likes/$(senderId));
}
function dislikeExists(userId, postId, senderId) {
return exists(/databases/$(database)/documents/posts/$(userId)/posts/$(postId)/dislikes/$(senderId));
}
function isUuid(uuid) {
return uuid.matches('^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$');
}
function canUpdate(seconds){
return request.time < resource.data.updatedAt + duration.value(seconds, 's');
}
function onLimit(limit) {
return request.query.limit <= limit;
}
}
}