@edgedev/firebase 2.0.23 → 2.0.25

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "2.0.23",
3
+ "version": "2.0.25",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 plugin for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {
@@ -252,6 +252,82 @@ 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 (const 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 (const 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 adjustedPath = path.replace(/-/g, '/')
294
+ const docRef = db.doc(adjustedPath)
295
+ const doc = await docRef.get()
296
+
297
+ if (doc.exists) {
298
+ // If the path is a document, delete it directly
299
+ await docRef.delete()
300
+ }
301
+ else {
302
+ // If the path is a collection, delete all documents under it
303
+ const docsToDelete = await db.collection(adjustedPath).get()
304
+ const batch = db.batch()
305
+ docsToDelete.docs.forEach((doc) => {
306
+ batch.delete(doc.ref)
307
+ })
308
+ await batch.commit()
309
+ }
310
+ }
311
+ }
312
+
313
+ // Delete from 'staged-users' collection
314
+ await db.collection('staged-users').doc(request.data.uid).delete()
315
+
316
+ // Delete from 'users' collection
317
+ await db.collection('users').doc(request.data.uid).delete()
318
+
319
+ // Delete the user from Firebase
320
+ await admin.auth().deleteUser(request.data.uid)
321
+
322
+ return { success: true }
323
+ }
324
+ catch (error) {
325
+ console.error('Error deleting user:', error)
326
+ return { success: false, error }
327
+ }
328
+ }
329
+ })
330
+
255
331
  exports.updateUser = onDocumentUpdated({ document: 'staged-users/{docId}', timeoutSeconds: 180 }, async (event) => {
256
332
  const change = event.data
257
333
  const eventId = event.id
@@ -343,7 +419,6 @@ exports.updateUser = onDocumentUpdated({ document: 'staged-users/{docId}', timeo
343
419
  })
344
420
 
345
421
  async function setUser(userRef, newData, oldData, stagedDocId) {
346
-
347
422
  const user = await userRef.get()
348
423
  let userUpdate = { meta: newData.meta, stagedDocId }
349
424