@edgedev/firebase 1.4.3 → 1.4.5

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/README.md CHANGED
@@ -242,10 +242,10 @@ edgeFirebase.removeUser("user@edgemarketingdesign.com");
242
242
 
243
243
  ### List Users
244
244
 
245
- This will list all users that are members of collections that the user running the function has assign access for, it will be a listed index by email/user id.
245
+ This will list all users that are members of the collection and subcollections passed to the function that the user running the function has assign access for, it will be a listed index by email/user id.
246
246
 
247
247
  ```javascript
248
- const users = await edgeFirebase.listUsers();
248
+ const users = await edgeFirebase.listUsers("myItems");
249
249
  ```
250
250
 
251
251
  ```typescript
@@ -289,16 +289,6 @@ interface permissions {
289
289
  }
290
290
  ```
291
291
 
292
-
293
-
294
- ### List Collections with Assign Access
295
-
296
- This function will list all collections that the user running it has assign access for.
297
-
298
- ```javascript
299
- const collections = await edgeFirebase.listCollectionsCanAssign(); // returns array of strings (collection paths)
300
- ```
301
-
302
292
  # Firebase Authentication
303
293
 
304
294
  (currently only sign in with email and password is supported)
@@ -325,7 +315,8 @@ interface UserDataObject {
325
315
  logInErrorMessage: string;
326
316
  meta: object;
327
317
  roles: role[]; //see role below
328
- specialPermissions: specialPermission[]; //see specialPermission bleow
318
+ specialPermissions: specialPermission[]; //see specialPermission below
319
+ canAssignCollectionPaths: string[]; //an array of collectionPaths that the user has "assign" access to
329
320
  }
330
321
 
331
322
  // sub types of UserDataObject:
package/edgeFirebase.ts CHANGED
@@ -92,6 +92,7 @@ interface UserDataObject {
92
92
  meta: object;
93
93
  roles: role[];
94
94
  specialPermissions: specialPermission[];
95
+ canAssignCollectionPaths: string[];
95
96
  }
96
97
 
97
98
  interface newUser {
@@ -209,6 +210,7 @@ export const EdgeFirebase = class {
209
210
  }
210
211
  }
211
212
  this.user.specialPermissions = specialPermissions;
213
+ this.listCollectionsCanAssign()
212
214
  }
213
215
  const metaUnsubscribe = onSnapshot(
214
216
  doc(this.db, "users", this.user.email),
@@ -218,7 +220,7 @@ export const EdgeFirebase = class {
218
220
  email: this.user.email,
219
221
  roles: [],
220
222
  specialPermissions: [],
221
- meta: {}
223
+ meta: {},
222
224
  });
223
225
  this.user.meta = {};
224
226
  } else {
@@ -245,6 +247,7 @@ export const EdgeFirebase = class {
245
247
  }
246
248
  }
247
249
  this.user.specialPermissions = specialPermissions;
250
+ this.listCollectionsCanAssign()
248
251
  }
249
252
  }
250
253
  );
@@ -724,7 +727,8 @@ export const EdgeFirebase = class {
724
727
  logInErrorMessage: "",
725
728
  meta: {},
726
729
  roles: [],
727
- specialPermissions: []
730
+ specialPermissions: [],
731
+ canAssignCollectionPaths: [],
728
732
  });
729
733
 
730
734
  public getDocData = async (
@@ -997,8 +1001,7 @@ export const EdgeFirebase = class {
997
1001
  }
998
1002
  };
999
1003
 
