@firecms/user_management 3.0.0-canary.118 → 3.0.0-canary.119

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/dist/index.umd.js CHANGED
@@ -58,8 +58,9 @@
58
58
  edit: role.isAdmin || role.defaultPermissions?.edit,
59
59
  delete: role.isAdmin || role.defaultPermissions?.delete
60
60
  };
61
- if (role.collectionPermissions && role.collectionPermissions[id]) {
62
- return mergePermissions(role.collectionPermissions[id], basePermissions);
61
+ const thisCollectionPermissions = role.collectionPermissions?.[id];
62
+ if (thisCollectionPermissions) {
63
+ return mergePermissions(thisCollectionPermissions, basePermissions);
63
64
  } else if (role.defaultPermissions) {
64
65
  return mergePermissions(role.defaultPermissions, basePermissions);
65
66
  } else {
@@ -176,6 +177,11 @@
176
177
  React.useEffect(() => {
177
178
  if (!dataSourceDelegate || !rolesPath) return;
178
179
  if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
180
+ if (dataSourceDelegate.authenticated !== void 0 && !dataSourceDelegate.authenticated) {
181
+ setRolesLoading(false);
182
+ return;
183
+ }
184
+ setRolesLoading(true);
179
185
  return dataSourceDelegate.listenCollection?.({
180
186
  path: rolesPath,
181
187
  onUpdate(entities) {
@@ -198,10 +204,15 @@
198
204
  setRolesLoading(false);
199
205
  }
200
206
  });
201
- }, [dataSourceDelegate?.initialised, rolesPath]);
207
+ }, [dataSourceDelegate?.initialised, dataSourceDelegate?.authenticated, rolesPath]);
202
208
  React.useEffect(() => {
203
209
  if (!dataSourceDelegate || !usersPath) return;
204
210
  if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
211
+ if (dataSourceDelegate.authenticated !== void 0 && !dataSourceDelegate.authenticated) {
212
+ setUsersLoading(false);
213
+ return;
214
+ }
215
+ setUsersLoading(true);
205
216
  return dataSourceDelegate.listenCollection?.({
206
217
  path: usersPath,
207
218
  onUpdate(entities) {
@@ -224,7 +235,7 @@
224
235
  setUsersLoading(false);
225
236
  }
226
237
  });
227
- }, [dataSourceDelegate?.initialised, usersPath]);
238
+ }, [dataSourceDelegate?.initialised, dataSourceDelegate?.authenticated, usersPath]);
228
239
  const saveUser = React.useCallback(async (user) => {
229
240
  if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
230
241
  if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
@@ -314,7 +325,7 @@
314
325
  return true;
315
326
  }
316
327
  throw Error("Could not find a user with the provided email in the user management system.");
317
- }, [loading, users]);
328
+ }, [loading, users, usersError, rolesError]);
318
329
  const isAdmin = roles.some((r) => r.id === "admin");
319
330
  return {
320
331
  loading,
@@ -386,6 +397,13 @@
386
397
  id: Yup__namespace.string().required("Required"),
387
398
  name: Yup__namespace.string().required("Required")
388
399
  });
400
+ function canRoleBeEdited(loggedUser) {
401
+ const loggedUserIsAdmin = loggedUser.roles?.map((r) => r.id).includes("admin");
402
+ if (!loggedUserIsAdmin) {
403
+ throw new Error("Only admins can edit roles");
404
+ }
405
+ return true;
406
+ }
389
407
  function RolesDetailsForm({
390
408
  open,
391
409
  role,
@@ -395,22 +413,34 @@
395
413
  }) {
396
414
  const { saveRole } = useUserManagement();
397
415
  const isNewRole = !role;
416
+ const {
417
+ user: loggedInUser
418
+ } = core.useAuthController();
398
419
  const [savingError, setSavingError] = React.useState();
399
420
  const onRoleUpdated = React.useCallback((role2) => {
400
421
  setSavingError(void 0);
422
+ if (!loggedInUser) throw new Error("User not found");
423
+ canRoleBeEdited(loggedInUser);
401
424
  return saveRole(role2);
402
- }, [saveRole]);
425
+ }, [saveRole, loggedInUser]);
403
426
  const formex$1 = formex.useCreateFormex({
404
427
  initialValues: role ?? {
405
428
  name: ""
406
429
  },
407
430
  onSubmit: (role2, formexController) => {
408
- return onRoleUpdated(role2).then(() => {
409
- formexController.resetForm({
410
- values: role2
431
+ try {
432
+ return onRoleUpdated(role2).then(() => {
433
+ formexController.resetForm({
434
+ values: role2
435
+ });
436
+ handleClose();
437
+ }).catch((e) => {
438
+ setSavingError(e);
411
439
  });
412
- handleClose();
413
- }).catch((e) => setSavingError(e));
440
+ } catch (e) {
441
+ setSavingError(e);
442
+ return Promise.resolve();
443
+ }
414
444
  },
415
445
  validation: (values2) => {
416
446
  return RoleYupSchema.validate(values2, { abortEarly: false }).then(() => ({})).catch((e) => {
@@ -859,7 +889,7 @@
859
889
  ] })
860
890
  ] }),
861
891
  /* @__PURE__ */ jsxRuntime.jsxs(ui.DialogActions, { position: "sticky", children: [
862
- savingError && /* @__PURE__ */ jsxRuntime.jsx(ui.Typography, { className: "text-red-500", children: "There was an error saving this role" }),
892
+ savingError && /* @__PURE__ */ jsxRuntime.jsx(ui.Typography, { className: "text-red-500 dark:text-red-500", children: savingError.message ?? "There was an error saving this role" }),
863
893
  /* @__PURE__ */ jsxRuntime.jsx(
864
894
  ui.Button,
865
895
  {