@firecms/user_management 3.0.0-canary.29 → 3.0.0-canary.292

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.es.js CHANGED
@@ -1,1281 +1,2298 @@
1
- import A, { useRef as Ee, useEffect as Z, useCallback as k, useContext as Ae, useState as P } from "react";
2
- import { getFirestore as ee, onSnapshot as ae, collection as ne, setDoc as ce, doc as G, addDoc as Pe, deleteDoc as de } from "firebase/firestore";
3
- import { jsx as e, jsxs as l, Fragment as H } from "react/jsx-runtime";
4
- import { getColorSchemeForSeed as Fe, Chip as Te, Dialog as ue, DialogContent as me, Typography as O, TextField as K, Paper as Be, Table as re, TableHeader as oe, TableCell as a, TableBody as le, TableRow as q, Tooltip as V, Checkbox as E, Select as te, SelectItem as T, DialogActions as he, Button as Y, LoadingButton as fe, DoneIcon as ge, IconButton as pe, DeleteIcon as Ce, CenteredView as ve, Container as we, AddIcon as ye, MultiSelect as Le, MultiSelectItem as Ve } from "@firecms/ui";
5
- import * as _ from "yup";
6
- import { toSnakeCase as _e, FieldCaption as B, DeleteConfirmationDialog as be, useNavigationController as Oe, useSnackbarController as Ne, useAuthController as Re, useCustomizationController as $e, defaultDateFormat as Me } from "@firecms/core";
7
- import { useCreateFormex as xe, getIn as j, Formex as De } from "@firecms/formex";
8
- import { format as qe } from "date-fns";
9
- import * as Ye from "date-fns/locale";
10
- const pn = ["Admin"], We = {
11
- read: !1,
12
- edit: !1,
13
- create: !1,
14
- delete: !1
1
+ import React, { useEffect, useCallback, useContext, useState } from "react";
2
+ import equal from "react-fast-compare";
3
+ import { removeUndefined, useAuthController, toSnakeCase, FieldCaption, ConfirmationDialog, useNavigationController, useSnackbarController, useCustomizationController, defaultDateFormat } from "@firecms/core";
4
+ import { jsx, jsxs, Fragment } from "react/jsx-runtime";
5
+ import { c } from "react-compiler-runtime";
6
+ import { getColorSchemeForSeed, Chip, TextField, TableRow, TableCell, Checkbox, Tooltip, Button, Typography, DialogTitle, TableHeader, Paper, Table, TableBody, SelectItem, Select, DialogContent, LoadingButton, DialogActions, Dialog, IconButton, DeleteIcon, CenteredView, AddIcon, Container, MultiSelectItem, MultiSelect } from "@firecms/ui";
7
+ import * as Yup from "yup";
8
+ import { useCreateFormex, getIn, Formex } from "@firecms/formex";
9
+ import { format } from "date-fns";
10
+ import * as locales from "date-fns/locale";
11
+ const RESERVED_GROUPS = ["Admin"];
12
+ const DEFAULT_PERMISSIONS = {
13
+ read: false,
14
+ edit: false,
15
+ create: false,
16
+ delete: false
15
17
  };
16
- function je({
17
- collection: t,
18
- user: i
18
+ function resolveUserRolePermissions({
19
+ collection,
20
+ user
19
21
  }) {
20
- const n = i?.roles;
21
- if (n) {
22
- if (t.ownerId === i?.uid)
23
- return {
24
- read: !0,
25
- create: !0,
26
- edit: !0,
27
- delete: !0
28
- };
29
- {
30
- const c = {
31
- read: !1,
32
- create: !1,
33
- edit: !1,
34
- delete: !1
35
- };
36
- return n.map((r) => ze(r, t.id)).reduce(ie, c);
37
- }
38
- } else
39
- return We;
22
+ const roles = user?.roles;
23
+ if (!roles) {
24
+ return DEFAULT_PERMISSIONS;
25
+ } else if (collection.ownerId === user?.uid) {
26
+ return {
27
+ read: true,
28
+ create: true,
29
+ edit: true,
30
+ delete: true
31
+ };
32
+ } else {
33
+ const basePermissions = {
34
+ read: false,
35
+ create: false,
36
+ edit: false,
37
+ delete: false
38
+ };
39
+ return roles.map((role) => resolveCollectionRole(role, collection.id)).reduce(mergePermissions, basePermissions);
40
+ }
40
41
  }
41
- function ze(t, i) {
42
- const n = {
43
- read: t.isAdmin || t.defaultPermissions?.read,
44
- create: t.isAdmin || t.defaultPermissions?.create,
45
- edit: t.isAdmin || t.defaultPermissions?.edit,
46
- delete: t.isAdmin || t.defaultPermissions?.delete
42
+ function resolveCollectionRole(role, id) {
43
+ const basePermissions = {
44
+ read: (role.isAdmin || role.defaultPermissions?.read) ?? false,
45
+ create: (role.isAdmin || role.defaultPermissions?.create) ?? false,
46
+ edit: (role.isAdmin || role.defaultPermissions?.edit) ?? false,
47
+ delete: (role.isAdmin || role.defaultPermissions?.delete) ?? false
47
48
  };
48
- return t.collectionPermissions && t.collectionPermissions[i] ? ie(t.collectionPermissions[i], n) : t.defaultPermissions ? ie(t.defaultPermissions, n) : n;
49
+ const thisCollectionPermissions = role.collectionPermissions?.[id];
50
+ if (thisCollectionPermissions) {
51
+ return mergePermissions(thisCollectionPermissions, basePermissions);
52
+ } else if (role.defaultPermissions) {
53
+ return mergePermissions(role.defaultPermissions, basePermissions);
54
+ } else {
55
+ return basePermissions;
56
+ }
49
57
  }
50
- const ie = (t, i) => ({
51
- read: t.read || i.read,
52
- create: t.create || i.create,
53
- edit: t.edit || i.edit,
54
- delete: t.delete || i.delete
55
- });
56
- function Cn(t, i) {
57
- return t ? i.roles ? i.roles.map((n) => t.find((c) => c.id === n.id)).filter(Boolean) : [] : void 0;
58
+ const mergePermissions = (permA, permB) => {
59
+ return {
60
+ read: permA.read || permB.read,
61
+ create: permA.create || permB.create,
62
+ edit: permA.edit || permB.edit,
63
+ delete: permA.delete || permB.delete
64
+ };
65
+ };
66
+ function getUserRoles(roles, fireCMSUser) {
67
+ return !roles ? void 0 : fireCMSUser.roles ? fireCMSUser.roles.map((role) => roles.find((r) => r.id === role.id)).filter(Boolean) : [];
58
68
  }
59
- const Je = (t, i) => {
60
- const n = t.map((r) => r.id), c = i.map((r) => r.id);
61
- return n.length === i.length && n.every((r) => c.includes(r));
69
+ const areRolesEqual = (rolesA, rolesB) => {
70
+ const rolesAIds = rolesA.map((r) => r.id);
71
+ const rolesBIds = rolesB.map((r) => r.id);
72
+ return rolesAIds.length === rolesB.length && rolesAIds.every((role) => rolesBIds.includes(role));
62
73
  };
63
- function vn(t, i) {
64
- if (!i)
74
+ function cacheDelegatedLoginToken(projectId, delegatedToken) {
75
+ if (!delegatedToken) {
65
76
  return;
66
- const n = Ge(i), c = new Date(n.exp * 1e3);
67
- localStorage.setItem(`auth_token::${t}`, JSON.stringify({
68
- token: i,
69
- expiry: c
77
+ }
78
+ const data = parseJwt(delegatedToken);
79
+ const expiry = new Date(data.exp * 1e3);
80
+ localStorage.setItem(`auth_token::${projectId}`, JSON.stringify({
81
+ token: delegatedToken,
82
+ expiry
70
83
  }));
71
84
  }
72
- function wn(t) {
73
- const i = localStorage.getItem(`auth_token::${t}`);
74
- if (i) {
75
- const n = JSON.parse(i);
76
- if (n.expiry = new Date(n.expiry), n.expiry > /* @__PURE__ */ new Date())
77
- return n.token;
85
+ function getDelegatedLoginTokenFromCache(projectId) {
86
+ const entry = localStorage.getItem(`auth_token::${projectId}`);
87
+ if (entry) {
88
+ const data = JSON.parse(entry);
89
+ data.expiry = new Date(data.expiry);
90
+ if (data.expiry > /* @__PURE__ */ new Date()) {
91
+ return data.token;
92
+ }
78
93
  }
94
+ return void 0;
79
95
  }
80
- function yn() {
81
- for (let t = 0; t < localStorage.length; t++) {
82
- const i = localStorage.key(t);
83
- i?.startsWith("auth_token::") && localStorage.removeItem(i);
96
+ function clearDelegatedLoginTokensCache() {
97
+ for (let i = 0; i < localStorage.length; i++) {
98
+ const key = localStorage.key(i);
99
+ if (key?.startsWith("auth_token::")) {
100
+ localStorage.removeItem(key);
101
+ }
84
102
  }
85
103
  }
86
- function Ge(t) {
87
- if (!t)
104
+ function parseJwt(token) {
105
+ if (!token) {
88
106
  throw new Error("No JWT token");
89
- const n = t.split(".")[1].replace(/-/g, "+").replace(/_/g, "/"), c = decodeURIComponent(window.atob(n).split("").map(function(r) {
90
- return "%" + ("00" + r.charCodeAt(0).toString(16)).slice(-2);
107
+ }
108
+ const base64Url = token.split(".")[1];
109
+ const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
110
+ const jsonPayload = decodeURIComponent(window.atob(base64).split("").map(function(c2) {
111
+ return "%" + ("00" + c2.charCodeAt(0).toString(16)).slice(-2);
91
112
  }).join(""));
92
- return JSON.parse(c);
113
+ return JSON.parse(jsonPayload);
93
114
  }
94
- function bn(t, i = 10) {
95
- if (!/^#([0-9A-Fa-f]{3}){1,2}$/.test(t))
115
+ function darkenColor(hexColor, darkenBy = 10) {
116
+ if (!/^#([0-9A-Fa-f]{3}){1,2}$/.test(hexColor)) {
96
117
  throw new Error("Invalid color format");
97
- let n = t.substring(1).split("");
98
- n.length === 3 && (n = [n[0], n[0], n[1], n[1], n[2], n[2]]);
99
- let c = parseInt(n[0] + n[1], 16), r = parseInt(n[2] + n[3], 16), h = parseInt(n[4] + n[5], 16);
100
- return c = Math.floor(c * (1 - i / 100)), r = Math.floor(r * (1 - i / 100)), h = Math.floor(h * (1 - i / 100)), "#" + (c < 16 ? "0" : "") + c.toString(16) + (r < 16 ? "0" : "") + r.toString(16) + (h < 16 ? "0" : "") + h.toString(16);
118
+ }
119
+ let color = hexColor.substring(1).split("");
120
+ if (color.length === 3) {
121
+ color = [color[0], color[0], color[1], color[1], color[2], color[2]];
122
+ }
123
+ let r = parseInt(color[0] + color[1], 16);
124
+ let g = parseInt(color[2] + color[3], 16);
125
+ let b = parseInt(color[4] + color[5], 16);
126
+ r = Math.floor(r * (1 - darkenBy / 100));
127
+ g = Math.floor(g * (1 - darkenBy / 100));
128
+ b = Math.floor(b * (1 - darkenBy / 100));
129
+ return "#" + (r < 16 ? "0" : "") + r.toString(16) + (g < 16 ? "0" : "") + g.toString(16) + (b < 16 ? "0" : "") + b.toString(16);
101
130
  }
102
- function Nn(t, i = 10) {
103
- if (!/^#([0-9A-Fa-f]{3}){1,2}$/.test(t))
131
+ function hexToRgbaWithOpacity(hexColor, opacity = 10) {
132
+ if (!/^#([0-9A-Fa-f]{3}){1,2}$/.test(hexColor)) {
104
133
  throw new Error("Invalid color format");
105
- let n = t.substring(1).split("");
106
- n.length === 3 && (n = [n[0], n[0], n[1], n[1], n[2], n[2]]);
107
- const c = parseInt(n[0] + n[1], 16), r = parseInt(n[2] + n[3], 16), h = parseInt(n[4] + n[5], 16), w = i / 100;
108
- return `rgba(${c}, ${r}, ${h}, ${w})`;
134
+ }
135
+ let color = hexColor.substring(1).split("");
136
+ if (color.length === 3) {
137
+ color = [color[0], color[0], color[1], color[1], color[2], color[2]];
138
+ }
139
+ const r = parseInt(color[0] + color[1], 16);
140
+ const g = parseInt(color[2] + color[3], 16);
141
+ const b = parseInt(color[4] + color[5], 16);
142
+ const alpha = opacity / 100;
143
+ return `rgba(${r}, ${g}, ${b}, ${alpha})`;
109
144
  }
110
- function Rn({
111
- firebaseApp: t,
112
- usersPath: i = "__FIRECMS/config/users",
113
- rolesPath: n = "__FIRECMS/config/roles",
114
- usersLimit: c,
115
- canEditRoles: r = !0,
116
- allowDefaultRolesCreation: h,
117
- includeCollectionConfigPermissions: w
145
+ function useBuildUserManagement({
146
+ authController,
147
+ dataSourceDelegate,
148
+ roles: rolesProp,
149
+ usersPath = "__FIRECMS/config/users",
150
+ rolesPath = "__FIRECMS/config/roles",
151
+ allowDefaultRolesCreation,
152
+ includeCollectionConfigPermissions
118
153
  }) {
119
- const f = Ee(), [x, y] = A.useState(!0), [d, I] = A.useState(!0), [b, s] = A.useState([]), [C, D] = A.useState([]), p = C.map((m) => ({
120
- ...m,
121
- roles: b.filter((v) => m.roles?.includes(v.id))
122
- })), [z, L] = A.useState(), [g, u] = A.useState(), N = x || d;
123
- Z(() => {
124
- t && (f.current = ee(t));
125
- }, [t]), Z(() => {
126
- if (!t || !n)
127
- return;
128
- const m = ee(t);
129
- return ae(
130
- ne(m, n),
131
- {
132
- next: (v) => {
133
- L(void 0);
134
- try {
135
- const R = Ke(v.docs);
136
- s(R);
137
- } catch (R) {
138
- L(R);
154
+ if (!authController) {
155
+ throw Error("useBuildUserManagement: You need to provide an authController since version 3.0.0-beta.11. Check https://firecms.co/docs/pro/migrating_from_v3_beta");
156
+ }
157
+ const rolesDefinedInCode = (rolesProp ?? [])?.length > 0;
158
+ const [rolesLoading, setRolesLoading] = React.useState(!rolesDefinedInCode);
159
+ const [usersLoading, setUsersLoading] = React.useState(true);
160
+ const [roles, setRoles] = React.useState(rolesProp ?? []);
161
+ const [usersWithRoleIds, setUsersWithRoleIds] = React.useState([]);
162
+ const users = usersWithRoleIds.map((u) => ({
163
+ ...u,
164
+ roles: roles.filter((r) => u.roles?.includes(r.id))
165
+ }));
166
+ const [rolesError, setRolesError] = React.useState();
167
+ const [usersError, setUsersError] = React.useState();
168
+ const _usersLoading = usersLoading;
169
+ const _rolesLoading = rolesLoading;
170
+ const loading = _rolesLoading || _usersLoading;
171
+ useEffect(() => {
172
+ if (rolesDefinedInCode) return;
173
+ if (!dataSourceDelegate || !rolesPath) return;
174
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
175
+ if (authController?.initialLoading) return;
176
+ setRolesLoading(true);
177
+ return dataSourceDelegate.listenCollection?.({
178
+ path: rolesPath,
179
+ onUpdate(entities) {
180
+ setRolesError(void 0);
181
+ console.debug("Updating roles", entities);
182
+ try {
183
+ const newRoles = entityToRoles(entities);
184
+ if (!equal(newRoles, roles)) {
185
+ setRoles(newRoles);
139
186
  }
140
- y(!1);
141
- },
142
- error: (v) => {
143
- L(v), y(!1);
187
+ } catch (e) {
188
+ setRoles([]);
189
+ console.error("Error loading roles", e);
190
+ setRolesError(e);
144
191
  }
192
+ setRolesLoading(false);
193
+ },
194
+ onError(e_0) {
195
+ setRoles([]);
196
+ console.error("Error loading roles", e_0);
197
+ setRolesError(e_0);
198
+ setRolesLoading(false);
145
199
  }
146
- );
147
- }, [t, n]), Z(() => {
148
- if (!t || !i)
200
+ });
201
+ }, [rolesDefinedInCode, dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
202
+ useEffect(() => {
203
+ if (!dataSourceDelegate || !usersPath) return;
204
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) {
149
205
  return;
150
- const m = ee(t);
151
- return ae(
152
- ne(m, i),
153
- {
154
- next: (v) => {
155
- u(void 0);
156
- try {
157
- const R = He(v.docs);
158
- D(R);
159
- } catch (R) {
160
- u(R);
161
- }
162
- I(!1);
163
- },
164
- error: (v) => {
165
- u(v), I(!1);
206
+ }
207
+ if (authController?.initialLoading) {
208
+ return;
209
+ }
210
+ setUsersLoading(true);
211
+ return dataSourceDelegate.listenCollection?.({
212
+ path: usersPath,
213
+ onUpdate(entities_0) {
214
+ console.debug("Updating users", entities_0);
215
+ setUsersError(void 0);
216
+ try {
217
+ const newUsers = entitiesToUsers(entities_0);
218
+ setUsersWithRoleIds(newUsers);
219
+ } catch (e_1) {
220
+ setUsersWithRoleIds([]);
221
+ console.error("Error loading users", e_1);
222
+ setUsersError(e_1);
166
223
  }
224
+ setUsersLoading(false);
225
+ },
226
+ onError(e_2) {
227
+ console.error("Error loading users", e_2);
228
+ setUsersWithRoleIds([]);
229
+ setUsersError(e_2);
230
+ setUsersLoading(false);
167
231
  }
168
- );
169
- }, [t, i]);
170
- const U = k(async (m) => {
171
- const v = f.current;
172
- if (!v || !i)
173
- throw Error("useFirestoreConfigurationPersistence Firestore not initialised");
174
- console.debug("Persisting user", m);
175
- const R = m.roles?.map((ke) => ke.id), {
176
- uid: M,
177
- ...X
178
- } = m, se = {
179
- ...X,
180
- roles: R
232
+ });
233
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, usersPath]);
234
+ const saveUser = useCallback(async (user) => {
235
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
236
+ if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
237
+ console.debug("Persisting user", user);
238
+ const roleIds = user.roles?.map((r_0) => r_0.id);
239
+ const email = user.email?.toLowerCase().trim();
240
+ if (!email) throw Error("Email is required");
241
+ const userExists = users.find((u_0) => u_0.email?.toLowerCase() === email);
242
+ const data = {
243
+ ...user,
244
+ roles: roleIds ?? []
181
245
  };
182
- return M ? ce(G(v, i, M), se, { merge: !0 }).then(() => m) : Pe(ne(v, i), se).then(() => m);
183
- }, [i]), F = k((m) => {
184
- const v = f.current;
185
- if (!v || !n)
186
- throw Error("useFirestoreConfigurationPersistence Firestore not initialised");
187
- console.debug("Persisting role", m);
246
+ if (!userExists) {
247
+ data.created_on = /* @__PURE__ */ new Date();
248
+ }
249
+ if (userExists && userExists.uid !== user.uid) {
250
+ const entity = {
251
+ values: {},
252
+ path: usersPath,
253
+ id: userExists.uid
254
+ };
255
+ await dataSourceDelegate.deleteEntity({
256
+ entity
257
+ }).then(() => {
258
+ console.debug("Deleted previous user", userExists);
259
+ }).catch((e_3) => {
260
+ console.error("Error deleting user", e_3);
261
+ });
262
+ }
263
+ return dataSourceDelegate.saveEntity({
264
+ status: "existing",
265
+ path: usersPath,
266
+ entityId: email,
267
+ values: removeUndefined(data)
268
+ }).then(() => user);
269
+ }, [usersPath, dataSourceDelegate?.initialised]);
270
+ const saveRole = useCallback((role) => {
271
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
272
+ if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
273
+ console.debug("Persisting role", role);
188
274
  const {
189
- id: R,
190
- ...M
191
- } = m, X = G(v, n, R);
192
- return ce(X, M, { merge: !0 });
193
- }, [n]), o = k(async (m) => {
194
- const v = f.current;
195
- if (!v || !i)
196
- throw Error("useFirestoreConfigurationPersistence Firestore not initialised");
197
- console.debug("Deleting", m);
198
- const { uid: R } = m;
199
- return de(G(v, i, R));
200
- }, [i]), S = k((m) => {
201
- const v = f.current;
202
- if (!v || !n)
203
- throw Error("useFirestoreConfigurationPersistence Firestore not initialised");
204
- console.debug("Deleting", m);
205
- const { id: R } = m, M = G(v, n, R);
206
- return de(M);
207
- }, [n]), $ = k(({
208
- collection: m,
209
- user: v
210
- }) => je({
211
- collection: m,
212
- user: v
213
- }), []), J = p.map((m) => m.uid), Se = k((m) => {
214
- if (!p)
215
- throw Error("Users not loaded");
216
- return p.find((R) => R.email?.toLowerCase() === m?.email?.toLowerCase())?.roles;
217
- }, [J]), Ie = k(({ user: m }) => {
218
- if (console.log("Authenticating user", m), N)
219
- return console.log("User management is still loading"), !1;
220
- if (p.length === 0 || p.find((R) => R.email?.toLowerCase() === m?.email?.toLowerCase()))
221
- return !0;
222
- throw Error("Could not find a user with the provided email");
223
- }, [N, p]);
275
+ id,
276
+ ...roleData
277
+ } = role;
278
+ return dataSourceDelegate.saveEntity({
279
+ status: "existing",
280
+ path: rolesPath,
281
+ entityId: id,
282
+ values: removeUndefined(roleData)
283
+ }).then(() => {
284
+ return;
285
+ });
286
+ }, [rolesPath, dataSourceDelegate?.initialised]);
287
+ const deleteUser = useCallback(async (user_0) => {
288
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
289
+ if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
290
+ console.debug("Deleting", user_0);
291
+ const {
292
+ uid
293
+ } = user_0;
294
+ const entity_0 = {
295
+ path: usersPath,
296
+ id: uid,
297
+ values: {}
298
+ };
299
+ await dataSourceDelegate.deleteEntity({
300
+ entity: entity_0
301
+ });
302
+ }, [usersPath, dataSourceDelegate?.initialised]);
303
+ const deleteRole = useCallback(async (role_0) => {
304
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
305
+ if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
306
+ console.debug("Deleting", role_0);
307
+ const {
308
+ id: id_0
309
+ } = role_0;
310
+ const entity_1 = {
311
+ path: rolesPath,
312
+ id: id_0,
313
+ values: {}
314
+ };
315
+ await dataSourceDelegate.deleteEntity({
316
+ entity: entity_1
317
+ });
318
+ }, [rolesPath, dataSourceDelegate?.initialised]);
319
+ const collectionPermissions = useCallback(({
320
+ collection,
321
+ user: user_1
322
+ }) => resolveUserRolePermissions({
323
+ collection,
324
+ user: user_1
325
+ }), []);
326
+ const defineRolesFor = useCallback((user_2) => {
327
+ if (!usersWithRoleIds) throw Error("Users not loaded");
328
+ const users_0 = usersWithRoleIds.map((u_1) => ({
329
+ ...u_1,
330
+ roles: roles.filter((r_1) => u_1.roles?.includes(r_1.id))
331
+ }));
332
+ const mgmtUser = users_0.find((u_2) => u_2.email?.toLowerCase() === user_2?.email?.toLowerCase());
333
+ return mgmtUser?.roles;
334
+ }, [roles, usersWithRoleIds]);
335
+ const authenticator = useCallback(({
336
+ user: user_3
337
+ }) => {
338
+ if (loading) {
339
+ return false;
340
+ }
341
+ if (user_3 === null) {
342
+ console.warn("User is null, returning");
343
+ return false;
344
+ }
345
+ if (users.length === 0) {
346
+ console.warn("No users created yet");
347
+ return true;
348
+ }
349
+ const mgmtUser_0 = users.find((u_3) => u_3.email?.toLowerCase() === user_3?.email?.toLowerCase());
350
+ if (mgmtUser_0) {
351
+ if (mgmtUser_0.uid !== user_3.uid) {
352
+ console.warn("User uid has changed, updating user in user management system");
353
+ saveUser({
354
+ ...mgmtUser_0,
355
+ uid: user_3.uid
356
+ }).then(() => {
357
+ console.debug("User updated in user management system", mgmtUser_0);
358
+ }).catch((e_4) => {
359
+ console.error("Error updating user in user management system", e_4);
360
+ });
361
+ }
362
+ console.debug("User found in user management system", mgmtUser_0);
363
+ return true;
364
+ }
365
+ throw Error("Could not find a user with the provided email in the user management system.");
366
+ }, [loading, users]);
367
+ const userRoles = authController.user ? defineRolesFor(authController.user) : void 0;
368
+ const isAdmin = (userRoles ?? []).some((r_2) => r_2.id === "admin");
369
+ const userRoleIds = userRoles?.map((r_3) => r_3.id);
370
+ useEffect(() => {
371
+ console.debug("Setting user roles", {
372
+ userRoles,
373
+ roles
374
+ });
375
+ authController.setUserRoles?.(userRoles ?? []);
376
+ }, [userRoleIds]);
377
+ const getUser = useCallback((uid_0) => {
378
+ if (!users) return null;
379
+ const user_4 = users.find((u_4) => u_4.uid === uid_0);
380
+ return user_4 ?? null;
381
+ }, [users]);
224
382
  return {
225
- loading: N,
226
- roles: b,
227
- users: p,
228
- saveUser: U,
229
- saveRole: F,
230
- deleteUser: o,
231
- deleteRole: S,
232
- usersLimit: c,
233
- canEditRoles: r === void 0 ? !0 : r,
234
- allowDefaultRolesCreation: h === void 0 ? !0 : h,
235
- includeCollectionConfigPermissions: !!w,
236
- collectionPermissions: $,
237
- defineRolesFor: Se,
238
- authenticator: Ie
383
+ loading,
384
+ roles,
385
+ users,
386
+ saveUser,
387
+ saveRole,
388
+ rolesError,
389
+ deleteUser,
390
+ deleteRole,
391
+ usersError,
392
+ isAdmin,
393
+ allowDefaultRolesCreation: allowDefaultRolesCreation === void 0 ? true : allowDefaultRolesCreation,
394
+ includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
395
+ collectionPermissions,
396
+ defineRolesFor,
397
+ authenticator,
398
+ ...authController,
399
+ initialLoading: authController.initialLoading || loading,
400
+ userRoles,
401
+ getUser,
402
+ user: authController.user ? {
403
+ ...authController.user,
404
+ roles: userRoles
405
+ } : null
239
406
  };
240
407
  }
241
- const He = (t) => t.map((i) => {
242
- const n = i.data();
243
- return {
244
- uid: i.id,
245
- ...n,
246
- created_on: n?.created_on?.toDate(),
247
- updated_on: n?.updated_on?.toDate()
248
- };
249
- }), Ke = (t) => t.map((i) => ({
250
- id: i.id,
251
- ...i.data()
252
- })), Ue = A.createContext({});
253
- function Qe({
254
- children: t,
255
- userManagement: i
256
- }) {
257
- return /* @__PURE__ */ e(Ue.Provider, { value: i, children: t });
408
+ const entitiesToUsers = (docs) => {
409
+ return docs.map((doc) => {
410
+ const data = doc.values;
411
+ const newVar = {
412
+ uid: doc.id,
413
+ ...data,
414
+ created_on: data?.created_on,
415
+ updated_on: data?.updated_on
416
+ };
417
+ return newVar;
418
+ });
419
+ };
420
+ const entityToRoles = (entities) => {
421
+ return entities.map((doc) => ({
422
+ id: doc.id,
423
+ ...doc.values
424
+ }));
425
+ };
426
+ const UserManagementContext = React.createContext({});
427
+ function UserManagementProvider(t0) {
428
+ const $ = c(3);
429
+ const {
430
+ children,
431
+ userManagement
432
+ } = t0;
433
+ let t1;
434
+ if ($[0] !== children || $[1] !== userManagement) {
435
+ t1 = /* @__PURE__ */ jsx(UserManagementContext.Provider, { value: userManagement, children });
436
+ $[0] = children;
437
+ $[1] = userManagement;
438
+ $[2] = t1;
439
+ } else {
440
+ t1 = $[2];
441
+ }
442
+ return t1;
258
443
  }
259
- const W = () => Ae(Ue);
260
- function Q({ role: t }) {
261
- let i;
262
- return t.isAdmin ? i = "blueDarker" : t.id === "editor" ? i = "yellowLight" : t.id === "viewer" ? i = "grayLight" : i = Fe(t.id), /* @__PURE__ */ e(
263
- Te,
264
- {
265
- colorScheme: i,
266
- children: t.name
267
- },
268
- t.id
269
- );
444
+ const useUserManagement = () => {
445
+ return useContext(UserManagementContext);
446
+ };
447
+ function RoleChip(t0) {
448
+ const $ = c(6);
449
+ const {
450
+ role
451
+ } = t0;
452
+ let colorScheme;
453
+ if (role.isAdmin) {
454
+ colorScheme = "blueDarker";
455
+ } else {
456
+ if (role.id === "editor") {
457
+ colorScheme = "yellowLight";
458
+ } else {
459
+ if (role.id === "viewer") {
460
+ colorScheme = "grayLight";
461
+ } else {
462
+ let t12;
463
+ if ($[0] !== role.id) {
464
+ t12 = getColorSchemeForSeed(role.id);
465
+ $[0] = role.id;
466
+ $[1] = t12;
467
+ } else {
468
+ t12 = $[1];
469
+ }
470
+ colorScheme = t12;
471
+ }
472
+ }
473
+ }
474
+ let t1;
475
+ if ($[2] !== colorScheme || $[3] !== role.id || $[4] !== role.name) {
476
+ t1 = /* @__PURE__ */ jsx(Chip, { colorScheme, children: role.name }, role.id);
477
+ $[2] = colorScheme;
478
+ $[3] = role.id;
479
+ $[4] = role.name;
480
+ $[5] = t1;
481
+ } else {
482
+ t1 = $[5];
483
+ }
484
+ return t1;
270
485
  }
271
- const Xe = _.object().shape({
272
- id: _.string().required("Required"),
273
- name: _.string().required("Required")
486
+ const RoleYupSchema = Yup.object().shape({
487
+ id: Yup.string().required("Required"),
488
+ name: Yup.string().required("Required")
274
489
  });
275
- function Ze({
276
- open: t,
277
- role: i,
278
- editable: n,
279
- handleClose: c,
280
- collections: r
281
- }) {
282
- const { saveRole: h } = W(), w = !i, [f, x] = P(), y = k((o) => (x(void 0), h(o)), [h]), d = xe({
283
- initialValues: i ?? {
490
+ function canRoleBeEdited(loggedUser) {
491
+ const loggedUserIsAdmin = loggedUser.roles?.map((r) => r.id).includes("admin");
492
+ if (!loggedUserIsAdmin) {
493
+ throw new Error("Only admins can edit roles");
494
+ }
495
+ return true;
496
+ }
497
+ function RolesDetailsForm(t0) {
498
+ const $ = c(164);
499
+ const {
500
+ open,
501
+ role,
502
+ editable,
503
+ handleClose,
504
+ collections
505
+ } = t0;
506
+ const {
507
+ saveRole
508
+ } = useUserManagement();
509
+ const isNewRole = !role;
510
+ const {
511
+ user: loggedInUser
512
+ } = useAuthController();
513
+ const [savingError, setSavingError] = useState();
514
+ let t1;
515
+ if ($[0] !== loggedInUser || $[1] !== saveRole) {
516
+ t1 = (role_0) => {
517
+ setSavingError(void 0);
518
+ if (!loggedInUser) {
519
+ throw new Error("User not found");
520
+ }
521
+ canRoleBeEdited(loggedInUser);
522
+ return saveRole(role_0);
523
+ };
524
+ $[0] = loggedInUser;
525
+ $[1] = saveRole;
526
+ $[2] = t1;
527
+ } else {
528
+ t1 = $[2];
529
+ }
530
+ const onRoleUpdated = t1;
531
+ let t2;
532
+ if ($[3] !== role) {
533
+ t2 = role ?? {
284
534
  name: ""
285
- },
286
- onSubmit: (o, S) => y(o).then(() => {
287
- S.resetForm({
288
- values: o
289
- }), c();
290
- }).catch(($) => x($)),
291
- validation: (o) => Xe.validate(o, { abortEarly: !1 }).then(() => ({})).catch((S) => {
292
- const $ = {};
293
- return S.inner.forEach((J) => {
294
- $[J.path] = J.message;
295
- }), $;
296
- })
297
- }), {
298
- isSubmitting: I,
299
- touched: b,
300
- values: s,
301
- errors: C,
302
- handleChange: D,
303
- setFieldValue: p,
304
- dirty: z,
305
- setFieldTouched: L
306
- } = d, g = s.isAdmin ?? !1, u = s.defaultPermissions?.create ?? !1, N = s.defaultPermissions?.read ?? !1, U = s.defaultPermissions?.edit ?? !1, F = s.defaultPermissions?.delete ?? !1;
307
- return A.useEffect(() => {
308
- !j(b, "id") && s.name && p("id", _e(s.name));
309
- }, [b, s.name]), /* @__PURE__ */ e(
310
- ue,
311
- {
312
- open: t,
313
- maxWidth: "4xl",
314
- children: /* @__PURE__ */ e(De, { value: d, children: /* @__PURE__ */ l(
315
- "form",
316
- {
317
- noValidate: !0,
318
- autoComplete: "off",
319
- onSubmit: d.handleSubmit,
320
- style: {
321
- display: "flex",
322
- flexDirection: "column",
323
- position: "relative",
324
- height: "100%"
325
- },
326
- children: [
327
- /* @__PURE__ */ l(me, { className: "flex-grow", children: [
328
- /* @__PURE__ */ e(
329
- "div",
330
- {
331
- className: "flex flex-row pt-12 pb-8",
332
- children: /* @__PURE__ */ e(
333
- O,
334
- {
335
- variant: "h4",
336
- className: "flex-grow",
337
- children: "Role"
338
- }
339
- )
340
- }
341
- ),
342
- /* @__PURE__ */ l("div", { className: "grid grid-cols-12 gap-8", children: [
343
- /* @__PURE__ */ l("div", { className: "col-span-12 md:col-span-8", children: [
344
- /* @__PURE__ */ e(
345
- K,
346
- {
347
- name: "name",
348
- required: !0,
349
- error: b.name && !!C.name,
350
- value: s.name,
351
- disabled: g || !n,
352
- onChange: D,
353
- "aria-describedby": "name-helper-text",
354
- label: "Name"
355
- }
356
- ),
357
- /* @__PURE__ */ e(B, { children: b.name && C.name ? C.name : "Name of this role" })
358
- ] }),
359
- /* @__PURE__ */ l("div", { className: "col-span-12 md:col-span-4", children: [
360
- /* @__PURE__ */ e(
361
- K,
362
- {
363
- name: "id",
364
- required: !0,
365
- error: b.id && !!C.id,
366
- value: s.id,
367
- disabled: !w || !n,
368
- onChange: (o) => {
369
- D(o), L("id", !0);
370
- },
371
- "aria-describedby": "id-helper-text",
372
- label: "ID"
373
- }
374
- ),
375
- /* @__PURE__ */ e(B, { children: b.id && C.id ? C.id : "ID of this role" })
376
- ] }),
377
- /* @__PURE__ */ l("div", { className: "col-span-12", children: [
378
- /* @__PURE__ */ e(
379
- Be,
380
- {
381
- className: "bg-inherit",
382
- children: /* @__PURE__ */ l(re, { children: [
383
- /* @__PURE__ */ l(oe, { children: [
384
- /* @__PURE__ */ e(a, {}),
385
- /* @__PURE__ */ e(
386
- a,
387
- {
388
- align: "center",
389
- children: "Create entities"
390
- }
391
- ),
392
- /* @__PURE__ */ e(
393
- a,
394
- {
395
- align: "center",
396
- children: "Read entities"
397
- }
398
- ),
399
- /* @__PURE__ */ e(
400
- a,
401
- {
402
- align: "center",
403
- children: "Update entities"
404
- }
405
- ),
406
- /* @__PURE__ */ e(
407
- a,
408
- {
409
- align: "center",
410
- children: "Delete entities"
411
- }
412
- )
413
- ] }),
414
- /* @__PURE__ */ l(le, { children: [
415
- /* @__PURE__ */ l(q, { children: [
416
- /* @__PURE__ */ e(
417
- a,
418
- {
419
- scope: "row",
420
- children: /* @__PURE__ */ e("strong", { children: "All collections" })
421
- }
422
- ),
423
- /* @__PURE__ */ e(
424
- a,
425
- {
426
- align: "center",
427
- children: /* @__PURE__ */ e(
428
- V,
429
- {
430
- title: "Create entities in collections",
431
- children: /* @__PURE__ */ e(
432
- E,
433
- {
434
- disabled: g || !n,
435
- checked: (g || u) ?? !1,
436
- onCheckedChange: (o) => p("defaultPermissions.create", o)
437
- }
438
- )
439
- }
440
- )
441
- }
442
- ),
443
- /* @__PURE__ */ e(
444
- a,
445
- {
446
- align: "center",
447
- children: /* @__PURE__ */ e(
448
- V,
449
- {
450
- title: "Access all data in every collection",
451
- children: /* @__PURE__ */ e(
452
- E,
453
- {
454
- disabled: g || !n,
455
- checked: (g || N) ?? !1,
456
- onCheckedChange: (o) => p("defaultPermissions.read", o)
457
- }
458
- )
459
- }
460
- )
461
- }
462
- ),
463
- /* @__PURE__ */ e(
464
- a,
465
- {
466
- align: "center",
467
- children: /* @__PURE__ */ e(
468
- V,
469
- {
470
- title: "Update data in any collection",
471
- children: /* @__PURE__ */ e(
472
- E,
473
- {
474
- disabled: g || !n,
475
- checked: (g || U) ?? !1,
476
- onCheckedChange: (o) => p("defaultPermissions.edit", o)
477
- }
478
- )
479
- }
480
- )
481
- }
482
- ),
483
- /* @__PURE__ */ e(
484
- a,
485
- {
486
- align: "center",
487
- children: /* @__PURE__ */ e(
488
- V,
489
- {
490
- title: "Delete data in any collection",
491
- children: /* @__PURE__ */ e(
492
- E,
493
- {
494
- disabled: g || !n,
495
- checked: (g || F) ?? !1,
496
- onCheckedChange: (o) => p("defaultPermissions.delete", o)
497
- }
498
- )
499
- }
500
- )
501
- }
502
- )
503
- ] }),
504
- r && r.map((o) => /* @__PURE__ */ l(q, { children: [
505
- /* @__PURE__ */ e(
506
- a,
507
- {
508
- scope: "row",
509
- children: o.name
510
- }
511
- ),
512
- /* @__PURE__ */ e(
513
- a,
514
- {
515
- align: "center",
516
- children: /* @__PURE__ */ e(
517
- E,
518
- {
519
- disabled: g || u || !n,
520
- checked: (g || u || j(s, `collectionPermissions.${o.path}.create`)) ?? !1,
521
- onCheckedChange: (S) => p(`collectionPermissions.${o.path}.create`, S)
522
- }
523
- )
524
- }
525
- ),
526
- /* @__PURE__ */ e(
527
- a,
528
- {
529
- align: "center",
530
- children: /* @__PURE__ */ e(
531
- E,
532
- {
533
- disabled: g || N || !n,
534
- checked: (g || N || j(s, `collectionPermissions.${o.path}.read`)) ?? !1,
535
- onCheckedChange: (S) => p(`collectionPermissions.${o.path}.read`, S)
536
- }
537
- )
538
- }
539
- ),
540
- /* @__PURE__ */ e(
541
- a,
542
- {
543
- align: "center",
544
- children: /* @__PURE__ */ e(
545
- E,
546
- {
547
- disabled: g || U || !n,
548
- checked: (g || U || j(s, `collectionPermissions.${o.path}.edit`)) ?? !1,
549
- onCheckedChange: (S) => p(`collectionPermissions.${o.path}.edit`, S)
550
- }
551
- )
552
- }
553
- ),
554
- /* @__PURE__ */ e(
555
- a,
556
- {
557
- align: "center",
558
- children: /* @__PURE__ */ e(
559
- E,
560
- {
561
- disabled: g || F || !n,
562
- checked: (g || F || j(s, `collectionPermissions.${o.path}.delete`)) ?? !1,
563
- onCheckedChange: (S) => p(`collectionPermissions.${o.path}.delete`, S)
564
- }
565
- )
566
- }
567
- )
568
- ] }, o.name))
569
- ] })
570
- ] })
571
- }
572
- ),
573
- /* @__PURE__ */ e(B, { children: "You can customise the permissions that the users related to this role can perform in the entities of each collection" })
574
- ] }),
575
- /* @__PURE__ */ l("div", { className: "col-span-12 md:col-span-4", children: [
576
- /* @__PURE__ */ l(
577
- te,
578
- {
579
- error: b.config && !!C.config,
580
- id: "createCollections",
581
- name: "createCollections",
582
- label: "Create collections",
583
- position: "item-aligned",
584
- disabled: g || !n,
585
- onChange: (o) => p("config.createCollections", o.target.value === "true"),
586
- value: g || s.config?.createCollections ? "true" : "false",
587
- renderValue: (o) => o === "true" ? "Yes" : "No",
588
- children: [
589
- /* @__PURE__ */ e(
590
- T,
591
- {
592
- value: "true",
593
- children: " Yes "
594
- }
595
- ),
596
- /* @__PURE__ */ e(
597
- T,
598
- {
599
- value: "false",
600
- children: " No "
601
- }
602
- )
603
- ]
604
- }
605
- ),
606
- /* @__PURE__ */ e(B, { children: b.config && C.config ? C.config : "Can the user create collections" })
607
- ] }),
608
- /* @__PURE__ */ l("div", { className: "col-span-12 md:col-span-4", children: [
609
- /* @__PURE__ */ l(
610
- te,
611
- {
612
- error: b.config && !!C.config,
613
- id: "editCollections",
614
- name: "editCollections",
615
- label: "Edit collections",
616
- disabled: g || !n,
617
- position: "item-aligned",
618
- onChange: (o) => p("config.editCollections", o.target.value === "own" ? "own" : o.target.value === "true"),
619
- value: g ? "true" : s.config?.editCollections === "own" ? "own" : s.config?.editCollections ? "true" : "false",
620
- renderValue: (o) => o === "own" ? "Own" : o === "true" ? "Yes" : "No",
621
- children: [
622
- /* @__PURE__ */ e(
623
- T,
624
- {
625
- value: "true",
626
- children: " Yes "
627
- }
628
- ),
629
- /* @__PURE__ */ e(
630
- T,
631
- {
632
- value: "false",
633
- children: " No "
634
- }
635
- ),
636
- /* @__PURE__ */ e(
637
- T,
638
- {
639
- value: "own",
640
- children: " Only his/her own "
641
- }
642
- )
643
- ]
644
- }
645
- ),
646
- /* @__PURE__ */ e(B, { children: b.config && C.config ? C.config : "Can the user edit collections" })
647
- ] }),
648
- /* @__PURE__ */ l("div", { className: "col-span-12 md:col-span-4", children: [
649
- /* @__PURE__ */ l(
650
- te,
651
- {
652
- error: b.config && !!C.config,
653
- id: "deleteCollections",
654
- name: "deleteCollections",
655
- label: "Delete collections",
656
- disabled: g || !n,
657
- position: "item-aligned",
658
- onChange: (o) => p("config.deleteCollections", o.target.value === "own" ? "own" : o.target.value === "true"),
659
- value: g ? "true" : s.config?.deleteCollections === "own" ? "own" : s.config?.deleteCollections ? "true" : "false",
660
- renderValue: (o) => o === "own" ? "Own" : o === "true" ? "Yes" : "No",
661
- children: [
662
- /* @__PURE__ */ e(
663
- T,
664
- {
665
- value: "true",
666
- children: " Yes "
667
- }
668
- ),
669
- /* @__PURE__ */ e(
670
- T,
671
- {
672
- value: "false",
673
- children: " No "
674
- }
675
- ),
676
- /* @__PURE__ */ e(
677
- T,
678
- {
679
- value: "own",
680
- children: " Only his/her own "
681
- }
682
- )
683
- ]
684
- }
685
- ),
686
- /* @__PURE__ */ e(B, { children: b.config && C.config ? C.config : "Can the user delete collections" })
687
- ] })
688
- ] })
689
- ] }),
690
- /* @__PURE__ */ l(he, { position: "sticky", children: [
691
- f && /* @__PURE__ */ e(O, { className: "text-red-500", children: "There was an error saving this role" }),
692
- /* @__PURE__ */ e(
693
- Y,
694
- {
695
- variant: "text",
696
- onClick: () => {
697
- c();
698
- },
699
- children: "Cancel"
700
- }
701
- ),
702
- /* @__PURE__ */ e(
703
- fe,
704
- {
705
- variant: "filled",
706
- color: "primary",
707
- type: "submit",
708
- disabled: !z,
709
- loading: I,
710
- startIcon: /* @__PURE__ */ e(ge, {}),
711
- children: w ? "Create role" : "Update"
712
- }
713
- )
714
- ] })
715
- ]
716
- }
717
- ) })
718
- }
719
- );
535
+ };
536
+ $[3] = role;
537
+ $[4] = t2;
538
+ } else {
539
+ t2 = $[4];
540
+ }
541
+ let t3;
542
+ if ($[5] !== handleClose || $[6] !== onRoleUpdated) {
543
+ t3 = (role_1, formexController) => {
544
+ try {
545
+ return onRoleUpdated(role_1).then(() => {
546
+ formexController.resetForm({
547
+ values: role_1
548
+ });
549
+ handleClose();
550
+ }).catch((e_0) => {
551
+ setSavingError(e_0);
552
+ });
553
+ } catch (t410) {
554
+ const e = t410;
555
+ setSavingError(e);
556
+ return Promise.resolve();
557
+ }
558
+ };
559
+ $[5] = handleClose;
560
+ $[6] = onRoleUpdated;
561
+ $[7] = t3;
562
+ } else {
563
+ t3 = $[7];
564
+ }
565
+ let t4;
566
+ if ($[8] !== t2 || $[9] !== t3) {
567
+ t4 = {
568
+ initialValues: t2,
569
+ onSubmit: t3,
570
+ validation: _temp3$1
571
+ };
572
+ $[8] = t2;
573
+ $[9] = t3;
574
+ $[10] = t4;
575
+ } else {
576
+ t4 = $[10];
577
+ }
578
+ const formex = useCreateFormex(t4);
579
+ const {
580
+ isSubmitting,
581
+ touched,
582
+ values: values_0,
583
+ errors: errors_0,
584
+ handleChange,
585
+ setFieldValue,
586
+ dirty,
587
+ setFieldTouched
588
+ } = formex;
589
+ const isAdmin = values_0.isAdmin ?? false;
590
+ const defaultCreate = values_0.defaultPermissions?.create ?? false;
591
+ const defaultRead = values_0.defaultPermissions?.read ?? false;
592
+ const defaultEdit = values_0.defaultPermissions?.edit ?? false;
593
+ const defaultDelete = values_0.defaultPermissions?.delete ?? false;
594
+ let t5;
595
+ if ($[11] !== setFieldValue || $[12] !== touched || $[13] !== values_0.name) {
596
+ t5 = () => {
597
+ const idTouched = getIn(touched, "id");
598
+ if (!idTouched && values_0.name) {
599
+ setFieldValue("id", toSnakeCase(values_0.name));
600
+ }
601
+ };
602
+ $[11] = setFieldValue;
603
+ $[12] = touched;
604
+ $[13] = values_0.name;
605
+ $[14] = t5;
606
+ } else {
607
+ t5 = $[14];
608
+ }
609
+ let t6;
610
+ if ($[15] !== touched || $[16] !== values_0.name) {
611
+ t6 = [touched, values_0.name];
612
+ $[15] = touched;
613
+ $[16] = values_0.name;
614
+ $[17] = t6;
615
+ } else {
616
+ t6 = $[17];
617
+ }
618
+ React.useEffect(t5, t6);
619
+ let t7;
620
+ let t8;
621
+ if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
622
+ t7 = {
623
+ display: "flex",
624
+ flexDirection: "column",
625
+ position: "relative",
626
+ height: "100%"
627
+ };
628
+ t8 = /* @__PURE__ */ jsx(DialogTitle, { variant: "h4", gutterBottom: false, children: "Role" });
629
+ $[18] = t7;
630
+ $[19] = t8;
631
+ } else {
632
+ t7 = $[18];
633
+ t8 = $[19];
634
+ }
635
+ const t9 = touched.name && Boolean(errors_0.name);
636
+ const t10 = isAdmin || !editable;
637
+ let t11;
638
+ if ($[20] !== handleChange || $[21] !== t10 || $[22] !== t9 || $[23] !== values_0.name) {
639
+ t11 = /* @__PURE__ */ jsx(TextField, { name: "name", required: true, error: t9, value: values_0.name, disabled: t10, onChange: handleChange, "aria-describedby": "name-helper-text", label: "Name" });
640
+ $[20] = handleChange;
641
+ $[21] = t10;
642
+ $[22] = t9;
643
+ $[23] = values_0.name;
644
+ $[24] = t11;
645
+ } else {
646
+ t11 = $[24];
647
+ }
648
+ const t12 = touched.name && Boolean(errors_0.name) ? errors_0.name : "Name of this role";
649
+ let t13;
650
+ if ($[25] !== t12) {
651
+ t13 = /* @__PURE__ */ jsx(FieldCaption, { children: t12 });
652
+ $[25] = t12;
653
+ $[26] = t13;
654
+ } else {
655
+ t13 = $[26];
656
+ }
657
+ let t14;
658
+ if ($[27] !== t11 || $[28] !== t13) {
659
+ t14 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-8", children: [
660
+ t11,
661
+ t13
662
+ ] });
663
+ $[27] = t11;
664
+ $[28] = t13;
665
+ $[29] = t14;
666
+ } else {
667
+ t14 = $[29];
668
+ }
669
+ const t15 = touched.id && Boolean(errors_0.id);
670
+ const t16 = !isNewRole || !editable;
671
+ let t17;
672
+ if ($[30] !== handleChange || $[31] !== setFieldTouched) {
673
+ t17 = (e_2) => {
674
+ handleChange(e_2);
675
+ setFieldTouched("id", true);
676
+ };
677
+ $[30] = handleChange;
678
+ $[31] = setFieldTouched;
679
+ $[32] = t17;
680
+ } else {
681
+ t17 = $[32];
682
+ }
683
+ let t18;
684
+ if ($[33] !== t15 || $[34] !== t16 || $[35] !== t17 || $[36] !== values_0.id) {
685
+ t18 = /* @__PURE__ */ jsx(TextField, { name: "id", required: true, error: t15, value: values_0.id, disabled: t16, onChange: t17, "aria-describedby": "id-helper-text", label: "ID" });
686
+ $[33] = t15;
687
+ $[34] = t16;
688
+ $[35] = t17;
689
+ $[36] = values_0.id;
690
+ $[37] = t18;
691
+ } else {
692
+ t18 = $[37];
693
+ }
694
+ const t19 = touched.id && Boolean(errors_0.id) ? errors_0.id : "ID of this role";
695
+ let t20;
696
+ if ($[38] !== t19) {
697
+ t20 = /* @__PURE__ */ jsx(FieldCaption, { children: t19 });
698
+ $[38] = t19;
699
+ $[39] = t20;
700
+ } else {
701
+ t20 = $[39];
702
+ }
703
+ let t21;
704
+ if ($[40] !== t18 || $[41] !== t20) {
705
+ t21 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
706
+ t18,
707
+ t20
708
+ ] });
709
+ $[40] = t18;
710
+ $[41] = t20;
711
+ $[42] = t21;
712
+ } else {
713
+ t21 = $[42];
714
+ }
715
+ let t22;
716
+ if ($[43] === Symbol.for("react.memo_cache_sentinel")) {
717
+ t22 = /* @__PURE__ */ jsxs(TableHeader, { className: "rounded-md", children: [
718
+ /* @__PURE__ */ jsx(TableCell, {}),
719
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Create entities" }),
720
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Read entities" }),
721
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Update entities" }),
722
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Delete entities" }),
723
+ /* @__PURE__ */ jsx(TableCell, { align: "center" })
724
+ ] });
725
+ $[43] = t22;
726
+ } else {
727
+ t22 = $[43];
728
+ }
729
+ let t23;
730
+ if ($[44] === Symbol.for("react.memo_cache_sentinel")) {
731
+ t23 = /* @__PURE__ */ jsx(TableCell, { scope: "row", children: /* @__PURE__ */ jsx("strong", { children: "All collections" }) });
732
+ $[44] = t23;
733
+ } else {
734
+ t23 = $[44];
735
+ }
736
+ const t24 = isAdmin || !editable;
737
+ const t25 = (isAdmin || defaultCreate) ?? false;
738
+ let t26;
739
+ if ($[45] !== setFieldValue) {
740
+ t26 = (checked) => setFieldValue("defaultPermissions.create", checked);
741
+ $[45] = setFieldValue;
742
+ $[46] = t26;
743
+ } else {
744
+ t26 = $[46];
745
+ }
746
+ let t27;
747
+ if ($[47] !== t24 || $[48] !== t25 || $[49] !== t26) {
748
+ t27 = /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Tooltip, { title: "Create entities in collections", children: /* @__PURE__ */ jsx(Checkbox, { disabled: t24, checked: t25, onCheckedChange: t26 }) }) });
749
+ $[47] = t24;
750
+ $[48] = t25;
751
+ $[49] = t26;
752
+ $[50] = t27;
753
+ } else {
754
+ t27 = $[50];
755
+ }
756
+ const t28 = isAdmin || !editable;
757
+ const t29 = (isAdmin || defaultRead) ?? false;
758
+ let t30;
759
+ if ($[51] !== setFieldValue) {
760
+ t30 = (checked_0) => setFieldValue("defaultPermissions.read", checked_0);
761
+ $[51] = setFieldValue;
762
+ $[52] = t30;
763
+ } else {
764
+ t30 = $[52];
765
+ }
766
+ let t31;
767
+ if ($[53] !== t28 || $[54] !== t29 || $[55] !== t30) {
768
+ t31 = /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Tooltip, { title: "Access all data in every collection", children: /* @__PURE__ */ jsx(Checkbox, { disabled: t28, checked: t29, onCheckedChange: t30 }) }) });
769
+ $[53] = t28;
770
+ $[54] = t29;
771
+ $[55] = t30;
772
+ $[56] = t31;
773
+ } else {
774
+ t31 = $[56];
775
+ }
776
+ const t32 = isAdmin || !editable;
777
+ const t33 = (isAdmin || defaultEdit) ?? false;
778
+ let t34;
779
+ if ($[57] !== setFieldValue) {
780
+ t34 = (checked_1) => setFieldValue("defaultPermissions.edit", checked_1);
781
+ $[57] = setFieldValue;
782
+ $[58] = t34;
783
+ } else {
784
+ t34 = $[58];
785
+ }
786
+ let t35;
787
+ if ($[59] !== t32 || $[60] !== t33 || $[61] !== t34) {
788
+ t35 = /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Tooltip, { title: "Update data in any collection", children: /* @__PURE__ */ jsx(Checkbox, { disabled: t32, checked: t33, onCheckedChange: t34 }) }) });
789
+ $[59] = t32;
790
+ $[60] = t33;
791
+ $[61] = t34;
792
+ $[62] = t35;
793
+ } else {
794
+ t35 = $[62];
795
+ }
796
+ const t36 = isAdmin || !editable;
797
+ const t37 = (isAdmin || defaultDelete) ?? false;
798
+ let t38;
799
+ if ($[63] !== setFieldValue) {
800
+ t38 = (checked_2) => setFieldValue("defaultPermissions.delete", checked_2);
801
+ $[63] = setFieldValue;
802
+ $[64] = t38;
803
+ } else {
804
+ t38 = $[64];
805
+ }
806
+ let t39;
807
+ if ($[65] !== t36 || $[66] !== t37 || $[67] !== t38) {
808
+ t39 = /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Tooltip, { title: "Delete data in any collection", children: /* @__PURE__ */ jsx(Checkbox, { disabled: t36, checked: t37, onCheckedChange: t38 }) }) });
809
+ $[65] = t36;
810
+ $[66] = t37;
811
+ $[67] = t38;
812
+ $[68] = t39;
813
+ } else {
814
+ t39 = $[68];
815
+ }
816
+ let t40;
817
+ if ($[69] === Symbol.for("react.memo_cache_sentinel")) {
818
+ t40 = /* @__PURE__ */ jsx(TableCell, { align: "center" });
819
+ $[69] = t40;
820
+ } else {
821
+ t40 = $[69];
822
+ }
823
+ let t41;
824
+ if ($[70] !== t27 || $[71] !== t31 || $[72] !== t35 || $[73] !== t39) {
825
+ t41 = /* @__PURE__ */ jsxs(TableRow, { children: [
826
+ t23,
827
+ t27,
828
+ t31,
829
+ t35,
830
+ t39,
831
+ t40
832
+ ] });
833
+ $[70] = t27;
834
+ $[71] = t31;
835
+ $[72] = t35;
836
+ $[73] = t39;
837
+ $[74] = t41;
838
+ } else {
839
+ t41 = $[74];
840
+ }
841
+ let t42;
842
+ if ($[75] !== collections || $[76] !== defaultCreate || $[77] !== defaultDelete || $[78] !== defaultEdit || $[79] !== defaultRead || $[80] !== editable || $[81] !== isAdmin || $[82] !== setFieldValue || $[83] !== values_0) {
843
+ t42 = collections && collections.map((col) => /* @__PURE__ */ jsxs(TableRow, { children: [
844
+ /* @__PURE__ */ jsx(TableCell, { scope: "row", children: col.name }),
845
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Checkbox, { disabled: isAdmin || defaultCreate || !editable, checked: (isAdmin || defaultCreate || getIn(values_0, `collectionPermissions.${col.id}.create`)) ?? false, onCheckedChange: (checked_3) => setFieldValue(`collectionPermissions.${col.id}.create`, checked_3) }) }),
846
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Checkbox, { disabled: isAdmin || defaultRead || !editable, checked: (isAdmin || defaultRead || getIn(values_0, `collectionPermissions.${col.id}.read`)) ?? false, onCheckedChange: (checked_4) => setFieldValue(`collectionPermissions.${col.id}.read`, checked_4) }) }),
847
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Checkbox, { disabled: isAdmin || defaultEdit || !editable, checked: (isAdmin || defaultEdit || getIn(values_0, `collectionPermissions.${col.id}.edit`)) ?? false, onCheckedChange: (checked_5) => setFieldValue(`collectionPermissions.${col.id}.edit`, checked_5) }) }),
848
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Checkbox, { disabled: isAdmin || defaultDelete || !editable, checked: (isAdmin || defaultDelete || getIn(values_0, `collectionPermissions.${col.id}.delete`)) ?? false, onCheckedChange: (checked_6) => setFieldValue(`collectionPermissions.${col.id}.delete`, checked_6) }) }),
849
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Tooltip, { title: "Allow all permissions in this collections", children: /* @__PURE__ */ jsx(Button, { className: "color-inherit", onClick: () => {
850
+ setFieldValue(`collectionPermissions.${col.id}.create`, true);
851
+ setFieldValue(`collectionPermissions.${col.id}.read`, true);
852
+ setFieldValue(`collectionPermissions.${col.id}.edit`, true);
853
+ setFieldValue(`collectionPermissions.${col.id}.delete`, true);
854
+ }, disabled: isAdmin || !editable, variant: "text", children: "All" }) }) })
855
+ ] }, col.name));
856
+ $[75] = collections;
857
+ $[76] = defaultCreate;
858
+ $[77] = defaultDelete;
859
+ $[78] = defaultEdit;
860
+ $[79] = defaultRead;
861
+ $[80] = editable;
862
+ $[81] = isAdmin;
863
+ $[82] = setFieldValue;
864
+ $[83] = values_0;
865
+ $[84] = t42;
866
+ } else {
867
+ t42 = $[84];
868
+ }
869
+ let t43;
870
+ if ($[85] !== t41 || $[86] !== t42) {
871
+ t43 = /* @__PURE__ */ jsx(Paper, { className: "bg-inherit overflow-hidden", children: /* @__PURE__ */ jsxs(Table, { className: "w-full rounded-md", children: [
872
+ t22,
873
+ /* @__PURE__ */ jsxs(TableBody, { children: [
874
+ t41,
875
+ t42
876
+ ] })
877
+ ] }) });
878
+ $[85] = t41;
879
+ $[86] = t42;
880
+ $[87] = t43;
881
+ } else {
882
+ t43 = $[87];
883
+ }
884
+ let t44;
885
+ if ($[88] === Symbol.for("react.memo_cache_sentinel")) {
886
+ t44 = /* @__PURE__ */ jsx(FieldCaption, { children: "You can customise the permissions that the users related to this role can perform in the entities of each collection" });
887
+ $[88] = t44;
888
+ } else {
889
+ t44 = $[88];
890
+ }
891
+ let t45;
892
+ if ($[89] !== t43) {
893
+ t45 = /* @__PURE__ */ jsxs("div", { className: "col-span-12", children: [
894
+ t43,
895
+ t44
896
+ ] });
897
+ $[89] = t43;
898
+ $[90] = t45;
899
+ } else {
900
+ t45 = $[90];
901
+ }
902
+ const t46 = touched.config && Boolean(errors_0.config);
903
+ const t47 = isAdmin || !editable;
904
+ let t48;
905
+ if ($[91] !== setFieldValue) {
906
+ t48 = (event) => setFieldValue("config.createCollections", event.target.value === "true");
907
+ $[91] = setFieldValue;
908
+ $[92] = t48;
909
+ } else {
910
+ t48 = $[92];
911
+ }
912
+ const t49 = isAdmin || values_0.config?.createCollections ? "true" : "false";
913
+ let t50;
914
+ let t51;
915
+ if ($[93] === Symbol.for("react.memo_cache_sentinel")) {
916
+ t50 = /* @__PURE__ */ jsx(SelectItem, { value: "true", children: " Yes " });
917
+ t51 = /* @__PURE__ */ jsx(SelectItem, { value: "false", children: " No " });
918
+ $[93] = t50;
919
+ $[94] = t51;
920
+ } else {
921
+ t50 = $[93];
922
+ t51 = $[94];
923
+ }
924
+ let t52;
925
+ if ($[95] !== t46 || $[96] !== t47 || $[97] !== t48 || $[98] !== t49) {
926
+ t52 = /* @__PURE__ */ jsxs(Select, { error: t46, size: "large", fullWidth: true, id: "createCollections", name: "createCollections", label: "Create collections", position: "item-aligned", disabled: t47, onChange: t48, value: t49, renderValue: _temp4$1, children: [
927
+ t50,
928
+ t51
929
+ ] });
930
+ $[95] = t46;
931
+ $[96] = t47;
932
+ $[97] = t48;
933
+ $[98] = t49;
934
+ $[99] = t52;
935
+ } else {
936
+ t52 = $[99];
937
+ }
938
+ const t53 = touched.config && Boolean(errors_0.config) ? errors_0.config : "Can the user create collections";
939
+ let t54;
940
+ if ($[100] !== t53) {
941
+ t54 = /* @__PURE__ */ jsx(FieldCaption, { children: t53 });
942
+ $[100] = t53;
943
+ $[101] = t54;
944
+ } else {
945
+ t54 = $[101];
946
+ }
947
+ let t55;
948
+ if ($[102] !== t52 || $[103] !== t54) {
949
+ t55 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
950
+ t52,
951
+ t54
952
+ ] });
953
+ $[102] = t52;
954
+ $[103] = t54;
955
+ $[104] = t55;
956
+ } else {
957
+ t55 = $[104];
958
+ }
959
+ const t56 = touched.config && Boolean(errors_0.config);
960
+ const t57 = isAdmin || !editable;
961
+ let t58;
962
+ if ($[105] !== setFieldValue) {
963
+ t58 = (event_0) => setFieldValue("config.editCollections", event_0.target.value === "own" ? "own" : event_0.target.value === "true");
964
+ $[105] = setFieldValue;
965
+ $[106] = t58;
966
+ } else {
967
+ t58 = $[106];
968
+ }
969
+ const t59 = isAdmin ? "true" : values_0.config?.editCollections === "own" ? "own" : values_0.config?.editCollections ? "true" : "false";
970
+ let t60;
971
+ let t61;
972
+ let t62;
973
+ if ($[107] === Symbol.for("react.memo_cache_sentinel")) {
974
+ t60 = /* @__PURE__ */ jsx(SelectItem, { value: "true", children: " Yes " });
975
+ t61 = /* @__PURE__ */ jsx(SelectItem, { value: "false", children: " No " });
976
+ t62 = /* @__PURE__ */ jsx(SelectItem, { value: "own", children: " Only his/her own " });
977
+ $[107] = t60;
978
+ $[108] = t61;
979
+ $[109] = t62;
980
+ } else {
981
+ t60 = $[107];
982
+ t61 = $[108];
983
+ t62 = $[109];
984
+ }
985
+ let t63;
986
+ if ($[110] !== t56 || $[111] !== t57 || $[112] !== t58 || $[113] !== t59) {
987
+ t63 = /* @__PURE__ */ jsxs(Select, { size: "large", fullWidth: true, error: t56, id: "editCollections", name: "editCollections", label: "Edit collections", disabled: t57, position: "item-aligned", onChange: t58, value: t59, renderValue: _temp5$1, children: [
988
+ t60,
989
+ t61,
990
+ t62
991
+ ] });
992
+ $[110] = t56;
993
+ $[111] = t57;
994
+ $[112] = t58;
995
+ $[113] = t59;
996
+ $[114] = t63;
997
+ } else {
998
+ t63 = $[114];
999
+ }
1000
+ const t64 = touched.config && Boolean(errors_0.config) ? errors_0.config : "Can the user edit collections";
1001
+ let t65;
1002
+ if ($[115] !== t64) {
1003
+ t65 = /* @__PURE__ */ jsx(FieldCaption, { children: t64 });
1004
+ $[115] = t64;
1005
+ $[116] = t65;
1006
+ } else {
1007
+ t65 = $[116];
1008
+ }
1009
+ let t66;
1010
+ if ($[117] !== t63 || $[118] !== t65) {
1011
+ t66 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
1012
+ t63,
1013
+ t65
1014
+ ] });
1015
+ $[117] = t63;
1016
+ $[118] = t65;
1017
+ $[119] = t66;
1018
+ } else {
1019
+ t66 = $[119];
1020
+ }
1021
+ const t67 = touched.config && Boolean(errors_0.config);
1022
+ const t68 = isAdmin || !editable;
1023
+ let t69;
1024
+ if ($[120] !== setFieldValue) {
1025
+ t69 = (event_1) => setFieldValue("config.deleteCollections", event_1.target.value === "own" ? "own" : event_1.target.value === "true");
1026
+ $[120] = setFieldValue;
1027
+ $[121] = t69;
1028
+ } else {
1029
+ t69 = $[121];
1030
+ }
1031
+ const t70 = isAdmin ? "true" : values_0.config?.deleteCollections === "own" ? "own" : values_0.config?.deleteCollections ? "true" : "false";
1032
+ let t71;
1033
+ let t72;
1034
+ let t73;
1035
+ if ($[122] === Symbol.for("react.memo_cache_sentinel")) {
1036
+ t71 = /* @__PURE__ */ jsx(SelectItem, { value: "true", children: " Yes " });
1037
+ t72 = /* @__PURE__ */ jsx(SelectItem, { value: "false", children: " No " });
1038
+ t73 = /* @__PURE__ */ jsx(SelectItem, { value: "own", children: " Only his/her own " });
1039
+ $[122] = t71;
1040
+ $[123] = t72;
1041
+ $[124] = t73;
1042
+ } else {
1043
+ t71 = $[122];
1044
+ t72 = $[123];
1045
+ t73 = $[124];
1046
+ }
1047
+ let t74;
1048
+ if ($[125] !== t67 || $[126] !== t68 || $[127] !== t69 || $[128] !== t70) {
1049
+ t74 = /* @__PURE__ */ jsxs(Select, { size: "large", fullWidth: true, error: t67, id: "deleteCollections", name: "deleteCollections", label: "Delete collections", disabled: t68, position: "item-aligned", onChange: t69, value: t70, renderValue: _temp6$1, children: [
1050
+ t71,
1051
+ t72,
1052
+ t73
1053
+ ] });
1054
+ $[125] = t67;
1055
+ $[126] = t68;
1056
+ $[127] = t69;
1057
+ $[128] = t70;
1058
+ $[129] = t74;
1059
+ } else {
1060
+ t74 = $[129];
1061
+ }
1062
+ const t75 = touched.config && Boolean(errors_0.config) ? errors_0.config : "Can the user delete collections";
1063
+ let t76;
1064
+ if ($[130] !== t75) {
1065
+ t76 = /* @__PURE__ */ jsx(FieldCaption, { children: t75 });
1066
+ $[130] = t75;
1067
+ $[131] = t76;
1068
+ } else {
1069
+ t76 = $[131];
1070
+ }
1071
+ let t77;
1072
+ if ($[132] !== t74 || $[133] !== t76) {
1073
+ t77 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
1074
+ t74,
1075
+ t76
1076
+ ] });
1077
+ $[132] = t74;
1078
+ $[133] = t76;
1079
+ $[134] = t77;
1080
+ } else {
1081
+ t77 = $[134];
1082
+ }
1083
+ let t78;
1084
+ if ($[135] !== t14 || $[136] !== t21 || $[137] !== t45 || $[138] !== t55 || $[139] !== t66 || $[140] !== t77) {
1085
+ t78 = /* @__PURE__ */ jsx(DialogContent, { className: "flex-grow", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-12 gap-4", children: [
1086
+ t14,
1087
+ t21,
1088
+ t45,
1089
+ t55,
1090
+ t66,
1091
+ t77
1092
+ ] }) });
1093
+ $[135] = t14;
1094
+ $[136] = t21;
1095
+ $[137] = t45;
1096
+ $[138] = t55;
1097
+ $[139] = t66;
1098
+ $[140] = t77;
1099
+ $[141] = t78;
1100
+ } else {
1101
+ t78 = $[141];
1102
+ }
1103
+ let t79;
1104
+ if ($[142] !== savingError) {
1105
+ t79 = savingError && /* @__PURE__ */ jsx(Typography, { className: "text-red-500 dark:text-red-500", children: savingError.message ?? "There was an error saving this role" });
1106
+ $[142] = savingError;
1107
+ $[143] = t79;
1108
+ } else {
1109
+ t79 = $[143];
1110
+ }
1111
+ let t80;
1112
+ if ($[144] !== handleClose) {
1113
+ t80 = /* @__PURE__ */ jsx(Button, { variant: "text", color: "primary", onClick: () => {
1114
+ handleClose();
1115
+ }, children: "Cancel" });
1116
+ $[144] = handleClose;
1117
+ $[145] = t80;
1118
+ } else {
1119
+ t80 = $[145];
1120
+ }
1121
+ const t81 = !dirty;
1122
+ const t82 = isNewRole ? "Create role" : "Update";
1123
+ let t83;
1124
+ if ($[146] !== isSubmitting || $[147] !== t81 || $[148] !== t82) {
1125
+ t83 = /* @__PURE__ */ jsx(LoadingButton, { variant: "filled", color: "primary", type: "submit", disabled: t81, loading: isSubmitting, children: t82 });
1126
+ $[146] = isSubmitting;
1127
+ $[147] = t81;
1128
+ $[148] = t82;
1129
+ $[149] = t83;
1130
+ } else {
1131
+ t83 = $[149];
1132
+ }
1133
+ let t84;
1134
+ if ($[150] !== t79 || $[151] !== t80 || $[152] !== t83) {
1135
+ t84 = /* @__PURE__ */ jsxs(DialogActions, { position: "sticky", children: [
1136
+ t79,
1137
+ t80,
1138
+ t83
1139
+ ] });
1140
+ $[150] = t79;
1141
+ $[151] = t80;
1142
+ $[152] = t83;
1143
+ $[153] = t84;
1144
+ } else {
1145
+ t84 = $[153];
1146
+ }
1147
+ let t85;
1148
+ if ($[154] !== formex.handleSubmit || $[155] !== t78 || $[156] !== t84) {
1149
+ t85 = /* @__PURE__ */ jsxs("form", { noValidate: true, autoComplete: "off", onSubmit: formex.handleSubmit, style: t7, children: [
1150
+ t8,
1151
+ t78,
1152
+ t84
1153
+ ] });
1154
+ $[154] = formex.handleSubmit;
1155
+ $[155] = t78;
1156
+ $[156] = t84;
1157
+ $[157] = t85;
1158
+ } else {
1159
+ t85 = $[157];
1160
+ }
1161
+ let t86;
1162
+ if ($[158] !== formex || $[159] !== t85) {
1163
+ t86 = /* @__PURE__ */ jsx(Formex, { value: formex, children: t85 });
1164
+ $[158] = formex;
1165
+ $[159] = t85;
1166
+ $[160] = t86;
1167
+ } else {
1168
+ t86 = $[160];
1169
+ }
1170
+ let t87;
1171
+ if ($[161] !== open || $[162] !== t86) {
1172
+ t87 = /* @__PURE__ */ jsx(Dialog, { open, maxWidth: "4xl", children: t86 });
1173
+ $[161] = open;
1174
+ $[162] = t86;
1175
+ $[163] = t87;
1176
+ } else {
1177
+ t87 = $[163];
1178
+ }
1179
+ return t87;
720
1180
  }
721
- const en = [
722
- {
723
- id: "admin",
724
- name: "Admin",
725
- isAdmin: !0
726
- },
727
- {
728
- id: "editor",
729
- name: "Editor",
730
- isAdmin: !1,
731
- defaultPermissions: {
732
- read: !0,
733
- create: !0,
734
- edit: !0,
735
- delete: !0
736
- },
737
- config: {
738
- createCollections: !0,
739
- editCollections: "own",
740
- deleteCollections: "own"
741
- }
1181
+ function _temp6$1(value_1) {
1182
+ return value_1 === "own" ? "Own" : value_1 === "true" ? "Yes" : "No";
1183
+ }
1184
+ function _temp5$1(value_0) {
1185
+ return value_0 === "own" ? "Own" : value_0 === "true" ? "Yes" : "No";
1186
+ }
1187
+ function _temp4$1(value) {
1188
+ return value === "true" ? "Yes" : "No";
1189
+ }
1190
+ function _temp3$1(values) {
1191
+ return RoleYupSchema.validate(values, {
1192
+ abortEarly: false
1193
+ }).then(_temp$3).catch(_temp2$1);
1194
+ }
1195
+ function _temp2$1(e_1) {
1196
+ const errors = {};
1197
+ e_1.inner.forEach((error) => {
1198
+ errors[error.path] = error.message;
1199
+ });
1200
+ return errors;
1201
+ }
1202
+ function _temp$3() {
1203
+ return {};
1204
+ }
1205
+ const DEFAULT_ROLES = [{
1206
+ id: "admin",
1207
+ name: "Admin",
1208
+ isAdmin: true
1209
+ }, {
1210
+ id: "editor",
1211
+ name: "Editor",
1212
+ isAdmin: false,
1213
+ defaultPermissions: {
1214
+ read: true,
1215
+ create: true,
1216
+ edit: true,
1217
+ delete: true
742
1218
  },
743
- {
744
- id: "viewer",
745
- name: "Viewer",
746
- isAdmin: !1,
747
- defaultPermissions: {
748
- read: !0,
749
- create: !1,
750
- edit: !1,
751
- delete: !1
752
- }
1219
+ config: {
1220
+ createCollections: true,
1221
+ editCollections: "own",
1222
+ deleteCollections: "own"
753
1223
  }
754
- ];
755
- function nn({
756
- onRoleClicked: t,
757
- editable: i
758
- }) {
1224
+ }, {
1225
+ id: "viewer",
1226
+ name: "Viewer",
1227
+ isAdmin: false,
1228
+ defaultPermissions: {
1229
+ read: true,
1230
+ create: false,
1231
+ edit: false,
1232
+ delete: false
1233
+ }
1234
+ }];
1235
+ function RolesTable(t0) {
1236
+ const $ = c(25);
759
1237
  const {
760
- roles: n,
761
- saveRole: c,
762
- deleteRole: r,
763
- allowDefaultRolesCreation: h
764
- } = W(), [w, f] = P(void 0), [x, y] = P(!1);
765
- return /* @__PURE__ */ l(
766
- "div",
767
- {
768
- className: "w-full overflow-auto",
769
- children: [
770
- /* @__PURE__ */ l(re, { children: [
771
- /* @__PURE__ */ l(oe, { children: [
772
- /* @__PURE__ */ e(a, { header: !0, className: "w-16" }),
773
- /* @__PURE__ */ e(a, { header: !0, children: "Role" }),
774
- /* @__PURE__ */ e(a, { header: !0, className: "items-center", children: "Is Admin" }),
775
- /* @__PURE__ */ e(a, { header: !0, children: "Default permissions" })
776
- ] }),
777
- /* @__PURE__ */ l(le, { children: [
778
- n && n.map((d) => {
779
- const I = d.isAdmin || d.defaultPermissions?.create, b = d.isAdmin || d.defaultPermissions?.read, s = d.isAdmin || d.defaultPermissions?.edit, C = d.isAdmin || d.defaultPermissions?.delete;
780
- return /* @__PURE__ */ l(
781
- q,
782
- {
783
- onClick: () => {
784
- t(d);
785
- },
786
- children: [
787
- /* @__PURE__ */ e(a, { style: { width: "64px" }, children: !d.isAdmin && /* @__PURE__ */ e(V, { title: "Delete this role", children: /* @__PURE__ */ e(
788
- pe,
789
- {
790
- size: "small",
791
- disabled: !i,
792
- onClick: (D) => (D.stopPropagation(), f(d)),
793
- children: /* @__PURE__ */ e(Ce, {})
794
- }
795
- ) }) }),
796
- /* @__PURE__ */ e(a, { children: /* @__PURE__ */ e(Q, { role: d }) }),
797
- /* @__PURE__ */ e(a, { className: "items-center", children: /* @__PURE__ */ e(E, { checked: d.isAdmin ?? !1 }) }),
798
- /* @__PURE__ */ e(a, { children: /* @__PURE__ */ l("ul", { children: [
799
- I && /* @__PURE__ */ e("li", { children: "Create" }),
800
- b && /* @__PURE__ */ e("li", { children: "Read" }),
801
- s && /* @__PURE__ */ e("li", { children: "Update" }),
802
- C && /* @__PURE__ */ e("li", { children: "Delete" })
803
- ] }) })
804
- ]
805
- },
806
- d.name
807
- );
808
- }),
809
- (!n || n.length === 0) && /* @__PURE__ */ e(q, { children: /* @__PURE__ */ e(a, { colspan: 4, children: /* @__PURE__ */ l(ve, { className: "flex flex-col gap-4 my-8 items-center", children: [
810
- /* @__PURE__ */ e(O, { variant: "label", children: "You don't have any roles yet." }),
811
- h && /* @__PURE__ */ e(
812
- Y,
813
- {
814
- variant: "outlined",
815
- onClick: () => {
816
- en.forEach((d) => {
817
- c(d);
818
- });
819
- },
820
- children: "Create default roles"
821
- }
822
- )
823
- ] }) }) })
824
- ] })
825
- ] }),
826
- /* @__PURE__ */ e(
827
- be,
828
- {
829
- open: !!w,
830
- loading: x,
831
- onAccept: () => {
832
- w && (y(!0), r(w).then(() => {
833
- f(void 0);
834
- }).finally(() => {
835
- y(!1);
836
- }));
837
- },
838
- onCancel: () => {
839
- f(void 0);
840
- },
841
- title: /* @__PURE__ */ e(H, { children: "Delete?" }),
842
- body: /* @__PURE__ */ e(H, { children: "Are you sure you want to delete this role?" })
843
- }
844
- )
845
- ]
846
- }
847
- );
1238
+ onRoleClicked,
1239
+ editable
1240
+ } = t0;
1241
+ const {
1242
+ roles,
1243
+ saveRole,
1244
+ deleteRole,
1245
+ allowDefaultRolesCreation
1246
+ } = useUserManagement();
1247
+ const [roleToBeDeleted, setRoleToBeDeleted] = useState(void 0);
1248
+ const [deleteInProgress, setDeleteInProgress] = useState(false);
1249
+ let t1;
1250
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
1251
+ t1 = /* @__PURE__ */ jsxs(TableHeader, { children: [
1252
+ /* @__PURE__ */ jsx(TableCell, { header: true, className: "w-16" }),
1253
+ /* @__PURE__ */ jsx(TableCell, { header: true, children: "Role" }),
1254
+ /* @__PURE__ */ jsx(TableCell, { header: true, className: "items-center", children: "Is Admin" }),
1255
+ /* @__PURE__ */ jsx(TableCell, { header: true, children: "Default permissions" })
1256
+ ] });
1257
+ $[0] = t1;
1258
+ } else {
1259
+ t1 = $[0];
1260
+ }
1261
+ let t2;
1262
+ if ($[1] !== editable || $[2] !== onRoleClicked || $[3] !== roles) {
1263
+ t2 = roles && roles.map((role) => {
1264
+ const canCreateAll = role.isAdmin || role.defaultPermissions?.create;
1265
+ const canReadAll = role.isAdmin || role.defaultPermissions?.read;
1266
+ const canUpdateAll = role.isAdmin || role.defaultPermissions?.edit;
1267
+ const canDeleteAll = role.isAdmin || role.defaultPermissions?.delete;
1268
+ return /* @__PURE__ */ jsxs(TableRow, { onClick: () => {
1269
+ onRoleClicked(role);
1270
+ }, children: [
1271
+ /* @__PURE__ */ jsx(TableCell, { style: {
1272
+ width: "64px"
1273
+ }, children: !role.isAdmin && /* @__PURE__ */ jsx(Tooltip, { asChild: true, title: "Delete this role", children: /* @__PURE__ */ jsx(IconButton, { size: "small", disabled: !editable, onClick: (event) => {
1274
+ event.stopPropagation();
1275
+ return setRoleToBeDeleted(role);
1276
+ }, children: /* @__PURE__ */ jsx(DeleteIcon, {}) }) }) }),
1277
+ /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsx(RoleChip, { role }) }),
1278
+ /* @__PURE__ */ jsx(TableCell, { className: "items-center", children: /* @__PURE__ */ jsx(Checkbox, { checked: role.isAdmin ?? false }) }),
1279
+ /* @__PURE__ */ jsx(TableCell, { children: /* @__PURE__ */ jsxs("ul", { children: [
1280
+ canCreateAll && /* @__PURE__ */ jsx("li", { children: "Create" }),
1281
+ canReadAll && /* @__PURE__ */ jsx("li", { children: "Read" }),
1282
+ canUpdateAll && /* @__PURE__ */ jsx("li", { children: "Update" }),
1283
+ canDeleteAll && /* @__PURE__ */ jsx("li", { children: "Delete" })
1284
+ ] }) })
1285
+ ] }, role.name);
1286
+ });
1287
+ $[1] = editable;
1288
+ $[2] = onRoleClicked;
1289
+ $[3] = roles;
1290
+ $[4] = t2;
1291
+ } else {
1292
+ t2 = $[4];
1293
+ }
1294
+ let t3;
1295
+ if ($[5] !== allowDefaultRolesCreation || $[6] !== roles || $[7] !== saveRole) {
1296
+ t3 = (!roles || roles.length === 0) && /* @__PURE__ */ jsx(TableRow, { children: /* @__PURE__ */ jsx(TableCell, { colspan: 4, children: /* @__PURE__ */ jsxs(CenteredView, { className: "flex flex-col gap-4 my-8 items-center", children: [
1297
+ /* @__PURE__ */ jsx(Typography, { variant: "label", children: "You don't have any roles yet." }),
1298
+ allowDefaultRolesCreation && /* @__PURE__ */ jsx(Button, { variant: "outlined", onClick: () => {
1299
+ DEFAULT_ROLES.forEach((role_0) => {
1300
+ saveRole(role_0);
1301
+ });
1302
+ }, children: "Create default roles" })
1303
+ ] }) }) });
1304
+ $[5] = allowDefaultRolesCreation;
1305
+ $[6] = roles;
1306
+ $[7] = saveRole;
1307
+ $[8] = t3;
1308
+ } else {
1309
+ t3 = $[8];
1310
+ }
1311
+ let t4;
1312
+ if ($[9] !== t2 || $[10] !== t3) {
1313
+ t4 = /* @__PURE__ */ jsxs(Table, { className: "w-full", children: [
1314
+ t1,
1315
+ /* @__PURE__ */ jsxs(TableBody, { children: [
1316
+ t2,
1317
+ t3
1318
+ ] })
1319
+ ] });
1320
+ $[9] = t2;
1321
+ $[10] = t3;
1322
+ $[11] = t4;
1323
+ } else {
1324
+ t4 = $[11];
1325
+ }
1326
+ const t5 = Boolean(roleToBeDeleted);
1327
+ let t6;
1328
+ if ($[12] !== deleteRole || $[13] !== roleToBeDeleted) {
1329
+ t6 = () => {
1330
+ if (roleToBeDeleted) {
1331
+ setDeleteInProgress(true);
1332
+ deleteRole(roleToBeDeleted).then(() => {
1333
+ setRoleToBeDeleted(void 0);
1334
+ }).finally(() => {
1335
+ setDeleteInProgress(false);
1336
+ });
1337
+ }
1338
+ };
1339
+ $[12] = deleteRole;
1340
+ $[13] = roleToBeDeleted;
1341
+ $[14] = t6;
1342
+ } else {
1343
+ t6 = $[14];
1344
+ }
1345
+ let t7;
1346
+ let t8;
1347
+ let t9;
1348
+ if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
1349
+ t7 = () => {
1350
+ setRoleToBeDeleted(void 0);
1351
+ };
1352
+ t8 = /* @__PURE__ */ jsx(Fragment, { children: "Delete?" });
1353
+ t9 = /* @__PURE__ */ jsx(Fragment, { children: "Are you sure you want to delete this role?" });
1354
+ $[15] = t7;
1355
+ $[16] = t8;
1356
+ $[17] = t9;
1357
+ } else {
1358
+ t7 = $[15];
1359
+ t8 = $[16];
1360
+ t9 = $[17];
1361
+ }
1362
+ let t10;
1363
+ if ($[18] !== deleteInProgress || $[19] !== t5 || $[20] !== t6) {
1364
+ t10 = /* @__PURE__ */ jsx(ConfirmationDialog, { open: t5, loading: deleteInProgress, onAccept: t6, onCancel: t7, title: t8, body: t9 });
1365
+ $[18] = deleteInProgress;
1366
+ $[19] = t5;
1367
+ $[20] = t6;
1368
+ $[21] = t10;
1369
+ } else {
1370
+ t10 = $[21];
1371
+ }
1372
+ let t11;
1373
+ if ($[22] !== t10 || $[23] !== t4) {
1374
+ t11 = /* @__PURE__ */ jsxs("div", { className: "w-full overflow-auto", children: [
1375
+ t4,
1376
+ t10
1377
+ ] });
1378
+ $[22] = t10;
1379
+ $[23] = t4;
1380
+ $[24] = t11;
1381
+ } else {
1382
+ t11 = $[24];
1383
+ }
1384
+ return t11;
848
1385
  }