1000
- // TODO: change this function to be synced dynamically on the user object
1001
- public listCollectionsCanAssign = async (): Promise<string[]> => {
1004
+ private listCollectionsCanAssign = async (): Promise<void> => {
1002
1005
  let collectionPaths = [];
1003
1006
  for (const role of this.user.roles) {
1004
1007
  const canAssign = await this.permissionCheck(
@@ -1042,75 +1045,71 @@ export const EdgeFirebase = class {
1042
1045
  }
1043
1046
  }
1044
1047
  collectionPathList = [...new Set(collectionPathList)];
1045
- return collectionPathList;
1048
+ this.user.canAssignCollectionPaths = collectionPathList;
1046
1049
  };
1047
1050
 
1048
- // TODO: finish making this query by collectionPath if passed.. in furture will be used to get users by collectionPath
1049
- // because having one giant list of users is not scalable
1050
1051
  public listUsers = async (collectionPath = ''): Promise<usersByEmail> => {
1051
1052
  const userList = {};
1052
- if (collectionPath) {
1053
- const canAssign = await this.permissionCheck("assign", collectionPath);
1054
- if (!canAssign) {
1055
- return {}
1056
- }
1057
- }
1058
- const collectionPathList = await this.listCollectionsCanAssign();
1059
- for (const collectionPath of collectionPathList) {
1060
- const roleUsers = await getDocs(
1061
- query(
1062
- collection(this.db, "users"),
1063
- where(
1064
- "roles." + collectionPath + ".collectionPath",
1065
- "==",
1066
- collectionPath
1053
+
1054
+ for (const collectionPathCheck of this.user.canAssignCollectionPaths) {
1055
+
1056
+ if (collectionPathCheck.startsWith(collectionPath.replaceAll('/', '-'))) {
1057
+ const roleUsers = await getDocs(
1058
+ query(
1059
+ collection(this.db, "users"),
1060
+ where(
1061
+ "roles." + collectionPathCheck + ".collectionPath",
1062
+ "==",
1063
+ collectionPathCheck
1064
+ )
1067
1065
  )
1068
- )
1069
- );
1070
- roleUsers.forEach((doc) => {
1071
- const user = doc.data();
1072
- if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
1073
- userList[user.email] = {
1074
- docId: user.docId,
1075
- email: user.email,
1076
- roles: [{collectionPath, role: user.roles[collectionPath].role }],
1077
- specialPermissions: [],
1078
- meta: user.meta,
1079
- last_updated: user.last_updated,
1080
- userId: user.userId,
1081
- uid: user.uid
1066
+ );
1067
+
1068
+ roleUsers.forEach((doc) => {
1069
+ const user = doc.data();
1070
+ if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
1071
+ userList[user.email] = {
1072
+ docId: user.docId,
1073
+ email: user.email,
1074
+ roles: [{collectionPath: collectionPathCheck, role: user.roles[collectionPathCheck].role }],
1075
+ specialPermissions: [],
1076
+ meta: user.meta,
1077
+ last_updated: user.last_updated,
1078
+ userId: user.userId,
1079
+ uid: user.uid
1080
+ }
1081
+ } else {
1082
+ userList[user.email].roles.push({ collectionPath: collectionPathCheck, role: user.roles[collectionPathCheck].role })
1082
1083
  }
1083
- } else {
1084
- userList[user.email].roles.push({ collectionPath, role: user.roles[collectionPath].role })
1085
- }
1086
- });
1087
- const specialPermissionsUsers = await getDocs(
1088
- query(
1089
- collection(this.db, "users"),
1090
- where(
1091
- "specialPermissions." + collectionPath + ".collectionPath",
1092
- "==",
1093
- collectionPath
1084
+ });
1085
+ const specialPermissionsUsers = await getDocs(
1086
+ query(
1087
+ collection(this.db, "users"),
1088
+ where(
1089
+ "specialPermissions." + collectionPathCheck + ".collectionPath",
1090
+ "==",
1091
+ collectionPathCheck
1092
+ )
1094
1093
  )
1095
- )
1096
- );
1097
- specialPermissionsUsers.forEach((doc) => {
1098
- const user = doc.data();
1099
- if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
1100
- userList[user.email] = {
1101
- docId: user.docId,
1102
- email: user.email,
1103
- role: [],
1104
- specialPermissions: [{ collectionPath, permissions: user.specialPermissions[collectionPath].permissions }],
1105
- meta: user.meta,
1106
- last_updated: user.last_updated,
1107
- userId: user.userId,
1108
- uid: user.uid
1094
+ );
1095
+ specialPermissionsUsers.forEach((doc) => {
1096
+ const user = doc.data();
1097
+ if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
1098
+ userList[user.email] = {
1099
+ docId: user.docId,
1100
+ email: user.email,
1101
+ role: [],
1102
+ specialPermissions: [{ collectionPath: collectionPathCheck, permissions: user.specialPermissions[collectionPathCheck].permissions }],
1103
+ meta: user.meta,
1104
+ last_updated: user.last_updated,
1105
+ userId: user.userId,
1106
+ uid: user.uid
1107
+ }
1108
+ } else {
1109
+ userList[user.email].specialPermissions.push({ collectionPath: collectionPathCheck, permissions: user.specialPermissions[collectionPathCheck].permissions })
1109
1110
  }
1110
- } else {
1111
- userList[user.email].specialPermissions.push({ collectionPath, permissions: user.specialPermissions[collectionPath].permissions })
1112
- }
1113
- });
1111
+ });
1112
+ }
1114
1113
  }
1115
1114
  return userList;
1116
1115
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 global composable for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {