-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirestore.rules
More file actions
182 lines (144 loc) · 6.04 KB
/
Copy pathfirestore.rules
File metadata and controls
182 lines (144 loc) · 6.04 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
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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// --- Helper Functions ---
function isAuthenticated() {
return request.auth.uid != null;
}
function isOwner(userId) {
return request.auth.uid == userId;
}
function isValidStatus(status) {
return status in ['active', 'done', 'deleted', 'archived', 'inactive'];
}
function hasRequiredCreateFields() {
let data = request.resource.data;
return data.status == 'active'
&& data.createdAt == request.time
&& data.updatedAt == request.time
&& data.createdBy == request.auth.uid
&& data.updatedBy == request.auth.uid;
}
function hasRequiredUpdateFields() {
let data = request.resource.data;
return data.updatedAt == request.time
&& data.updatedBy == request.auth.uid
&& isValidStatus(data.status);
}
// Check if user is a member of the board (assumes composite doc ID: boardId_userId)
function isBoardMember(boardId) {
return exists(/databases/$(database)/documents/board_memberships/$(boardId + '_' + request.auth.uid));
}
// Get membership role
function getBoardRole(boardId) {
let membership = get(/databases/$(database)/documents/board_memberships/$(boardId + '_' + request.auth.uid));
return membership.data.role;
}
// Check if user is the owner of the referenced board
function isBoardOwner(boardId) {
let board = get(/databases/$(database)/documents/boards_current/$(boardId));
return board != null && (board.data.ownerId == request.auth.uid || board.data.userId == request.auth.uid);
}
// --- Collection Rules ---
match /activities/{activityId} {
allow read: if isAuthenticated() && (
isBoardMember(resource.data.boardId) ||
isBoardOwner(resource.data.boardId)
);
allow create: if isAuthenticated();
allow update, delete: if false;
}
match /board_memberships/{membershipId} {
// Allow read if authenticated (temporary for debug)
allow read: if isAuthenticated();
// Allow create if board owner OR if board doesn't exist yet (creating new board)
allow create: if isAuthenticated() && (
isBoardOwner(request.resource.data.boardId) ||
(
!exists(/databases/$(database)/documents/boards_current/$(request.resource.data.boardId)) &&
request.resource.data.role == 'owner' &&
request.resource.data.userId == request.auth.uid
)
);
// Allow update only by board owner
allow update: if isAuthenticated() && isBoardOwner(request.resource.data.boardId);
// Allow delete only by board owner
allow delete: if isAuthenticated() && isBoardOwner(resource.data.boardId);
}
match /boards_current/{boardId} {
// Allow read if authenticated (temporary for debug)
allow read: if isAuthenticated();
allow create: if isAuthenticated() && hasRequiredCreateFields();
allow update: if isAuthenticated() && hasRequiredUpdateFields() && (
resource.data.userId == request.auth.uid ||
resource.data.ownerId == request.auth.uid ||
(isBoardMember(boardId) && getBoardRole(boardId) in ['editor', 'owner'])
);
allow delete: if false;
match /history/{historyId} {
allow read, create: if isAuthenticated();
allow update, delete: if false;
}
}
match /lists_current/{listId} {
allow read: if isAuthenticated() && (
isOwner(resource.data.createdBy) ||
isBoardMember(resource.data.boardId) ||
isBoardOwner(resource.data.boardId)
);
allow create: if isAuthenticated() && hasRequiredCreateFields() && (
isBoardMember(request.resource.data.boardId) ||
isBoardOwner(request.resource.data.boardId)
);
allow update: if isAuthenticated() && hasRequiredUpdateFields() && (
isBoardMember(resource.data.boardId) && getBoardRole(resource.data.boardId) in ['editor', 'owner'] ||
isBoardOwner(resource.data.boardId)
);
allow delete: if false;
match /history/{historyId} {
allow read, create: if isAuthenticated();
allow update, delete: if false;
}
}
match /cards_current/{cardId} {
// Helper to get boardId from list
function getBoardIdFromList(listId) {
return get(/databases/$(database)/documents/lists_current/$(listId)).data.boardId;
}
allow read: if isAuthenticated() && (
isOwner(resource.data.createdBy) ||
isBoardMember(getBoardIdFromList(resource.data.listId)) ||
isBoardOwner(getBoardIdFromList(resource.data.listId))
);
allow create: if isAuthenticated() && hasRequiredCreateFields() && (
isBoardMember(getBoardIdFromList(request.resource.data.listId)) ||
isBoardOwner(getBoardIdFromList(request.resource.data.listId))
);
allow update: if isAuthenticated() && hasRequiredUpdateFields() && (
isBoardMember(getBoardIdFromList(resource.data.listId)) && getBoardRole(getBoardIdFromList(resource.data.listId)) in ['editor', 'owner'] ||
isBoardOwner(getBoardIdFromList(resource.data.listId))
);
allow delete: if false;
match /history/{historyId} {
allow read, create: if isAuthenticated();
allow update, delete: if false;
}
}
match /comments_current/{commentId} {
allow read: if isAuthenticated();
allow create: if isAuthenticated() && hasRequiredCreateFields();
allow update: if isAuthenticated() && isOwner(resource.data.userId) && hasRequiredUpdateFields();
allow delete: if false;
match /history/{historyId} {
allow read, create: if isAuthenticated();
allow update, delete: if false;
}
}
// Users: Can only read/update their own document
match /users/{userId} {
allow read: if isAuthenticated();
allow update: if isOwner(userId);
allow create: if isAuthenticated();
}
}
}