@firecms/user_management 3.0.0-canary.117 → 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
@@ -1,6 +1,6 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("react-fast-compare"), require("react/jsx-runtime"), require("@firecms/ui"), require("yup"), require("@firecms/core"), require("@firecms/formex"), require("date-fns"), require("date-fns/locale")) : typeof define === "function" && define.amd ? define(["exports", "react", "react-fast-compare", "react/jsx-runtime", "@firecms/ui", "yup", "@firecms/core", "@firecms/formex", "date-fns", "date-fns/locale"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.FireCMS = {}, global.React, global.equal, global.jsxRuntime, global.ui, global.Yup, global.core, global.formex, global.dateFns, global.locales));
3
- })(this, function(exports2, React, equal, jsxRuntime, ui, Yup, core, formex, dateFns, locales) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("react-fast-compare"), require("@firecms/core"), require("react/jsx-runtime"), require("@firecms/ui"), require("yup"), require("@firecms/formex"), require("date-fns"), require("date-fns/locale")) : typeof define === "function" && define.amd ? define(["exports", "react", "react-fast-compare", "@firecms/core", "react/jsx-runtime", "@firecms/ui", "yup", "@firecms/formex", "date-fns", "date-fns/locale"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.FireCMS = {}, global.React, global.equal, global.core, global.jsxRuntime, global.ui, global.Yup, global.formex, global.dateFns, global.locales));
3
+ })(this, function(exports2, React, equal, core, jsxRuntime, ui, Yup, formex, dateFns, locales) {
4
4
  "use strict";
5
5
  function _interopNamespaceDefault(e) {
6
6
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -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");
@@ -232,15 +243,19 @@
232
243
  const roleIds = user.roles?.map((r) => r.id);
233
244
  const email = user.email?.toLowerCase().trim();
234
245
  if (!email) throw Error("Email is required");
246
+ const userExists = users.find((u) => u.email?.toLowerCase() === email);
235
247
  const data = {
236
248
  ...user,
237
- roles: roleIds
249
+ roles: roleIds ?? []
238
250
  };
251
+ if (!userExists) {
252
+ data.created_on = /* @__PURE__ */ new Date();
253
+ }
239
254
  return dataSourceDelegate.saveEntity({
240
255
  status: "existing",
241
256
  path: usersPath,
242
257
  entityId: email,
243
- values: data
258
+ values: core.removeUndefined(data)
244
259
  }).then(() => user);
245
260
  }, [usersPath, dataSourceDelegate?.initialised]);
246
261
  const saveRole = React.useCallback((role) => {
@@ -255,7 +270,7 @@
255
270
  status: "existing",
256
271
  path: rolesPath,
257
272
  entityId: id,
258
- values: roleData
273
+ values: core.removeUndefined(roleData)
259
274
  }).then(() => {
260
275
  return;
261
276
  });
@@ -310,7 +325,7 @@
310
325
  return true;
311
326
  }
312
327
  throw Error("Could not find a user with the provided email in the user management system.");
313
- }, [loading, users]);
328
+ }, [loading, users, usersError, rolesError]);
314
329
  const isAdmin = roles.some((r) => r.id === "admin");
315
330
  return {
316
331
  loading,
@@ -382,6 +397,13 @@
382
397
  id: Yup__namespace.string().required("Required"),
383
398
  name: Yup__namespace.string().required("Required")
384
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
+ }
385
407
  function RolesDetailsForm({
386
408
  open,
387
409
  role,
@@ -391,22 +413,34 @@
391
413
  }) {
392
414
  const { saveRole } = useUserManagement();
393
415
  const isNewRole = !role;
416
+ const {
417
+ user: loggedInUser
418
+ } = core.useAuthController();
394
419
  const [savingError, setSavingError] = React.useState();
395
420
  const onRoleUpdated = React.useCallback((role2) => {
396
421
  setSavingError(void 0);
422
+ if (!loggedInUser) throw new Error("User not found");
423
+ canRoleBeEdited(loggedInUser);
397
424
  return saveRole(role2);
398
- }, [saveRole]);
425
+ }, [saveRole, loggedInUser]);
399
426
  const formex$1 = formex.useCreateFormex({
400
427
  initialValues: role ?? {
401
428
  name: ""
402
429
  },
403
430
  onSubmit: (role2, formexController) => {
404
- return onRoleUpdated(role2).then(() => {
405
- formexController.resetForm({
406
- values: role2
431
+ try {
432
+ return onRoleUpdated(role2).then(() => {
433
+ formexController.resetForm({
434
+ values: role2
435
+ });
436
+ handleClose();
437
+ }).catch((e) => {
438
+ setSavingError(e);
407
439
  });
408
- handleClose();
409
- }).catch((e) => setSavingError(e));
440
+ } catch (e) {
441
+ setSavingError(e);
442
+ return Promise.resolve();
443
+ }
410
444
  },
411
445
  validation: (values2) => {
412
446
  return RoleYupSchema.validate(values2, { abortEarly: false }).then(() => ({})).catch((e) => {
@@ -855,7 +889,7 @@
855
889
  ] })
856
890
  ] }),
857
891
  /* @__PURE__ */ jsxRuntime.jsxs(ui.DialogActions, { position: "sticky", children: [
858
- 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" }),
859
893
  /* @__PURE__ */ jsxRuntime.jsx(
860
894
  ui.Button,
861
895
  {