849
- const tn = A.memo(
850
- function({ children: i }) {
851
- const { collections: n } = Oe(), [c, r] = P(!1), [h, w] = P(), { canEditRoles: f } = W(), x = k((d) => {
852
- r(!0), w(d);
853
- }, []);
854
- return /* @__PURE__ */ l(we, { className: "w-full flex flex-col py-4 gap-4", maxWidth: "6xl", children: [
855
- i,
856
- /* @__PURE__ */ l("div", { className: "flex items-center mt-12", children: [
857
- /* @__PURE__ */ e(
858
- O,
859
- {
860
- gutterBottom: !0,
861
- variant: "h4",
862
- className: "flex-grow",
863
- component: "h4",
864
- children: "Roles"
865
- }
866
- ),
867
- /* @__PURE__ */ e(V, { title: f ? void 0 : "Update plans to customise roles", children: /* @__PURE__ */ e(
868
- Y,
869
- {
870
- size: "large",
871
- disabled: !f,
872
- startIcon: /* @__PURE__ */ e(ye, {}),
873
- onClick: () => r(!0),
874
- children: "Add role"
875
- }
876
- ) })
877
- ] }),
878
- /* @__PURE__ */ e(nn, { onRoleClicked: x, editable: !!f }),
879
- /* @__PURE__ */ e(
880
- Ze,
881
- {
882
- open: c,
883
- role: h,
884
- editable: f,
885
- collections: n,
886
- handleClose: () => {
887
- w(void 0), r(!1);
888
- }
889
- },
890
- h?.id ?? "new"
891
- )
1386
+ const RolesView = React.memo(function RolesView2(t0) {
1387
+ const $ = c(13);
1388
+ const {
1389
+ children
1390
+ } = t0;
1391
+ const {
1392
+ collections
1393
+ } = useNavigationController();
1394
+ const [dialogOpen, setDialogOpen] = useState(false);
1395
+ const [selectedRole, setSelectedRole] = useState();
1396
+ let t1;
1397
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
1398
+ t1 = (user) => {
1399
+ setDialogOpen(true);
1400
+ setSelectedRole(user);
1401
+ };
1402
+ $[0] = t1;
1403
+ } else {
1404
+ t1 = $[0];
1405
+ }
1406
+ const onRoleClicked = t1;
1407
+ let t2;
1408
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
1409
+ t2 = () => {
1410
+ setSelectedRole(void 0);
1411
+ setDialogOpen(false);
1412
+ };
1413
+ $[1] = t2;
1414
+ } else {
1415
+ t2 = $[1];
1416
+ }
1417
+ const handleClose = t2;
1418
+ let t3;
1419
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
1420
+ t3 = /* @__PURE__ */ jsx(Typography, { gutterBottom: true, variant: "h4", className: "flex-grow", component: "h4", children: "Roles" });
1421
+ $[2] = t3;
1422
+ } else {
1423
+ t3 = $[2];
1424
+ }
1425
+ let t4;
1426
+ let t5;
1427
+ if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
1428
+ t4 = /* @__PURE__ */ jsxs("div", { className: "flex items-center mt-12", children: [
1429
+ t3,
1430
+ /* @__PURE__ */ jsx(Button, { size: "large", startIcon: /* @__PURE__ */ jsx(AddIcon, {}), onClick: () => setDialogOpen(true), children: "Add role" })
1431
+ ] });
1432
+ t5 = /* @__PURE__ */ jsx(RolesTable, { onRoleClicked, editable: true });
1433
+ $[3] = t4;
1434
+ $[4] = t5;
1435
+ } else {
1436
+ t4 = $[3];
1437
+ t5 = $[4];
1438
+ }
1439
+ const t6 = selectedRole?.id ?? "new";
1440
+ let t7;
1441
+ if ($[5] !== collections || $[6] !== dialogOpen || $[7] !== selectedRole || $[8] !== t6) {
1442
+ t7 = /* @__PURE__ */ jsx(RolesDetailsForm, { open: dialogOpen, role: selectedRole, editable: true, collections, handleClose }, t6);
1443
+ $[5] = collections;
1444
+ $[6] = dialogOpen;
1445
+ $[7] = selectedRole;
1446
+ $[8] = t6;
1447
+ $[9] = t7;
1448
+ } else {
1449
+ t7 = $[9];
1450
+ }
1451
+ let t8;
1452
+ if ($[10] !== children || $[11] !== t7) {
1453
+ t8 = /* @__PURE__ */ jsxs(Container, { className: "w-full flex flex-col py-4 gap-4", maxWidth: "6xl", children: [
1454
+ children,
1455
+ t4,
1456
+ t5,
1457
+ t7
892
1458
  ] });
1459
+ $[10] = children;
1460
+ $[11] = t7;
1461
+ $[12] = t8;
1462
+ } else {
1463
+ t8 = $[12];
893
1464
  }
894
- ), rn = _.object().shape({
895
- displayName: _.string().required("Required"),
896
- email: _.string().email().required("Required"),
897
- roles: _.array().min(1)
1465
+ return t8;
1466
+ });
1467
+ const UserYupSchema = Yup.object().shape({
1468
+ displayName: Yup.string().required("Required"),
1469
+ email: Yup.string().email().required("Required"),
1470
+ roles: Yup.array().min(1)
898
1471
  });
899
- function on(t, i, n, c, r) {
900
- const h = n.filter((y) => y.roles?.map((d) => d.id).includes("admin")), w = t.roles?.map((y) => y.id).includes("admin");
901
- if ((!r || !Je(r.roles ?? [], i.roles ?? [])) && !w)
1472
+ function canUserBeEdited(loggedUser, user, users, roles, prevUser) {
1473
+ const admins = users.filter((u) => u.roles?.map((r) => r.id).includes("admin"));
1474
+ const loggedUserIsAdmin = loggedUser.roles?.map((r) => r.id).includes("admin");
1475
+ const didRolesChange = !prevUser || !areRolesEqual(prevUser.roles ?? [], user.roles ?? []);
1476
+ if (didRolesChange && !loggedUserIsAdmin) {
902
1477
  throw new Error("Only admins can change roles");
903
- if (r && r.roles?.map((y) => y.id).includes("admin") && !i.roles?.map((y) => y.id).includes("admin") && h.length === 1)
1478
+ }
1479
+ const adminRoleRemoved = prevUser && prevUser.roles?.map((r) => r.id).includes("admin") && !user.roles?.map((r) => r.id).includes("admin");
1480
+ if (adminRoleRemoved && admins.length === 1) {
904
1481
  throw new Error("There must be at least one admin");
905
- return !0;
1482
+ }
1483
+ return true;
906
1484
  }
907
- function ln({
908
- open: t,
909
- user: i,
910
- handleClose: n
911
- }) {
912
- const c = Ne(), {
913
- user: r
914
- } = Re(), {
915
- saveUser: h,
916
- users: w,
917
- roles: f
918
- } = W(), x = !i, y = k((u) => {
919
- if (!r)
920
- throw new Error("Logged user not found");
921
- try {
922
- return on(r, u, w, f, i), h(u);
923
- } catch (N) {
924
- return Promise.reject(N);
925
- }
926
- }, [f, h, i, w, r]), d = xe({
927
- initialValues: i ?? {
1485
+ function UserDetailsForm(t0) {
1486
+ const $ = c(73);
1487
+ const {
1488
+ open,
1489
+ user: userProp,
1490
+ handleClose
1491
+ } = t0;
1492
+ const snackbarController = useSnackbarController();
1493
+ const {
1494
+ user: loggedInUser
1495
+ } = useAuthController();
1496
+ const {
1497
+ saveUser,
1498
+ users,
1499
+ roles
1500
+ } = useUserManagement();
1501
+ const isNewUser = !userProp;
1502
+ let t1;
1503
+ if ($[0] !== loggedInUser || $[1] !== roles || $[2] !== saveUser || $[3] !== userProp || $[4] !== users) {
1504
+ t1 = (savedUser) => {
1505
+ if (!loggedInUser) {
1506
+ throw new Error("Logged user not found");
1507
+ }
1508
+ try {
1509
+ canUserBeEdited(loggedInUser, savedUser, users, roles, userProp);
1510
+ return saveUser(savedUser);
1511
+ } catch (t210) {
1512
+ const e = t210;
1513
+ return Promise.reject(e);
1514
+ }
1515
+ };
1516
+ $[0] = loggedInUser;
1517
+ $[1] = roles;
1518
+ $[2] = saveUser;
1519
+ $[3] = userProp;
1520
+ $[4] = users;
1521
+ $[5] = t1;
1522
+ } else {
1523
+ t1 = $[5];
1524
+ }
1525
+ const onUserUpdated = t1;
1526
+ let t2;
1527
+ if ($[6] !== roles || $[7] !== userProp) {
1528
+ t2 = userProp ?? {
928
1529
  displayName: "",
929
1530
  email: "",
930
- roles: f.filter((u) => u.id === "editor")
931
- },
932
- validation: (u) => rn.validate(u, { abortEarly: !1 }).then(() => ({})).catch((N) => N.inner.reduce((U, F) => (U[F.path] = F.message, U), {})),
933
- onSubmit: (u, N) => y(u).then(() => {
934
- n(), N.resetForm({
935
- values: u
1531
+ roles: roles.filter(_temp$2)
1532
+ };
1533
+ $[6] = roles;
1534
+ $[7] = userProp;
1535
+ $[8] = t2;
1536
+ } else {
1537
+ t2 = $[8];
1538
+ }
1539
+ let t3;
1540
+ if ($[9] !== handleClose || $[10] !== onUserUpdated || $[11] !== snackbarController) {
1541
+ t3 = (user, formexController) => onUserUpdated(user).then(() => {
1542
+ handleClose();
1543
+ formexController.resetForm({
1544
+ values: user
936
1545
  });
937
- }).catch((U) => {
938
- c.open({
1546
+ }).catch((e_1) => {
1547
+ snackbarController.open({
939
1548
  type: "error",
940
- message: U.message
1549
+ message: e_1.message
941
1550
  });
942
- })
943
- }), {
944
- isSubmitting: I,
945
- touched: b,
946
- handleChange: s,
947
- values: C,
948
- errors: D,
949
- setFieldValue: p,
950
- dirty: z,
951
- handleSubmit: L,
952
- submitCount: g
953
- } = d;
954
- return /* @__PURE__ */ e(
955
- ue,
956
- {
957
- open: t,
958
- onOpenChange: (u) => u ? void 0 : n(),
959
- maxWidth: "4xl",
960
- children: /* @__PURE__ */ e(De, { value: d, children: /* @__PURE__ */ l(
961
- "form",
962
- {
963
- onSubmit: L,
964
- autoComplete: "off",
965
- noValidate: !0,
966
- style: {
967
- display: "flex",
968
- flexDirection: "column",
969
- position: "relative",
970
- height: "100%"
971
- },
972
- children: [
973
- /* @__PURE__ */ l(me, { className: "h-full flex-grow", children: [
974
- /* @__PURE__ */ e(
975
- "div",
976
- {
977
- className: "flex flex-row pt-4 pb-4",
978
- children: /* @__PURE__ */ e(
979
- O,
980
- {
981
- variant: "h4",
982
- className: "flex-grow",
983
- children: "User"
984
- }
985
- )
986
- }
987
- ),
988
- /* @__PURE__ */ l("div", { className: "grid grid-cols-12 gap-8", children: [
989
- /* @__PURE__ */ l("div", { className: "col-span-12", children: [
990
- /* @__PURE__ */ e(
991
- K,
992
- {
993
- name: "displayName",
994
- required: !0,
995
- error: g > 0 && !!D.displayName,
996
- value: C.displayName ?? "",
997
- onChange: s,
998
- "aria-describedby": "name-helper-text",
999
- label: "Name"
1000
- }
1001
- ),
1002
- /* @__PURE__ */ e(B, { children: g > 0 && D.displayName ? D.displayName : "Name of this user" })
1003
- ] }),
1004
- /* @__PURE__ */ l("div", { className: "col-span-12", children: [
1005
- /* @__PURE__ */ e(
1006
- K,
1007
- {
1008
- required: !0,
1009
- error: g > 0 && !!D.email,
1010
- name: "email",
1011
- value: C.email ?? "",
1012
- onChange: s,
1013
- "aria-describedby": "email-helper-text",
1014
- label: "Email"
1015
- }
1016
- ),
1017
- /* @__PURE__ */ e(B, { children: g > 0 && D.email ? D.email : "Email of this user" })
1018
- ] }),
1019
- /* @__PURE__ */ e("div", { className: "col-span-12", children: /* @__PURE__ */ e(
1020
- Le,
1021
- {
1022
- label: "Roles",
1023
- value: C.roles?.map((u) => u.id) ?? [],
1024
- onMultiValueChange: (u) => p("roles", u.map((N) => f.find((U) => U.id === N))),
1025
- renderValue: (u) => {
1026
- const N = f.find((U) => U.id === u);
1027
- return N ? /* @__PURE__ */ e("div", { className: "flex flex-wrap space-x-2 space-y-2", children: /* @__PURE__ */ e(Q, { role: N }, N?.id) }) : null;
1028
- },
1029
- children: f.map((u) => /* @__PURE__ */ e(
1030
- Ve,
1031
- {
1032
- value: u.id,
1033
- children: /* @__PURE__ */ e(Q, { role: u }, u?.id)
1034
- },
1035
- u.id
1036
- ))
1037
- }
1038
- ) })
1039
- ] })
1040
- ] }),
1041
- /* @__PURE__ */ l(he, { children: [
1042
- /* @__PURE__ */ e(
1043
- Y,
1044
- {
1045
- variant: "text",
1046
- onClick: () => {
1047
- n();
1048
- },
1049
- children: "Cancel"
1050
- }
1051
- ),
1052
- /* @__PURE__ */ e(
1053
- fe,
1054
- {
1055
- variant: "filled",
1056
- color: "primary",
1057
- type: "submit",
1058
- disabled: !z,
1059
- loading: I,
1060
- startIcon: /* @__PURE__ */ e(ge, {}),
1061
- children: x ? "Create user" : "Update"
1062
- }
1063
- )
1064
- ] })
1065
- ]
1066
- }
1067
- ) })
1068
- }
1069
- );
1551
+ });
1552
+ $[9] = handleClose;
1553
+ $[10] = onUserUpdated;
1554
+ $[11] = snackbarController;
1555
+ $[12] = t3;
1556
+ } else {
1557
+ t3 = $[12];
1558
+ }
1559
+ let t4;
1560
+ if ($[13] !== t2 || $[14] !== t3) {
1561
+ t4 = {
1562
+ initialValues: t2,
1563
+ validation: _temp5,
1564
+ onSubmit: t3
1565
+ };
1566
+ $[13] = t2;
1567
+ $[14] = t3;
1568
+ $[15] = t4;
1569
+ } else {
1570
+ t4 = $[15];
1571
+ }
1572
+ const formex = useCreateFormex(t4);
1573
+ const {
1574
+ isSubmitting,
1575
+ handleChange,
1576
+ values: values_0,
1577
+ errors,
1578
+ setFieldValue,
1579
+ dirty,
1580
+ handleSubmit,
1581
+ submitCount
1582
+ } = formex;
1583
+ let t5;
1584
+ if ($[16] !== handleClose) {
1585
+ t5 = (open_0) => !open_0 ? handleClose() : void 0;
1586
+ $[16] = handleClose;
1587
+ $[17] = t5;
1588
+ } else {
1589
+ t5 = $[17];
1590
+ }
1591
+ let t6;
1592
+ let t7;
1593
+ if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
1594
+ t6 = {
1595
+ display: "flex",
1596
+ flexDirection: "column",
1597
+ position: "relative",
1598
+ height: "100%"
1599
+ };
1600
+ t7 = /* @__PURE__ */ jsx(DialogTitle, { variant: "h4", gutterBottom: false, children: "User" });
1601
+ $[18] = t6;
1602
+ $[19] = t7;
1603
+ } else {
1604
+ t6 = $[18];
1605
+ t7 = $[19];
1606
+ }
1607
+ const t8 = submitCount > 0 && Boolean(errors.displayName);
1608
+ const t9 = values_0.displayName ?? "";
1609
+ let t10;
1610
+ if ($[20] !== handleChange || $[21] !== t8 || $[22] !== t9) {
1611
+ t10 = /* @__PURE__ */ jsx(TextField, { name: "displayName", required: true, error: t8, value: t9, onChange: handleChange, "aria-describedby": "name-helper-text", label: "Name" });
1612
+ $[20] = handleChange;
1613
+ $[21] = t8;
1614
+ $[22] = t9;
1615
+ $[23] = t10;
1616
+ } else {
1617
+ t10 = $[23];
1618
+ }
1619
+ const t11 = submitCount > 0 && Boolean(errors.displayName) ? errors.displayName : "Name of this user";
1620
+ let t12;
1621
+ if ($[24] !== t11) {
1622
+ t12 = /* @__PURE__ */ jsx(FieldCaption, { children: t11 });
1623
+ $[24] = t11;
1624
+ $[25] = t12;
1625
+ } else {
1626
+ t12 = $[25];
1627
+ }
1628
+ let t13;
1629
+ if ($[26] !== t10 || $[27] !== t12) {
1630
+ t13 = /* @__PURE__ */ jsxs("div", { className: "col-span-12", children: [
1631
+ t10,
1632
+ t12
1633
+ ] });
1634
+ $[26] = t10;
1635
+ $[27] = t12;
1636
+ $[28] = t13;
1637
+ } else {
1638
+ t13 = $[28];
1639
+ }
1640
+ const t14 = submitCount > 0 && Boolean(errors.email);
1641
+ const t15 = values_0.email ?? "";
1642
+ let t16;
1643
+ if ($[29] !== handleChange || $[30] !== t14 || $[31] !== t15) {
1644
+ t16 = /* @__PURE__ */ jsx(TextField, { required: true, error: t14, name: "email", value: t15, onChange: handleChange, "aria-describedby": "email-helper-text", label: "Email" });
1645
+ $[29] = handleChange;
1646
+ $[30] = t14;
1647
+ $[31] = t15;
1648
+ $[32] = t16;
1649
+ } else {
1650
+ t16 = $[32];
1651
+ }
1652
+ const t17 = submitCount > 0 && Boolean(errors.email) ? errors.email : "Email of this user";
1653
+ let t18;
1654
+ if ($[33] !== t17) {
1655
+ t18 = /* @__PURE__ */ jsx(FieldCaption, { children: t17 });
1656
+ $[33] = t17;
1657
+ $[34] = t18;
1658
+ } else {
1659
+ t18 = $[34];
1660
+ }
1661
+ let t19;
1662
+ if ($[35] !== t16 || $[36] !== t18) {
1663
+ t19 = /* @__PURE__ */ jsxs("div", { className: "col-span-12", children: [
1664
+ t16,
1665
+ t18
1666
+ ] });
1667
+ $[35] = t16;
1668
+ $[36] = t18;
1669
+ $[37] = t19;
1670
+ } else {
1671
+ t19 = $[37];
1672
+ }
1673
+ let t20;
1674
+ if ($[38] !== values_0.roles) {
1675
+ t20 = values_0.roles?.map(_temp6) ?? [];
1676
+ $[38] = values_0.roles;
1677
+ $[39] = t20;
1678
+ } else {
1679
+ t20 = $[39];
1680
+ }
1681
+ let t21;
1682
+ if ($[40] !== roles || $[41] !== setFieldValue) {
1683
+ t21 = (value) => setFieldValue("roles", value.map((id) => roles.find((r_1) => r_1.id === id)));
1684
+ $[40] = roles;
1685
+ $[41] = setFieldValue;
1686
+ $[42] = t21;
1687
+ } else {
1688
+ t21 = $[42];
1689
+ }
1690
+ let t22;
1691
+ if ($[43] !== roles) {
1692
+ t22 = roles.map(_temp7);
1693
+ $[43] = roles;
1694
+ $[44] = t22;
1695
+ } else {
1696
+ t22 = $[44];
1697
+ }
1698
+ let t23;
1699
+ if ($[45] !== t20 || $[46] !== t21 || $[47] !== t22) {
1700
+ t23 = /* @__PURE__ */ jsx("div", { className: "col-span-12", children: /* @__PURE__ */ jsx(MultiSelect, { className: "w-full", label: "Roles", value: t20, onValueChange: t21, children: t22 }) });
1701
+ $[45] = t20;
1702
+ $[46] = t21;
1703
+ $[47] = t22;
1704
+ $[48] = t23;
1705
+ } else {
1706
+ t23 = $[48];
1707
+ }
1708
+ let t24;
1709
+ if ($[49] !== t13 || $[50] !== t19 || $[51] !== t23) {
1710
+ t24 = /* @__PURE__ */ jsx(DialogContent, { className: "h-full flex-grow", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-12 gap-4", children: [
1711
+ t13,
1712
+ t19,
1713
+ t23
1714
+ ] }) });
1715
+ $[49] = t13;
1716
+ $[50] = t19;
1717
+ $[51] = t23;
1718
+ $[52] = t24;
1719
+ } else {
1720
+ t24 = $[52];
1721
+ }
1722
+ let t25;
1723
+ if ($[53] !== handleClose) {
1724
+ t25 = /* @__PURE__ */ jsx(Button, { variant: "text", color: "primary", onClick: () => {
1725
+ handleClose();
1726
+ }, children: "Cancel" });
1727
+ $[53] = handleClose;
1728
+ $[54] = t25;
1729
+ } else {
1730
+ t25 = $[54];
1731
+ }
1732
+ const t26 = !dirty;
1733
+ const t27 = isNewUser ? "Create user" : "Update";
1734
+ let t28;
1735
+ if ($[55] !== isSubmitting || $[56] !== t26 || $[57] !== t27) {
1736
+ t28 = /* @__PURE__ */ jsx(LoadingButton, { variant: "filled", color: "primary", type: "submit", disabled: t26, loading: isSubmitting, children: t27 });
1737
+ $[55] = isSubmitting;
1738
+ $[56] = t26;
1739
+ $[57] = t27;
1740
+ $[58] = t28;
1741
+ } else {
1742
+ t28 = $[58];
1743
+ }
1744
+ let t29;
1745
+ if ($[59] !== t25 || $[60] !== t28) {
1746
+ t29 = /* @__PURE__ */ jsxs(DialogActions, { children: [
1747
+ t25,
1748
+ t28
1749
+ ] });
1750
+ $[59] = t25;
1751
+ $[60] = t28;
1752
+ $[61] = t29;
1753
+ } else {
1754
+ t29 = $[61];
1755
+ }
1756
+ let t30;
1757
+ if ($[62] !== handleSubmit || $[63] !== t24 || $[64] !== t29) {
1758
+ t30 = /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, autoComplete: "off", noValidate: true, style: t6, children: [
1759
+ t7,
1760
+ t24,
1761
+ t29
1762
+ ] });
1763
+ $[62] = handleSubmit;
1764
+ $[63] = t24;
1765
+ $[64] = t29;
1766
+ $[65] = t30;
1767
+ } else {
1768
+ t30 = $[65];
1769
+ }
1770
+ let t31;
1771
+ if ($[66] !== formex || $[67] !== t30) {
1772
+ t31 = /* @__PURE__ */ jsx(Formex, { value: formex, children: t30 });
1773
+ $[66] = formex;
1774
+ $[67] = t30;
1775
+ $[68] = t31;
1776
+ } else {
1777
+ t31 = $[68];
1778
+ }
1779
+ let t32;
1780
+ if ($[69] !== open || $[70] !== t31 || $[71] !== t5) {
1781
+ t32 = /* @__PURE__ */ jsx(Dialog, { open, onOpenChange: t5, maxWidth: "4xl", children: t31 });
1782
+ $[69] = open;
1783
+ $[70] = t31;
1784
+ $[71] = t5;
1785
+ $[72] = t32;
1786
+ } else {
1787
+ t32 = $[72];
1788
+ }
1789
+ return t32;
1790
+ }
1791
+ function _temp7(userRole) {
1792
+ return /* @__PURE__ */ jsx(MultiSelectItem, { value: userRole.id, children: /* @__PURE__ */ jsx(RoleChip, { role: userRole }, userRole?.id) }, userRole.id);
1793
+ }
1794
+ function _temp6(r_0) {
1795
+ return r_0.id;
1070
1796
  }
1071
- function sn({ onUserClicked: t }) {
1797
+ function _temp5(values) {
1798
+ return UserYupSchema.validate(values, {
1799
+ abortEarly: false
1800
+ }).then(_temp2).catch(_temp4);
1801
+ }
1802
+ function _temp4(e_0) {
1803
+ return e_0.inner.reduce(_temp3, {});
1804
+ }
1805
+ function _temp3(acc, error) {
1806
+ acc[error.path] = error.message;
1807
+ return acc;
1808
+ }
1809
+ function _temp2() {
1810
+ return {};
1811
+ }
1812
+ function _temp$2(r) {
1813
+ return r.id === "editor";
1814
+ }
1815
+ function UsersTable(t0) {
1816
+ const $ = c(28);
1072
1817
  const {
1073
- users: i,
1074
- saveUser: n,
1075
- deleteUser: c
1076
- } = W(), r = Re(), h = Ne(), w = $e(), f = w?.locale ? Ye[w?.locale] : void 0, x = w?.dateTimeFormat ?? Me, [y, d] = P(void 0), [I, b] = P(!1);
1077
- return /* @__PURE__ */ l("div", { className: "overflow-auto", children: [
1078
- /* @__PURE__ */ l(re, { children: [
1079
- /* @__PURE__ */ l(oe, { children: [
1080
- /* @__PURE__ */ e(a, { className: "truncate w-16" }),
1081
- /* @__PURE__ */ e(a, { children: "ID" }),
1082
- /* @__PURE__ */ e(a, { children: "Email" }),
1083
- /* @__PURE__ */ e(a, { children: "Name" }),
1084
- /* @__PURE__ */ e(a, { children: "Roles" }),
1085
- /* @__PURE__ */ e(a, { children: "Created on" })
1086
- ] }),
1087
- /* @__PURE__ */ l(le, { children: [
1088
- i && i.map((s) => {
1089
- const C = s.roles, D = s.created_on ? qe(s.created_on, x, { locale: f }) : "";
1090
- return /* @__PURE__ */ l(
1091
- q,
1092
- {
1093
- onClick: () => {
1094
- t(s);
1095
- },
1096
- children: [
1097
- /* @__PURE__ */ e(a, { className: "w-10", children: /* @__PURE__ */ e(V, { title: "Delete this user", children: /* @__PURE__ */ e(
1098
- pe,
1099
- {
1100
- size: "small",
1101
- onClick: (p) => (p.stopPropagation(), d(s)),
1102
- children: /* @__PURE__ */ e(Ce, {})
1103
- }
1104
- ) }) }),
1105
- /* @__PURE__ */ e(a, { children: s.uid }),
1106
- /* @__PURE__ */ e(a, { children: s.email }),
1107
- /* @__PURE__ */ e(a, { className: "font-medium align-left", children: s.displayName }),
1108
- /* @__PURE__ */ e(a, { className: "align-left", children: C ? /* @__PURE__ */ e("div", { className: "flex flex-wrap gap-2", children: C.map(
1109
- (p) => /* @__PURE__ */ e(Q, { role: p }, p?.id)
1110
- ) }) : null }),
1111
- /* @__PURE__ */ e(a, { children: D })
1112
- ]
1113
- },
1114
- "row_" + s.uid
1115
- );
1116
- }),
1117
- (!i || i.length === 0) && /* @__PURE__ */ e(q, { children: /* @__PURE__ */ e(a, { colspan: 6, children: /* @__PURE__ */ l(ve, { className: "flex flex-col gap-4 my-8 items-center", children: [
1118
- /* @__PURE__ */ e(O, { variant: "label", children: "There are no users yet" }),
1119
- /* @__PURE__ */ e(
1120
- Y,
1121
- {
1122
- variant: "outlined",
1123
- onClick: () => {
1124
- if (!r.user?.uid)
1125
- throw Error("UsersTable, authController misconfiguration");
1126
- n({
1127
- uid: r.user?.uid,
1128
- email: r.user?.email,
1129
- displayName: r.user?.displayName,
1130
- photoURL: r.user?.photoURL,
1131
- providerId: r.user?.providerId,
1132
- isAnonymous: r.user?.isAnonymous,
1133
- roles: [{ id: "admin", name: "Admin" }],
1134
- created_on: /* @__PURE__ */ new Date()
1135
- }).then(() => {
1136
- h.open({
1137
- type: "success",
1138
- message: "User added successfully"
1139
- });
1140
- }).catch((s) => {
1141
- h.open({
1142
- type: "error",
1143
- message: "Error adding user: " + s.message
1144
- });
1145
- });
1146
- },
1147
- children: "Add the logged user as an admin"
1148
- }
1149
- )
1150
- ] }) }) })
1818
+ onUserClicked
1819
+ } = t0;
1820
+ const {
1821
+ users,
1822
+ saveUser,
1823
+ deleteUser
1824
+ } = useUserManagement();
1825
+ const authController = useAuthController();
1826
+ const snackbarController = useSnackbarController();
1827
+ const customizationController = useCustomizationController();
1828
+ const dateUtilsLocale = customizationController?.locale ? locales[customizationController?.locale] : void 0;
1829
+ const dateFormat = customizationController?.dateTimeFormat ?? defaultDateFormat;
1830
+ const [userToBeDeleted, setUserToBeDeleted] = useState(void 0);
1831
+ const [deleteInProgress, setDeleteInProgress] = useState(false);
1832
+ let t1;
1833
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
1834
+ t1 = /* @__PURE__ */ jsxs(TableHeader, { children: [
1835
+ /* @__PURE__ */ jsx(TableCell, { className: "truncate w-16" }),
1836
+ /* @__PURE__ */ jsx(TableCell, { children: "Email" }),
1837
+ /* @__PURE__ */ jsx(TableCell, { children: "Name" }),
1838
+ /* @__PURE__ */ jsx(TableCell, { children: "Roles" }),
1839
+ /* @__PURE__ */ jsx(TableCell, { children: "Created on" })
1840
+ ] });
1841
+ $[0] = t1;
1842
+ } else {
1843
+ t1 = $[0];
1844
+ }
1845
+ let t2;
1846
+ if ($[1] !== dateFormat || $[2] !== dateUtilsLocale || $[3] !== onUserClicked || $[4] !== users) {
1847
+ t2 = users && users.map((user) => {
1848
+ const userRoles = user.roles;
1849
+ const formattedDate = user.created_on ? format(user.created_on, dateFormat, {
1850
+ locale: dateUtilsLocale
1851
+ }) : "";
1852
+ return /* @__PURE__ */ jsxs(TableRow, { onClick: () => {
1853
+ onUserClicked(user);
1854
+ }, children: [
1855
+ /* @__PURE__ */ jsx(TableCell, { className: "w-10", children: /* @__PURE__ */ jsx(Tooltip, { asChild: true, title: "Delete this user", children: /* @__PURE__ */ jsx(IconButton, { size: "small", onClick: (event) => {
1856
+ event.stopPropagation();
1857
+ return setUserToBeDeleted(user);
1858
+ }, children: /* @__PURE__ */ jsx(DeleteIcon, {}) }) }) }),
1859
+ /* @__PURE__ */ jsx(TableCell, { children: user.email }),
1860
+ /* @__PURE__ */ jsx(TableCell, { className: "font-medium align-left", children: user.displayName }),
1861
+ /* @__PURE__ */ jsx(TableCell, { className: "align-left", children: userRoles ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: userRoles.map(_temp$1) }) : null }),
1862
+ /* @__PURE__ */ jsx(TableCell, { children: formattedDate })
1863
+ ] }, "row_" + user.uid);
1864
+ });
1865
+ $[1] = dateFormat;
1866
+ $[2] = dateUtilsLocale;
1867
+ $[3] = onUserClicked;
1868
+ $[4] = users;
1869
+ $[5] = t2;
1870
+ } else {
1871
+ t2 = $[5];
1872
+ }
1873
+ let t3;
1874
+ if ($[6] !== authController || $[7] !== saveUser || $[8] !== snackbarController || $[9] !== users) {
1875
+ t3 = (!users || users.length === 0) && /* @__PURE__ */ jsx(TableRow, { children: /* @__PURE__ */ jsx(TableCell, { colspan: 6, children: /* @__PURE__ */ jsxs(CenteredView, { className: "flex flex-col gap-4 my-8 items-center", children: [
1876
+ /* @__PURE__ */ jsx(Typography, { variant: "label", children: "There are no users yet" }),
1877
+ /* @__PURE__ */ jsx(Button, { variant: "outlined", onClick: () => {
1878
+ if (!authController.user?.uid) {
1879
+ throw Error("UsersTable, authController misconfiguration");
1880
+ }
1881
+ saveUser({
1882
+ uid: authController.user?.uid,
1883
+ email: authController.user?.email,
1884
+ displayName: authController.user?.displayName,
1885
+ photoURL: authController.user?.photoURL,
1886
+ providerId: authController.user?.providerId,
1887
+ isAnonymous: authController.user?.isAnonymous,
1888
+ roles: [{
1889
+ id: "admin",
1890
+ name: "Admin"
1891
+ }],
1892
+ created_on: /* @__PURE__ */ new Date()
1893
+ }).then(() => {
1894
+ snackbarController.open({
1895
+ type: "success",
1896
+ message: "User added successfully"
1897
+ });
1898
+ }).catch((error) => {
1899
+ snackbarController.open({
1900
+ type: "error",
1901
+ message: "Error adding user: " + error.message
1902
+ });
1903
+ });
1904
+ }, children: "Add the logged user as an admin" })
1905
+ ] }) }) });
1906
+ $[6] = authController;
1907
+ $[7] = saveUser;
1908
+ $[8] = snackbarController;
1909
+ $[9] = users;
1910
+ $[10] = t3;
1911
+ } else {
1912
+ t3 = $[10];
1913
+ }
1914
+ let t4;
1915
+ if ($[11] !== t2 || $[12] !== t3) {
1916
+ t4 = /* @__PURE__ */ jsxs(Table, { className: "w-full", children: [
1917
+ t1,
1918
+ /* @__PURE__ */ jsxs(TableBody, { children: [
1919
+ t2,
1920
+ t3
1151
1921
  ] })
1152
- ] }),
1153
- /* @__PURE__ */ e(
1154
- be,
1155
- {
1156
- open: !!y,
1157
- loading: I,
1158
- onAccept: () => {
1159
- y && (b(!0), c(y).then(() => {
1160
- d(void 0);
1161
- }).catch((s) => {
1162
- h.open({
1163
- type: "error",
1164
- message: "Error deleting user: " + s.message
1165
- });
1166
- }).finally(() => {
1167
- b(!1);
1168
- }));
1169
- },
1170
- onCancel: () => {
1171
- d(void 0);
1172
- },
1173
- title: /* @__PURE__ */ e(H, { children: "Delete?" }),
1174
- body: /* @__PURE__ */ e(H, { children: "Are you sure you want to delete this user?" })
1922
+ ] });
1923
+ $[11] = t2;
1924
+ $[12] = t3;
1925
+ $[13] = t4;
1926
+ } else {
1927
+ t4 = $[13];
1928
+ }
1929
+ const t5 = Boolean(userToBeDeleted);
1930
+ let t6;
1931
+ if ($[14] !== deleteUser || $[15] !== snackbarController || $[16] !== userToBeDeleted) {
1932
+ t6 = () => {
1933
+ if (userToBeDeleted) {
1934
+ setDeleteInProgress(true);
1935
+ deleteUser(userToBeDeleted).then(() => {
1936
+ setUserToBeDeleted(void 0);
1937
+ }).catch((error_0) => {
1938
+ snackbarController.open({
1939
+ type: "error",
1940
+ message: "Error deleting user: " + error_0.message
1941
+ });
1942
+ }).finally(() => {
1943
+ setDeleteInProgress(false);
1944
+ });
1175
1945
  }
1176
- )
1177
- ] });
1946
+ };
1947
+ $[14] = deleteUser;
1948
+ $[15] = snackbarController;
1949
+ $[16] = userToBeDeleted;
1950
+ $[17] = t6;
1951
+ } else {
1952
+ t6 = $[17];
1953
+ }
1954
+ let t7;
1955
+ let t8;
1956
+ let t9;
1957
+ if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
1958
+ t7 = () => {
1959
+ setUserToBeDeleted(void 0);
1960
+ };
1961
+ t8 = /* @__PURE__ */ jsx(Fragment, { children: "Delete?" });
1962
+ t9 = /* @__PURE__ */ jsx(Fragment, { children: "Are you sure you want to delete this user?" });
1963
+ $[18] = t7;
1964
+ $[19] = t8;
1965
+ $[20] = t9;
1966
+ } else {
1967
+ t7 = $[18];
1968
+ t8 = $[19];
1969
+ t9 = $[20];
1970
+ }
1971
+ let t10;
1972
+ if ($[21] !== deleteInProgress || $[22] !== t5 || $[23] !== t6) {
1973
+ t10 = /* @__PURE__ */ jsx(ConfirmationDialog, { open: t5, loading: deleteInProgress, onAccept: t6, onCancel: t7, title: t8, body: t9 });
1974
+ $[21] = deleteInProgress;
1975
+ $[22] = t5;
1976
+ $[23] = t6;
1977
+ $[24] = t10;
1978
+ } else {
1979
+ t10 = $[24];
1980
+ }
1981
+ let t11;
1982
+ if ($[25] !== t10 || $[26] !== t4) {
1983
+ t11 = /* @__PURE__ */ jsxs("div", { className: "overflow-auto", children: [
1984
+ t4,
1985
+ t10
1986
+ ] });
1987
+ $[25] = t10;
1988
+ $[26] = t4;
1989
+ $[27] = t11;
1990
+ } else {
1991
+ t11 = $[27];
1992
+ }
1993
+ return t11;
1178
1994
  }
1179
- const an = function({ children: i }) {
1180
- const [n, c] = P(), [r, h] = P(), { users: w, usersLimit: f } = W(), x = f !== void 0 && w && w.length >= f, y = k((I) => {
1181
- h(I), c(!0);
1182
- }, []), d = k(() => {
1183
- c(!1), h(void 0);
1184
- }, []);
1185
- return /* @__PURE__ */ l(we, { className: "w-full flex flex-col py-4 gap-4", maxWidth: "6xl", children: [
1186
- i,
1187
- /* @__PURE__ */ l(
1188
- "div",
1189
- {
1190
- className: "flex items-center mt-12",
1191
- children: [
1192
- /* @__PURE__ */ e(
1193
- O,
1194
- {
1195
- gutterBottom: !0,
1196
- variant: "h4",
1197
- className: "flex-grow",
1198
- component: "h4",
1199
- children: "Users"
1200
- }
1201
- ),
1202
- /* @__PURE__ */ e(
1203
- Y,
1204
- {
1205
- size: "large",
1206
- disabled: x,
1207
- startIcon: /* @__PURE__ */ e(ye, {}),
1208
- onClick: () => c(!0),
1209
- children: "Add user"
1210
- }
1211
- )
1212
- ]
1213
- }
1214
- ),
1215
- /* @__PURE__ */ e(sn, { onUserClicked: y }),
1216
- /* @__PURE__ */ e(
1217
- ln,
1218
- {
1219
- open: n ?? !1,
1220
- user: r,
1221
- handleClose: d
1222
- },
1223
- r?.uid ?? "new"
1224
- )
1225
- ] });
1995
+ function _temp$1(userRole) {
1996
+ return /* @__PURE__ */ jsx(RoleChip, { role: userRole }, userRole?.id);
1997
+ }
1998
+ const UsersView = function UsersView2(t0) {
1999
+ const $ = c(13);
2000
+ const {
2001
+ children
2002
+ } = t0;
2003
+ const [dialogOpen, setDialogOpen] = useState(false);
2004
+ const [selectedUser, setSelectedUser] = useState();
2005
+ const [newFormKey, setNewFormKey] = useState(0);
2006
+ useUserManagement();
2007
+ let t1;
2008
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
2009
+ t1 = (user) => {
2010
+ setSelectedUser(user);
2011
+ setDialogOpen(true);
2012
+ };
2013
+ $[0] = t1;
2014
+ } else {
2015
+ t1 = $[0];
2016
+ }
2017
+ const onUserClicked = t1;
2018
+ let t2;
2019
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
2020
+ t2 = () => {
2021
+ setDialogOpen(false);
2022
+ setSelectedUser(void 0);
2023
+ };
2024
+ $[1] = t2;
2025
+ } else {
2026
+ t2 = $[1];
2027
+ }
2028
+ const handleClose = t2;
2029
+ let t3;
2030
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
2031
+ t3 = () => {
2032
+ setSelectedUser(void 0);
2033
+ setNewFormKey(_temp);
2034
+ setDialogOpen(true);
2035
+ };
2036
+ $[2] = t3;
2037
+ } else {
2038
+ t3 = $[2];
2039
+ }
2040
+ const handleAddUser = t3;
2041
+ let t4;
2042
+ if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
2043
+ t4 = /* @__PURE__ */ jsx(Typography, { gutterBottom: true, variant: "h4", className: "flex-grow", component: "h4", children: "Users" });
2044
+ $[3] = t4;
2045
+ } else {
2046
+ t4 = $[3];
2047
+ }
2048
+ let t5;
2049
+ let t6;
2050
+ if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
2051
+ t5 = /* @__PURE__ */ jsxs("div", { className: "flex items-center mt-12", children: [
2052
+ t4,
2053
+ /* @__PURE__ */ jsx(Button, { size: "large", startIcon: /* @__PURE__ */ jsx(AddIcon, {}), onClick: handleAddUser, children: "Add user" })
2054
+ ] });
2055
+ t6 = /* @__PURE__ */ jsx(UsersTable, { onUserClicked });
2056
+ $[4] = t5;
2057
+ $[5] = t6;
2058
+ } else {
2059
+ t5 = $[4];
2060
+ t6 = $[5];
2061
+ }
2062
+ const t7 = selectedUser?.uid ?? `new-${newFormKey}`;
2063
+ let t8;
2064
+ if ($[6] !== dialogOpen || $[7] !== selectedUser || $[8] !== t7) {
2065
+ t8 = /* @__PURE__ */ jsx(UserDetailsForm, { open: dialogOpen, user: selectedUser, handleClose }, t7);
2066
+ $[6] = dialogOpen;
2067
+ $[7] = selectedUser;
2068
+ $[8] = t7;
2069
+ $[9] = t8;
2070
+ } else {
2071
+ t8 = $[9];
2072
+ }
2073
+ let t9;
2074
+ if ($[10] !== children || $[11] !== t8) {
2075
+ t9 = /* @__PURE__ */ jsxs(Container, { className: "w-full flex flex-col py-4 gap-4", maxWidth: "6xl", children: [
2076
+ children,
2077
+ t5,
2078
+ t6,
2079
+ t8
2080
+ ] });
2081
+ $[10] = children;
2082
+ $[11] = t8;
2083
+ $[12] = t9;
2084
+ } else {
2085
+ t9 = $[12];
2086
+ }
2087
+ return t9;
1226
2088
  };
