@edgedev/firebase 2.0.23 → 2.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/edgeFirebase.js +65 -0
package/package.json
CHANGED
package/src/edgeFirebase.js
CHANGED
|
@@ -252,6 +252,71 @@ exports.checkOrgIdExists = onCall(async (request) => {
|
|
|
252
252
|
return { exists: orgDoc.exists }
|
|
253
253
|
})
|
|
254
254
|
|
|
255
|
+
const permissionCheck = async (userId, action, collectionPath) => {
|
|
256
|
+
// Fetch user document
|
|
257
|
+
const userDoc = await db.collection('users').doc(userId).get()
|
|
258
|
+
const userData = userDoc.data()
|
|
259
|
+
|
|
260
|
+
// Fetch roles from user data
|
|
261
|
+
const roles = userData.roles || []
|
|
262
|
+
|
|
263
|
+
// Check each role for permission
|
|
264
|
+
for (let role of roles) {
|
|
265
|
+
if (role.collectionPath === collectionPath) {
|
|
266
|
+
// Fetch collection data
|
|
267
|
+
const collectionDoc = await db.collection('collection-data').doc(collectionPath).get()
|
|
268
|
+
const collectionData = collectionDoc.exists ? collectionDoc.data() : await db.collection('collection-data').doc('-default-').get().then(doc => doc.data())
|
|
269
|
+
|
|
270
|
+
// Check if action is permitted
|
|
271
|
+
if (collectionData && collectionData[role.role] && collectionData[role.role][action]) {
|
|
272
|
+
return true
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// If no permission found, return false
|
|
278
|
+
return false
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
exports.deleteSelf = onCall(async (request) => {
|
|
282
|
+
if (request.data.uid === request.auth.uid) {
|
|
283
|
+
try {
|
|
284
|
+
const userDoc = await db.collection('staged-users').doc(request.auth.uid).get()
|
|
285
|
+
const userData = userDoc.data()
|
|
286
|
+
const userCollectionPaths = userData.collectionPaths || []
|
|
287
|
+
|
|
288
|
+
for (let path of userCollectionPaths) {
|
|
289
|
+
const usersWithSamePath = await db.collection('staged-users').where('collectionPaths', 'array-contains', path).get()
|
|
290
|
+
|
|
291
|
+
// If no other users have the same collection path, delete the path and all documents and collections under it
|
|
292
|
+
if (usersWithSamePath.size <= 1) {
|
|
293
|
+
const docsToDelete = await db.collection(path).get()
|
|
294
|
+
const batch = db.batch()
|
|
295
|
+
docsToDelete.docs.forEach((doc) => {
|
|
296
|
+
batch.delete(doc.ref)
|
|
297
|
+
})
|
|
298
|
+
await batch.commit()
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Delete from 'staged-users' collection
|
|
303
|
+
await db.collection('staged-users').doc(request.data.uid).delete()
|
|
304
|
+
|
|
305
|
+
// Delete from 'users' collection
|
|
306
|
+
await db.collection('users').doc(request.data.uid).delete()
|
|
307
|
+
|
|
308
|
+
// Delete the user from Firebase
|
|
309
|
+
await admin.auth().deleteUser(request.data.uid)
|
|
310
|
+
|
|
311
|
+
return { success: true }
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
console.error('Error deleting user:', error)
|
|
315
|
+
return { success: false, error }
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
})
|
|
319
|
+
|
|
255
320
|
exports.updateUser = onDocumentUpdated({ document: 'staged-users/{docId}', timeoutSeconds: 180 }, async (event) => {
|
|
256
321
|
const change = event.data
|
|
257
322
|
const eventId = event.id
|