Repository navigation
Expand file tree
/
Copy pathfirestore.rules
More file actions
175 lines (151 loc) · 8.78 KB
/
Copy pathfirestore.rules
File metadata and controls
175 lines (151 loc) · 8.78 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
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// ===============================================================
// Helper Functions
// ===============================================================
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function hasRequiredFields(fields) {
return request.resource.data.keys().hasAll(fields);
}
function hasOnlyAllowedFields(fields) {
return request.resource.data.keys().hasOnly(fields);
}
function isValidEmail(email) {
return email is string && email.matches("^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$");
}
function isCompanyOwner(companyId) {
return isAuthenticated() &&
exists(/databases/$(database)/documents/companies/$(companyId)) &&
get(/databases/$(database)/documents/companies/$(companyId)).data.ownerId == request.auth.uid;
}
// ===============================================================
// Domain Validators
// ===============================================================
function isValidUser(data) {
return hasRequiredFields(['email', 'createdAt']) &&
data.email is string && isValidEmail(data.email) &&
data.createdAt is string &&
(!('displayName' in data) || (data.displayName is string && data.displayName.size() < 100)) &&
(!('photoURL' in data) || (data.photoURL is string && data.photoURL.size() < 1000));
}
function isValidCompany(data) {
return hasRequiredFields(['ownerId', 'name', 'industry', 'category', 'description', 'createdAt']) &&
data.ownerId == request.auth.uid &&
data.name is string && data.name.size() > 0 && data.name.size() < 100 &&
data.industry is string && data.industry.size() > 0 && data.industry.size() < 100 &&
data.category is string && data.category.size() > 0 && data.category.size() < 100 &&
data.description is string && data.description.size() > 0 && data.description.size() < 2000 &&
data.createdAt is string &&
(!('integrations' in data) || data.integrations is map);
}
function isValidAgent(data, companyId) {
return hasRequiredFields(['companyId', 'role', 'name', 'bio', 'expertise', 'avatarUrl', 'createdAt']) &&
data.companyId == companyId &&
data.role is string && data.role.size() > 0 && data.role.size() < 100 &&
data.name is string && data.name.size() > 0 && data.name.size() < 100 &&
data.bio is string && data.bio.size() > 0 && data.bio.size() < 2000 &&
data.expertise is list && data.expertise.size() > 0 && data.expertise.size() <= 10 &&
data.avatarUrl is string && data.avatarUrl.size() > 0 && data.avatarUrl.size() < 1000 &&
data.createdAt is string;
}
function isValidMessage(data, companyId) {
return hasRequiredFields(['companyId', 'senderId', 'text', 'timestamp']) &&
data.companyId == companyId &&
data.senderId is string && data.senderId.size() > 0 && data.senderId.size() < 100 &&
data.text is string && data.text.size() > 0 && data.text.size() < 10000 &&
data.timestamp is number &&
(!('fileName' in data) || (data.fileName is string && data.fileName.size() < 500)) &&
(!('fileContent' in data) || (data.fileContent is string && data.fileContent.size() < 1000000)) &&
(!('proposals' in data) || data.proposals is list);
}
function isValidTask(data, companyId) {
return hasRequiredFields(['companyId', 'assignedTo', 'title', 'status', 'createdAt']) &&
data.companyId == companyId &&
data.assignedTo is string && data.assignedTo.size() > 0 &&
data.title is string && data.title.size() > 0 && data.title.size() < 500 &&
data.status in ['pending', 'in-progress', 'completed', 'failed'] &&
data.createdAt is number &&
(!('description' in data) || (data.description is string && data.description.size() < 2000));
}
function isValidGoal(data, companyId) {
return hasRequiredFields(['companyId', 'objective', 'smartGoals', 'kpis', 'createdAt']) &&
data.companyId == companyId &&
data.objective is string && data.objective.size() > 0 && data.objective.size() < 2000 &&
data.smartGoals is list && data.smartGoals.size() >= 0 && data.smartGoals.size() <= 20 &&
data.kpis is list && data.kpis.size() >= 0 && data.kpis.size() <= 20 &&
data.createdAt is number;
}
function isValidAsset(data, companyId) {
return hasRequiredFields(['companyId', 'name', 'content', 'createdAt']) &&
data.companyId == companyId &&
data.name is string && data.name.size() > 0 && data.name.size() < 500 &&
data.content is string && data.content.size() > 0 && data.content.size() < 1000000 &&
data.createdAt is number;
}
// ===============================================================
// Rules
// ===============================================================
match /users/{userId} {
allow read: if isOwner(userId);
allow create: if isOwner(userId) && isValidUser(request.resource.data);
allow update: if isOwner(userId) && isValidUser(request.resource.data) &&
request.resource.data.createdAt == resource.data.createdAt;
allow delete: if false; // Users cannot delete their profiles directly
}
match /companies/{companyId} {
allow read: if isAuthenticated() && resource.data.ownerId == request.auth.uid;
allow create: if isAuthenticated() && isValidCompany(request.resource.data);
allow update: if isAuthenticated() && resource.data.ownerId == request.auth.uid &&
isValidCompany(request.resource.data) &&
request.resource.data.ownerId == resource.data.ownerId &&
request.resource.data.createdAt == resource.data.createdAt;
allow delete: if isAuthenticated() && resource.data.ownerId == request.auth.uid;
match /agents/{agentId} {
allow read: if isCompanyOwner(companyId);
allow create: if isCompanyOwner(companyId) && isValidAgent(request.resource.data, companyId);
allow update: if isCompanyOwner(companyId) && isValidAgent(request.resource.data, companyId) &&
request.resource.data.companyId == resource.data.companyId &&
request.resource.data.createdAt == resource.data.createdAt;
allow delete: if isCompanyOwner(companyId);
}
match /messages/{messageId} {
allow read: if isCompanyOwner(companyId);
allow create: if isCompanyOwner(companyId) && isValidMessage(request.resource.data, companyId);
allow update: if isCompanyOwner(companyId) && isValidMessage(request.resource.data, companyId) &&
request.resource.data.companyId == resource.data.companyId &&
request.resource.data.timestamp == resource.data.timestamp;
allow delete: if isCompanyOwner(companyId);
}
match /tasks/{taskId} {
allow read: if isCompanyOwner(companyId);
allow create: if isCompanyOwner(companyId) && isValidTask(request.resource.data, companyId);
allow update: if isCompanyOwner(companyId) && isValidTask(request.resource.data, companyId) &&
request.resource.data.companyId == resource.data.companyId &&
request.resource.data.createdAt == resource.data.createdAt;
allow delete: if isCompanyOwner(companyId);
}
match /goals/{goalId} {
allow read: if isCompanyOwner(companyId);
allow create: if isCompanyOwner(companyId) && isValidGoal(request.resource.data, companyId);
allow update: if isCompanyOwner(companyId) && isValidGoal(request.resource.data, companyId) &&
request.resource.data.companyId == resource.data.companyId &&
request.resource.data.createdAt == resource.data.createdAt;
allow delete: if isCompanyOwner(companyId);
}
match /assets/{assetId} {
allow read: if isCompanyOwner(companyId);
allow create: if isCompanyOwner(companyId) && isValidAsset(request.resource.data, companyId);
allow update: if isCompanyOwner(companyId) && isValidAsset(request.resource.data, companyId) &&
request.resource.data.companyId == resource.data.companyId &&
request.resource.data.createdAt == resource.data.createdAt;
allow delete: if isCompanyOwner(companyId);
}
}
}
}