1227
- function xn({ userManagement: t }) {
1228
- return {
1229
- key: "user_management",
1230
- loading: t.loading,
1231
- provider: {
1232
- Component: Qe,
2089
+ function _temp(k) {
2090
+ return k + 1;
2091
+ }
2092
+ function useUserManagementPlugin(t0) {
2093
+ const $ = c(15);
2094
+ const {
2095
+ userManagement
2096
+ } = t0;
2097
+ const noUsers = userManagement.users.length === 0;
2098
+ const noRoles = userManagement.roles.length === 0;
2099
+ let t1;
2100
+ if ($[0] !== userManagement) {
2101
+ t1 = {
2102
+ ...userManagement
2103
+ };
2104
+ $[0] = userManagement;
2105
+ $[1] = t1;
2106
+ } else {
2107
+ t1 = $[1];
2108
+ }
2109
+ let t2;
2110
+ if ($[2] !== noRoles || $[3] !== noUsers || $[4] !== userManagement) {
2111
+ t2 = noUsers || noRoles ? /* @__PURE__ */ jsx(IntroWidget, { noUsers, noRoles, userManagement }) : void 0;
2112
+ $[2] = noRoles;
2113
+ $[3] = noUsers;
2114
+ $[4] = userManagement;
2115
+ $[5] = t2;
2116
+ } else {
2117
+ t2 = $[5];
2118
+ }
2119
+ let t3;
2120
+ if ($[6] !== t2) {
2121
+ t3 = {
2122
+ additionalChildrenStart: t2
2123
+ };
2124
+ $[6] = t2;
2125
+ $[7] = t3;
2126
+ } else {
2127
+ t3 = $[7];
2128
+ }
2129
+ let t4;
2130
+ if ($[8] !== userManagement) {
2131
+ t4 = {
2132
+ Component: UserManagementProvider,
1233
2133
  props: {
1234
- userManagement: t
2134
+ userManagement
1235
2135
  }
1236
- }
1237
- };
2136
+ };
2137
+ $[8] = userManagement;
2138
+ $[9] = t4;
2139
+ } else {
2140
+ t4 = $[9];
2141
+ }
2142
+ let t5;
2143
+ if ($[10] !== t1 || $[11] !== t3 || $[12] !== t4 || $[13] !== userManagement.loading) {
2144
+ t5 = {
2145
+ key: "user_management",
2146
+ loading: userManagement.loading,
2147
+ userManagement: t1,
2148
+ homePage: t3,
2149
+ provider: t4
2150
+ };
2151
+ $[10] = t1;
2152
+ $[11] = t3;
2153
+ $[12] = t4;
2154
+ $[13] = userManagement.loading;
2155
+ $[14] = t5;
2156
+ } else {
2157
+ t5 = $[14];
2158
+ }
2159
+ return t5;
1238
2160
  }
1239
- const Dn = [
1240
- {
1241
- path: "users",
1242
- name: "CMS Users",
1243
- group: "Admin",
1244
- icon: "face",
1245
- view: /* @__PURE__ */ e(an, {})
1246
- },
1247
- {
1248
- path: "roles",
1249
- name: "Roles",
1250
- group: "Admin",
1251
- icon: "gpp_good",
1252
- view: /* @__PURE__ */ e(tn, {})
1253
- }
1254
- ];
2161
+ function IntroWidget(t0) {
2162
+ const $ = c(17);
2163
+ const {
2164
+ noUsers,
2165
+ noRoles,
2166
+ userManagement
2167
+ } = t0;
2168
+ const authController = useAuthController();
2169
+ const snackbarController = useSnackbarController();
2170
+ const buttonLabel = noUsers && noRoles ? "Create default roles and add current user as admin" : noUsers ? "Add current user as admin" : noRoles ? "Create default roles" : void 0;
2171
+ let t1;
2172
+ let t2;
2173
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
2174
+ t1 = /* @__PURE__ */ jsx(Typography, { variant: "subtitle2", className: "uppercase", children: "Create your users and roles" });
2175
+ t2 = /* @__PURE__ */ jsx(Typography, { children: "You have no users or roles defined. You can create default roles and add the current user as admin." });
2176
+ $[0] = t1;
2177
+ $[1] = t2;
2178
+ } else {
2179
+ t1 = $[0];
2180
+ t2 = $[1];
2181
+ }
2182
+ let t3;
2183
+ if ($[2] !== authController.user?.displayName || $[3] !== authController.user?.email || $[4] !== authController.user?.isAnonymous || $[5] !== authController.user?.photoURL || $[6] !== authController.user?.providerId || $[7] !== authController.user?.uid || $[8] !== noRoles || $[9] !== noUsers || $[10] !== snackbarController || $[11] !== userManagement) {
2184
+ t3 = () => {
2185
+ if (!authController.user?.uid) {
2186
+ throw Error("UsersTable, authController misconfiguration");
2187
+ }
2188
+ if (noUsers) {
2189
+ userManagement.saveUser({
2190
+ uid: authController.user?.uid,
2191
+ email: authController.user?.email,
2192
+ displayName: authController.user?.displayName,
2193
+ photoURL: authController.user?.photoURL,
2194
+ providerId: authController.user?.providerId,
2195
+ isAnonymous: authController.user?.isAnonymous,
2196
+ roles: [{
2197
+ id: "admin",
2198
+ name: "Admin"
2199
+ }],
2200
+ created_on: /* @__PURE__ */ new Date()
2201
+ }).then(() => {
2202
+ snackbarController.open({
2203
+ type: "success",
2204
+ message: "User added successfully"
2205
+ });
2206
+ }).catch((error) => {
2207
+ snackbarController.open({
2208
+ type: "error",
2209
+ message: "Error adding user: " + error.message
2210
+ });
2211
+ });
2212
+ }
2213
+ if (noRoles) {
2214
+ DEFAULT_ROLES.forEach((role) => {
2215
+ userManagement.saveRole(role);
2216
+ });
2217
+ }
2218
+ };
2219
+ $[2] = authController.user?.displayName;
2220
+ $[3] = authController.user?.email;
2221
+ $[4] = authController.user?.isAnonymous;
2222
+ $[5] = authController.user?.photoURL;
2223
+ $[6] = authController.user?.providerId;
2224
+ $[7] = authController.user?.uid;
2225
+ $[8] = noRoles;
2226
+ $[9] = noUsers;
2227
+ $[10] = snackbarController;
2228
+ $[11] = userManagement;
2229
+ $[12] = t3;
2230
+ } else {
2231
+ t3 = $[12];
2232
+ }
2233
+ let t4;
2234
+ if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
2235
+ t4 = /* @__PURE__ */ jsx(AddIcon, {});
2236
+ $[13] = t4;
2237
+ } else {
2238
+ t4 = $[13];
2239
+ }
2240
+ let t5;
2241
+ if ($[14] !== buttonLabel || $[15] !== t3) {
2242
+ t5 = /* @__PURE__ */ jsxs(Paper, { className: "my-4 flex flex-col px-4 py-6 bg-white dark:bg-surface-accent-800 gap-2", children: [
2243
+ t1,
2244
+ t2,
2245
+ /* @__PURE__ */ jsxs(Button, { variant: "outlined", onClick: t3, children: [
2246
+ t4,
2247
+ buttonLabel
2248
+ ] })
2249
+ ] });
2250
+ $[14] = buttonLabel;
2251
+ $[15] = t3;
2252
+ $[16] = t5;
2253
+ } else {
2254
+ t5 = $[16];
2255
+ }
2256
+ return t5;
2257
+ }
2258
+ const userManagementAdminViews = [{
2259
+ path: "users",
2260
+ name: "CMS Users",
2261
+ group: "Admin",
2262
+ icon: "face",
2263
+ view: /* @__PURE__ */ jsx(UsersView, {})
2264
+ }, {
2265
+ path: "roles",
2266
+ name: "Roles",
2267
+ group: "Admin",
2268
+ icon: "gpp_good",
2269
+ view: /* @__PURE__ */ jsx(RolesView, {})
2270
+ }];
1255
2271
  export {
1256
- pn as RESERVED_GROUPS,
1257
- Q as RoleChip,
1258
- Xe as RoleYupSchema,
1259
- Ze as RolesDetailsForm,
1260
- nn as RolesTable,
1261
- tn as RolesView,
1262
- ln as UserDetailsForm,
1263
- Ue as UserManagementContext,
1264
- Qe as UserManagementProvider,
1265
- rn as UserYupSchema,
1266
- sn as UsersTable,
1267
- an as UsersView,
1268
- Je as areRolesEqual,
1269
- vn as cacheDelegatedLoginToken,
1270
- yn as clearDelegatedLoginTokensCache,
1271
- bn as darkenColor,
1272
- wn as getDelegatedLoginTokenFromCache,
1273
- Cn as getUserRoles,
1274
- Nn as hexToRgbaWithOpacity,
1275
- je as resolveUserRolePermissions,
1276
- Rn as useBuildFirestoreUserManagement,
1277
- W as useUserManagement,
1278
- xn as useUserManagementPlugin,
1279
- Dn as userManagementAdminViews
2272
+ IntroWidget,
2273
+ RESERVED_GROUPS,
2274
+ RoleChip,
2275
+ RoleYupSchema,
2276
+ RolesDetailsForm,
2277
+ RolesTable,
2278
+ RolesView,
2279
+ UserDetailsForm,
2280
+ UserManagementContext,
2281
+ UserManagementProvider,
2282
+ UserYupSchema,
2283
+ UsersTable,
2284
+ UsersView,
2285
+ areRolesEqual,
2286
+ cacheDelegatedLoginToken,
2287
+ clearDelegatedLoginTokensCache,
2288
+ darkenColor,
2289
+ getDelegatedLoginTokenFromCache,
2290
+ getUserRoles,
2291
+ hexToRgbaWithOpacity,
2292
+ resolveUserRolePermissions,
2293
+ useBuildUserManagement,
2294
+ useUserManagement,
2295
+ useUserManagementPlugin,
2296
+ userManagementAdminViews
1280
2297
  };
1281
2298
  //# sourceMappingURL=index.es.js.map