@edgedev/firebase 1.4.1 → 1.4.3

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,23 +242,23 @@ 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 list them grouped by collections.
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.
246
246
 
247
247
  ```javascript
248
248
  const users = await edgeFirebase.listUsers();
249
249
  ```
250
250
 
251
251
  ```typescript
252
- interface usersByCollection {
253
- [collectionPath: string]: [user];
252
+ interface usersByEmail {
253
+ [email: string]: [user];
254
254
  }
255
255
  ```
256
256
 
257
257
  ```typescript
258
258
  interface user {
259
259
  email: string;
260
- role: "admin" | "user" | null;
261
- specialPermission: permissions | null;
260
+ roles: role[];
261
+ specialPermissions: specialPermission[];
262
262
  userId: string;
263
263
  docId: string;
264
264
  uid: string;
@@ -266,6 +266,29 @@ interface user {
266
266
  }
267
267
  ```
268
268
 
269
+ ```typescript
270
+ interface role {
271
+ collectionPath: "-" | string; // - is root
272
+ role: "admin" | "user";
273
+ }
274
+ ```
275
+
276
+ ```typescript
277
+ interface specialPermission {
278
+ collectionPath: "-" | string; // - is root
279
+ permissions: permissions;
280
+ }
281
+ ```
282
+
283
+ ```typescript
284
+ interface permissions {
285
+ assign: boolean;
286
+ read: boolean;
287
+ write: boolean;
288
+ delete: boolean;
289
+ }
290
+ ```
291
+
269
292
 
270
293
 
271
294
  ### List Collections with Assign Access
package/edgeFirebase.ts CHANGED
@@ -103,16 +103,16 @@ interface newUser {
103
103
 
104
104
  interface user {
105
105
  email: string;
106
- role: "admin" | "user" | null;
107
- specialPermission: permissions | null;
106
+ roles: role[];
107
+ specialPermissions: specialPermission[];
108
108
  userId: string;
109
109
  docId: string;
110
110
  uid: string;
111
111
  last_updated: Date;
112
112
  }
113
113
 
114
- interface usersByCollection {
115
- [collectionPath: string]: [user];
114
+ interface usersByEmail {
115
+ [email: string]: [user];
116
116
  }
117
117
  interface userMeta extends newUser {
118
118
  docId: string;
@@ -438,19 +438,28 @@ export const EdgeFirebase = class {
438
438
  newUser.specialPermissions
439
439
  );
440
440
  if (canAssignRole.canDo && canAssignSpecialPermissions.canDo) {
441
- const userMeta: userMeta = {
442
- docId: newUser.email,
443
- userId: "",
444
- email: newUser.email,
445
- roles: newUser.roles,
446
- specialPermissions: newUser.specialPermissions,
447
- meta: newUser.meta
448
- };
449
- this.generateUserMeta(userMeta);
450
- return this.sendResponse({
451
- success: true,
452
- message: ""
453
- });
441
+ const userRef = doc(this.db, "users", newUser.email);
442
+ const userSnap = await getDoc(userRef);
443
+ if (!userSnap.exists()) {
444
+ const userMeta: userMeta = {
445
+ docId: newUser.email,
446
+ userId: "",
447
+ email: newUser.email,
448
+ roles: newUser.roles,
449
+ specialPermissions: newUser.specialPermissions,
450
+ meta: newUser.meta
451
+ };
452
+ this.generateUserMeta(userMeta);
453
+ return this.sendResponse({
454
+ success: true,
455
+ message: ""
456
+ });
457
+ } else {
458
+ return this.sendResponse({
459
+ success: false,
460
+ message: "User already exists"
461
+ });
462
+ }
454
463
  } else {
455
464
  return this.sendResponse({
456
465
  success: false,
@@ -988,6 +997,7 @@ export const EdgeFirebase = class {
988
997
  }
989
998
  };
990
999
 
1000
+ // TODO: change this function to be synced dynamically on the user object
991
1001
  public listCollectionsCanAssign = async (): Promise<string[]> => {
992
1002
  let collectionPaths = [];
993
1003
  for (const role of this.user.roles) {
@@ -1035,11 +1045,18 @@ export const EdgeFirebase = class {
1035
1045
  return collectionPathList;
1036
1046
  };
1037
1047
 
1038
- public listUsers = async (): Promise<usersByCollection> => {
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
+ public listUsers = async (collectionPath = ''): Promise<usersByEmail> => {
1039
1051
  const userList = {};
1052
+ if (collectionPath) {
1053
+ const canAssign = await this.permissionCheck("assign", collectionPath);
1054
+ if (!canAssign) {
1055
+ return {}
1056
+ }
1057
+ }
1040
1058
  const collectionPathList = await this.listCollectionsCanAssign();
1041
1059
  for (const collectionPath of collectionPathList) {
1042
- userList[collectionPath] = [];
1043
1060
  const roleUsers = await getDocs(
1044
1061
  query(
1045
1062
  collection(this.db, "users"),
@@ -1052,16 +1069,20 @@ export const EdgeFirebase = class {
1052
1069
  );
1053
1070
  roleUsers.forEach((doc) => {
1054
1071
  const user = doc.data();
1055
- userList[collectionPath].push({
1056
- docId: user.docId,
1057
- email: user.email,
1058
- role: user.roles[collectionPath].role,
1059
- specialPermission: null,
1060
- meta: user.meta,
1061
- last_updated: user.last_updated,
1062
- userId: user.userId,
1063
- uid: user.uid
1064
- });
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
1082
+ }
1083
+ } else {
1084
+ userList[user.email].roles.push({ collectionPath, role: user.roles[collectionPath].role })
1085
+ }
1065
1086
  });
1066
1087
  const specialPermissionsUsers = await getDocs(
1067
1088
  query(
@@ -1075,17 +1096,20 @@ export const EdgeFirebase = class {
1075
1096
  );
1076
1097
  specialPermissionsUsers.forEach((doc) => {
1077
1098
  const user = doc.data();
1078
- userList[collectionPath].push({
1079
- docId: user.docId,
1080
- email: user.email,
1081
- role: null,
1082
- specialPermission:
1083
- user.specialPermissions[collectionPath].permissions,
1084
- meta: user.meta,
1085
- last_updated: user.last_updated,
1086
- userId: user.userId,
1087
- uid: user.uid
1088
- });
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
1109
+ }
1110
+ } else {
1111
+ userList[user.email].specialPermissions.push({ collectionPath, permissions: user.specialPermissions[collectionPath].permissions })
1112
+ }
1089
1113
  });
1090
1114
  }
1091
1115
  return userList;
@@ -1171,7 +1195,7 @@ export const EdgeFirebase = class {
1171
1195
  }
1172
1196
  };
1173
1197
 
1174
- public storeUserRoles = async (
1198
+ private storeUserRoles = async (
1175
1199
  email: string,
1176
1200
  collectionPath: string,
1177
1201
  role: "admin" | "user"
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 global composable for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
6
9
  "author": "Seth Fischer",
7
10
  "keywords": [
8
11
  "firebase authentication",
@@ -32,8 +35,5 @@
32
35
  "peerDependencies": {
33
36
  "firebase": "^9.12.1",
34
37
  "vue": "^3.0.0"
35
- },
36
- "scripts": {
37
- "test": "echo \"Error: no test specified\" && exit 1"
38
38
  }
39
- }
39
+ }