@firecms/user_management 3.0.0-canary.24 → 3.0.0-canary.241

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,2278 @@
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, CheckIcon, 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
+ usersPath = "__FIRECMS/config/users",
149
+ rolesPath = "__FIRECMS/config/roles",
150
+ allowDefaultRolesCreation,
151
+ includeCollectionConfigPermissions
118
152
  }) {
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);
153
+ if (!authController) {
154
+ 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");
155
+ }
156
+ const [rolesLoading, setRolesLoading] = React.useState(true);
157
+ const [usersLoading, setUsersLoading] = React.useState(true);
158
+ const [roles, setRoles] = React.useState([]);
159
+ const [usersWithRoleIds, setUsersWithRoleIds] = React.useState([]);
160
+ const users = usersWithRoleIds.map((u) => ({
161
+ ...u,
162
+ roles: roles.filter((r) => u.roles?.includes(r.id))
163
+ }));
164
+ const [rolesError, setRolesError] = React.useState();
165
+ const [usersError, setUsersError] = React.useState();
166
+ const _usersLoading = usersLoading;
167
+ const _rolesLoading = rolesLoading;
168
+ const loading = _rolesLoading || _usersLoading;
169
+ useEffect(() => {
170
+ if (!dataSourceDelegate || !rolesPath) return;
171
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
172
+ if (authController?.initialLoading) return;
173
+ setRolesLoading(true);
174
+ return dataSourceDelegate.listenCollection?.({
175
+ path: rolesPath,
176
+ onUpdate(entities) {
177
+ setRolesError(void 0);
178
+ try {
179
+ const newRoles = entityToRoles(entities);
180
+ if (!equal(newRoles, roles)) {
181
+ setRoles(newRoles);
139
182
  }
140
- y(!1);
141
- },
142
- error: (v) => {
143
- L(v), y(!1);
183
+ } catch (e) {
184
+ setRoles([]);
185
+ console.error("Error loading roles", e);
186
+ setRolesError(e);
144
187
  }
188
+ setRolesLoading(false);
189
+ },
190
+ onError(e_0) {
191
+ setRoles([]);
192
+ console.error("Error loading roles", e_0);
193
+ setRolesError(e_0);
194
+ setRolesLoading(false);
145
195
  }
146
- );
147
- }, [t, n]), Z(() => {
148
- if (!t || !i)
196
+ });
197
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
198
+ useEffect(() => {
199
+ if (!dataSourceDelegate || !usersPath) return;
200
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) {
149
201
  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);
202
+ }
203
+ if (authController?.initialLoading) {
204
+ return;
205
+ }
206
+ setUsersLoading(true);
207
+ return dataSourceDelegate.listenCollection?.({
208
+ path: usersPath,
209
+ onUpdate(entities_0) {
210
+ console.debug("Updating users", entities_0);
211
+ setUsersError(void 0);
212
+ try {
213
+ const newUsers = entitiesToUsers(entities_0);
214
+ setUsersWithRoleIds(newUsers);
215
+ } catch (e_1) {
216
+ setUsersWithRoleIds([]);
217
+ console.error("Error loading users", e_1);
218
+ setUsersError(e_1);
166
219
  }
220
+ setUsersLoading(false);
221
+ },
222
+ onError(e_2) {
223
+ console.error("Error loading users", e_2);
224
+ setUsersWithRoleIds([]);
225
+ setUsersError(e_2);
226
+ setUsersLoading(false);
167
227
  }
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
228
+ });
229
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, usersPath]);
230
+ const saveUser = useCallback(async (user) => {
231
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
232
+ if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
233
+ console.debug("Persisting user", user);
234
+ const roleIds = user.roles?.map((r_0) => r_0.id);
235
+ const email = user.email?.toLowerCase().trim();
236
+ if (!email) throw Error("Email is required");
237
+ const userExists = users.find((u_0) => u_0.email?.toLowerCase() === email);
238
+ const data = {
239
+ ...user,
240
+ roles: roleIds ?? []
181
241
  };
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);
242
+ if (!userExists) {
243
+ data.created_on = /* @__PURE__ */ new Date();
244
+ }
245
+ if (userExists && userExists.uid !== user.uid) {
246
+ const entity = {
247
+ values: {},
248
+ path: usersPath,
249
+ id: userExists.uid
250
+ };
251
+ await dataSourceDelegate.deleteEntity({
252
+ entity
253
+ }).then(() => {
254
+ console.debug("Deleted previous user", userExists);
255
+ }).catch((e_3) => {
256
+ console.error("Error deleting user", e_3);
257
+ });
258
+ }
259
+ return dataSourceDelegate.saveEntity({
260
+ status: "existing",
261
+ path: usersPath,
262
+ entityId: email,
263
+ values: removeUndefined(data)
264
+ }).then(() => user);
265
+ }, [usersPath, dataSourceDelegate?.initialised]);
266
+ const saveRole = useCallback((role) => {
267
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
268
+ if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
269
+ console.debug("Persisting role", role);
188
270
  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]);
271
+ id,
272
+ ...roleData
273
+ } = role;
274
+ return dataSourceDelegate.saveEntity({
275
+ status: "existing",
276
+ path: rolesPath,
277
+ entityId: id,
278
+ values: removeUndefined(roleData)
279
+ }).then(() => {
280
+ return;
281
+ });
282
+ }, [rolesPath, dataSourceDelegate?.initialised]);
283
+ const deleteUser = useCallback(async (user_0) => {
284
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
285
+ if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
286
+ console.debug("Deleting", user_0);
287
+ const {
288
+ uid
289
+ } = user_0;
290
+ const entity_0 = {
291
+ path: usersPath,
292
+ id: uid,
293
+ values: {}
294
+ };
295
+ await dataSourceDelegate.deleteEntity({
296
+ entity: entity_0
297
+ });
298
+ }, [usersPath, dataSourceDelegate?.initialised]);
299
+ const deleteRole = useCallback(async (role_0) => {
300
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
301
+ if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
302
+ console.debug("Deleting", role_0);
303
+ const {
304
+ id: id_0
305
+ } = role_0;
306
+ const entity_1 = {
307
+ path: rolesPath,
308
+ id: id_0,
309
+ values: {}
310
+ };
311
+ await dataSourceDelegate.deleteEntity({
312
+ entity: entity_1
313
+ });
314
+ }, [rolesPath, dataSourceDelegate?.initialised]);
315
+ const collectionPermissions = useCallback(({
316
+ collection,
317
+ user: user_1
318
+ }) => resolveUserRolePermissions({
319
+ collection,
320
+ user: user_1
321
+ }), []);
322
+ const defineRolesFor = useCallback((user_2) => {
323
+ if (!usersWithRoleIds) throw Error("Users not loaded");
324
+ const users_0 = usersWithRoleIds.map((u_1) => ({
325
+ ...u_1,
326
+ roles: roles.filter((r_1) => u_1.roles?.includes(r_1.id))
327
+ }));
328
+ const mgmtUser = users_0.find((u_2) => u_2.email?.toLowerCase() === user_2?.email?.toLowerCase());
329
+ return mgmtUser?.roles;
330
+ }, [roles, usersWithRoleIds]);
331
+ const authenticator = useCallback(({
332
+ user: user_3
333
+ }) => {
334
+ if (loading) {
335
+ return false;
336
+ }
337
+ if (user_3 === null) {
338
+ console.warn("User is null, returning");
339
+ return false;
340
+ }
341
+ if (users.length === 0) {
342
+ console.warn("No users created yet");
343
+ return true;
344
+ }
345
+ const mgmtUser_0 = users.find((u_3) => u_3.email?.toLowerCase() === user_3?.email?.toLowerCase());
346
+ if (mgmtUser_0) {
347
+ if (mgmtUser_0.uid !== user_3.uid) {
348
+ console.warn("User uid has changed, updating user in user management system");
349
+ saveUser({
350
+ ...mgmtUser_0,
351
+ uid: user_3.uid
352
+ }).then(() => {
353
+ console.debug("User updated in user management system", mgmtUser_0);
354
+ }).catch((e_4) => {
355
+ console.error("Error updating user in user management system", e_4);
356
+ });
357
+ }
358
+ console.debug("User found in user management system", mgmtUser_0);
359
+ return true;
360
+ }
361
+ throw Error("Could not find a user with the provided email in the user management system.");
362
+ }, [loading, users]);
363
+ const userRoles = authController.user ? defineRolesFor(authController.user) : void 0;
364
+ const isAdmin = (userRoles ?? []).some((r_2) => r_2.id === "admin");
365
+ const userRoleIds = userRoles?.map((r_3) => r_3.id);
366
+ useEffect(() => {
367
+ console.debug("Setting roles", userRoles);
368
+ authController.setUserRoles?.(userRoles ?? []);
369
+ }, [userRoleIds]);
370
+ const getUser = useCallback((uid_0) => {
371
+ if (!users) return null;
372
+ const user_4 = users.find((u_4) => u_4.uid === uid_0);
373
+ return user_4 ?? null;
374
+ }, [users]);
224
375
  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
376
+ loading,
377
+ roles,
378
+ users,
379
+ saveUser,
380
+ saveRole,
381
+ rolesError,
382
+ deleteUser,
383
+ deleteRole,
384
+ usersError,
385
+ isAdmin,
386
+ allowDefaultRolesCreation: allowDefaultRolesCreation === void 0 ? true : allowDefaultRolesCreation,
387
+ includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
388
+ collectionPermissions,
389
+ defineRolesFor,
390
+ authenticator,
391
+ ...authController,
392
+ initialLoading: authController.initialLoading || loading,
393
+ userRoles,
394
+ getUser,
395
+ user: authController.user ? {
396
+ ...authController.user,
397
+ roles: userRoles
398
+ } : null
239
399
  };
240
400
  }
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 });
401
+ const entitiesToUsers = (docs) => {
402
+ return docs.map((doc) => {
403
+ const data = doc.values;
404
+ const newVar = {
405
+ uid: doc.id,
406
+ ...data,
407
+ created_on: data?.created_on,
408
+ updated_on: data?.updated_on
409
+ };
410
+ return newVar;
411
+ });
412
+ };
413
+ const entityToRoles = (entities) => {
414
+ return entities.map((doc) => ({
415
+ id: doc.id,
416
+ ...doc.values
417
+ }));
418
+ };
419
+ const UserManagementContext = React.createContext({});
420
+ function UserManagementProvider(t0) {
421
+ const $ = c(3);
422
+ const {
423
+ children,
424
+ userManagement
425
+ } = t0;
426
+ let t1;
427
+ if ($[0] !== children || $[1] !== userManagement) {
428
+ t1 = /* @__PURE__ */ jsx(UserManagementContext.Provider, { value: userManagement, children });
429
+ $[0] = children;
430
+ $[1] = userManagement;
431
+ $[2] = t1;
432
+ } else {
433
+ t1 = $[2];
434
+ }
435
+ return t1;
258
436
  }
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
- );
437
+ const useUserManagement = () => {
438
+ return useContext(UserManagementContext);
439
+ };
440
+ function RoleChip(t0) {
441
+ const $ = c(6);
442
+ const {
443
+ role
444
+ } = t0;
445
+ let colorScheme;
446
+ if (role.isAdmin) {
447
+ colorScheme = "blueDarker";
448
+ } else {
449
+ if (role.id === "editor") {
450
+ colorScheme = "yellowLight";
451
+ } else {
452
+ if (role.id === "viewer") {
453
+ colorScheme = "grayLight";
454
+ } else {
455
+ let t12;
456
+ if ($[0] !== role.id) {
457
+ t12 = getColorSchemeForSeed(role.id);
458
+ $[0] = role.id;
459
+ $[1] = t12;
460
+ } else {
461
+ t12 = $[1];
462
+ }
463
+ colorScheme = t12;
464
+ }
465
+ }
466
+ }
467
+ let t1;
468
+ if ($[2] !== colorScheme || $[3] !== role.id || $[4] !== role.name) {
469
+ t1 = /* @__PURE__ */ jsx(Chip, { colorScheme, children: role.name }, role.id);
470
+ $[2] = colorScheme;
471
+ $[3] = role.id;
472
+ $[4] = role.name;
473
+ $[5] = t1;
474
+ } else {
475
+ t1 = $[5];
476
+ }
477
+ return t1;
270
478
  }
271
- const Xe = _.object().shape({
272
- id: _.string().required("Required"),
273
- name: _.string().required("Required")
479
+ const RoleYupSchema = Yup.object().shape({
480
+ id: Yup.string().required("Required"),
481
+ name: Yup.string().required("Required")
274
482
  });
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 ?? {
483
+ function canRoleBeEdited(loggedUser) {
484
+ const loggedUserIsAdmin = loggedUser.roles?.map((r) => r.id).includes("admin");
485
+ if (!loggedUserIsAdmin) {
486
+ throw new Error("Only admins can edit roles");
487
+ }
488
+ return true;
489
+ }
490
+ function RolesDetailsForm(t0) {
491
+ const $ = c(165);
492
+ const {
493
+ open,
494
+ role,
495
+ editable,
496
+ handleClose,
497
+ collections
498
+ } = t0;
499
+ const {
500
+ saveRole
501
+ } = useUserManagement();
502
+ const isNewRole = !role;
503
+ const {
504
+ user: loggedInUser
505
+ } = useAuthController();
506
+ const [savingError, setSavingError] = useState();
507
+ let t1;
508
+ if ($[0] !== loggedInUser || $[1] !== saveRole) {
509
+ t1 = (role_0) => {
510
+ setSavingError(void 0);
511
+ if (!loggedInUser) {
512
+ throw new Error("User not found");
513
+ }
514
+ canRoleBeEdited(loggedInUser);
515
+ return saveRole(role_0);
516
+ };
517
+ $[0] = loggedInUser;
518
+ $[1] = saveRole;
519
+ $[2] = t1;
520
+ } else {
521
+ t1 = $[2];
522
+ }
523
+ const onRoleUpdated = t1;
524
+ let t2;
525
+ if ($[3] !== role) {
526
+ t2 = role ?? {
284
527
  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
- );
528
+ };
529
+ $[3] = role;
530
+ $[4] = t2;
531
+ } else {
532
+ t2 = $[4];
533
+ }
534
+ let t3;
535
+ if ($[5] !== handleClose || $[6] !== onRoleUpdated) {
536
+ t3 = (role_1, formexController) => {
537
+ try {
538
+ return onRoleUpdated(role_1).then(() => {
539
+ formexController.resetForm({
540
+ values: role_1
541
+ });
542
+ handleClose();
543
+ }).catch((e_0) => {
544
+ setSavingError(e_0);
545
+ });
546
+ } catch (t410) {
547
+ const e = t410;
548
+ setSavingError(e);
549
+ return Promise.resolve();
550
+ }
551
+ };
552
+ $[5] = handleClose;
553
+ $[6] = onRoleUpdated;
554
+ $[7] = t3;
555
+ } else {
556
+ t3 = $[7];
557
+ }
558
+ let t4;
559
+ if ($[8] !== t2 || $[9] !== t3) {
560
+ t4 = {
561
+ initialValues: t2,
562
+ onSubmit: t3,
563
+ validation: _temp3$1
564
+ };
565
+ $[8] = t2;
566
+ $[9] = t3;
567
+ $[10] = t4;
568
+ } else {
569
+ t4 = $[10];
570
+ }
571
+ const formex = useCreateFormex(t4);
572
+ const {
573
+ isSubmitting,
574
+ touched,
575
+ values: values_0,
576
+ errors: errors_0,
577
+ handleChange,
578
+ setFieldValue,
579
+ dirty,
580
+ setFieldTouched
581
+ } = formex;
582
+ const isAdmin = values_0.isAdmin ?? false;
583
+ const defaultCreate = values_0.defaultPermissions?.create ?? false;
584
+ const defaultRead = values_0.defaultPermissions?.read ?? false;
585
+ const defaultEdit = values_0.defaultPermissions?.edit ?? false;
586
+ const defaultDelete = values_0.defaultPermissions?.delete ?? false;
587
+ let t5;
588
+ if ($[11] !== setFieldValue || $[12] !== touched || $[13] !== values_0.name) {
589
+ t5 = () => {
590
+ const idTouched = getIn(touched, "id");
591
+ if (!idTouched && values_0.name) {
592
+ setFieldValue("id", toSnakeCase(values_0.name));
593
+ }
594
+ };
595
+ $[11] = setFieldValue;
596
+ $[12] = touched;
597
+ $[13] = values_0.name;
598
+ $[14] = t5;
599
+ } else {
600
+ t5 = $[14];
601
+ }
602
+ let t6;
603
+ if ($[15] !== touched || $[16] !== values_0.name) {
604
+ t6 = [touched, values_0.name];
605
+ $[15] = touched;
606
+ $[16] = values_0.name;
607
+ $[17] = t6;
608
+ } else {
609
+ t6 = $[17];
610
+ }
611
+ React.useEffect(t5, t6);
612
+ let t7;
613
+ let t8;
614
+ if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
615
+ t7 = {
616
+ display: "flex",
617
+ flexDirection: "column",
618
+ position: "relative",
619
+ height: "100%"
620
+ };
621
+ t8 = /* @__PURE__ */ jsx(DialogTitle, { variant: "h4", gutterBottom: false, children: "Role" });
622
+ $[18] = t7;
623
+ $[19] = t8;
624
+ } else {
625
+ t7 = $[18];
626
+ t8 = $[19];
627
+ }
628
+ const t9 = touched.name && Boolean(errors_0.name);
629
+ const t10 = isAdmin || !editable;
630
+ let t11;
631
+ if ($[20] !== handleChange || $[21] !== t10 || $[22] !== t9 || $[23] !== values_0.name) {
632
+ 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" });
633
+ $[20] = handleChange;
634
+ $[21] = t10;
635
+ $[22] = t9;
636
+ $[23] = values_0.name;
637
+ $[24] = t11;
638
+ } else {
639
+ t11 = $[24];
640
+ }
641
+ const t12 = touched.name && Boolean(errors_0.name) ? errors_0.name : "Name of this role";
642
+ let t13;
643
+ if ($[25] !== t12) {
644
+ t13 = /* @__PURE__ */ jsx(FieldCaption, { children: t12 });
645
+ $[25] = t12;
646
+ $[26] = t13;
647
+ } else {
648
+ t13 = $[26];
649
+ }
650
+ let t14;
651
+ if ($[27] !== t11 || $[28] !== t13) {
652
+ t14 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-8", children: [
653
+ t11,
654
+ t13
655
+ ] });
656
+ $[27] = t11;
657
+ $[28] = t13;
658
+ $[29] = t14;
659
+ } else {
660
+ t14 = $[29];
661
+ }
662
+ const t15 = touched.id && Boolean(errors_0.id);
663
+ const t16 = !isNewRole || !editable;
664
+ let t17;
665
+ if ($[30] !== handleChange || $[31] !== setFieldTouched) {
666
+ t17 = (e_2) => {
667
+ handleChange(e_2);
668
+ setFieldTouched("id", true);
669
+ };
670
+ $[30] = handleChange;
671
+ $[31] = setFieldTouched;
672
+ $[32] = t17;
673
+ } else {
674
+ t17 = $[32];
675
+ }
676
+ let t18;
677
+ if ($[33] !== t15 || $[34] !== t16 || $[35] !== t17 || $[36] !== values_0.id) {
678
+ 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" });
679
+ $[33] = t15;
680
+ $[34] = t16;
681
+ $[35] = t17;
682
+ $[36] = values_0.id;
683
+ $[37] = t18;
684
+ } else {
685
+ t18 = $[37];
686
+ }
687
+ const t19 = touched.id && Boolean(errors_0.id) ? errors_0.id : "ID of this role";
688
+ let t20;
689
+ if ($[38] !== t19) {
690
+ t20 = /* @__PURE__ */ jsx(FieldCaption, { children: t19 });
691
+ $[38] = t19;
692
+ $[39] = t20;
693
+ } else {
694
+ t20 = $[39];
695
+ }
696
+ let t21;
697
+ if ($[40] !== t18 || $[41] !== t20) {
698
+ t21 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
699
+ t18,
700
+ t20
701
+ ] });
702
+ $[40] = t18;
703
+ $[41] = t20;
704
+ $[42] = t21;
705
+ } else {
706
+ t21 = $[42];
707
+ }
708
+ let t22;
709
+ if ($[43] === Symbol.for("react.memo_cache_sentinel")) {
710
+ t22 = /* @__PURE__ */ jsxs(TableHeader, { className: "rounded-md", children: [
711
+ /* @__PURE__ */ jsx(TableCell, {}),
712
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Create entities" }),
713
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Read entities" }),
714
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Update entities" }),
715
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: "Delete entities" }),
716
+ /* @__PURE__ */ jsx(TableCell, { align: "center" })
717
+ ] });
718
+ $[43] = t22;
719
+ } else {
720
+ t22 = $[43];
721
+ }
722
+ let t23;
723
+ if ($[44] === Symbol.for("react.memo_cache_sentinel")) {
724
+ t23 = /* @__PURE__ */ jsx(TableCell, { scope: "row", children: /* @__PURE__ */ jsx("strong", { children: "All collections" }) });
725
+ $[44] = t23;
726
+ } else {
727
+ t23 = $[44];
728
+ }
729
+ const t24 = isAdmin || !editable;
730
+ const t25 = (isAdmin || defaultCreate) ?? false;
731
+ let t26;
732
+ if ($[45] !== setFieldValue) {
733
+ t26 = (checked) => setFieldValue("defaultPermissions.create", checked);
734
+ $[45] = setFieldValue;
735
+ $[46] = t26;
736
+ } else {
737
+ t26 = $[46];
738
+ }
739
+ let t27;
740
+ if ($[47] !== t24 || $[48] !== t25 || $[49] !== t26) {
741
+ 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 }) }) });
742
+ $[47] = t24;
743
+ $[48] = t25;
744
+ $[49] = t26;
745
+ $[50] = t27;
746
+ } else {
747
+ t27 = $[50];
748
+ }
749
+ const t28 = isAdmin || !editable;
750
+ const t29 = (isAdmin || defaultRead) ?? false;
751
+ let t30;
752
+ if ($[51] !== setFieldValue) {
753
+ t30 = (checked_0) => setFieldValue("defaultPermissions.read", checked_0);
754
+ $[51] = setFieldValue;
755
+ $[52] = t30;
756
+ } else {
757
+ t30 = $[52];
758
+ }
759
+ let t31;
760
+ if ($[53] !== t28 || $[54] !== t29 || $[55] !== t30) {
761
+ 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 }) }) });
762
+ $[53] = t28;
763
+ $[54] = t29;
764
+ $[55] = t30;
765
+ $[56] = t31;
766
+ } else {
767
+ t31 = $[56];
768
+ }
769
+ const t32 = isAdmin || !editable;
770
+ const t33 = (isAdmin || defaultEdit) ?? false;
771
+ let t34;
772
+ if ($[57] !== setFieldValue) {
773
+ t34 = (checked_1) => setFieldValue("defaultPermissions.edit", checked_1);
774
+ $[57] = setFieldValue;
775
+ $[58] = t34;
776
+ } else {
777
+ t34 = $[58];
778
+ }
779
+ let t35;
780
+ if ($[59] !== t32 || $[60] !== t33 || $[61] !== t34) {
781
+ 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 }) }) });
782
+ $[59] = t32;
783
+ $[60] = t33;
784
+ $[61] = t34;
785
+ $[62] = t35;
786
+ } else {
787
+ t35 = $[62];
788
+ }
789
+ const t36 = isAdmin || !editable;
790
+ const t37 = (isAdmin || defaultDelete) ?? false;
791
+ let t38;
792
+ if ($[63] !== setFieldValue) {
793
+ t38 = (checked_2) => setFieldValue("defaultPermissions.delete", checked_2);
794
+ $[63] = setFieldValue;
795
+ $[64] = t38;
796
+ } else {
797
+ t38 = $[64];
798
+ }
799
+ let t39;
800
+ if ($[65] !== t36 || $[66] !== t37 || $[67] !== t38) {
801
+ 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 }) }) });
802
+ $[65] = t36;
803
+ $[66] = t37;
804
+ $[67] = t38;
805
+ $[68] = t39;
806
+ } else {
807
+ t39 = $[68];
808
+ }
809
+ let t40;
810
+ if ($[69] === Symbol.for("react.memo_cache_sentinel")) {
811
+ t40 = /* @__PURE__ */ jsx(TableCell, { align: "center" });
812
+ $[69] = t40;
813
+ } else {
814
+ t40 = $[69];
815
+ }
816
+ let t41;
817
+ if ($[70] !== t27 || $[71] !== t31 || $[72] !== t35 || $[73] !== t39) {
818
+ t41 = /* @__PURE__ */ jsxs(TableRow, { children: [
819
+ t23,
820
+ t27,
821
+ t31,
822
+ t35,
823
+ t39,
824
+ t40
825
+ ] });
826
+ $[70] = t27;
827
+ $[71] = t31;
828
+ $[72] = t35;
829
+ $[73] = t39;
830
+ $[74] = t41;
831
+ } else {
832
+ t41 = $[74];
833
+ }
834
+ let t42;
835
+ if ($[75] !== collections || $[76] !== defaultCreate || $[77] !== defaultDelete || $[78] !== defaultEdit || $[79] !== defaultRead || $[80] !== editable || $[81] !== isAdmin || $[82] !== setFieldValue || $[83] !== values_0) {
836
+ t42 = collections && collections.map((col) => /* @__PURE__ */ jsxs(TableRow, { children: [
837
+ /* @__PURE__ */ jsx(TableCell, { scope: "row", children: col.name }),
838
+ /* @__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) }) }),
839
+ /* @__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) }) }),
840
+ /* @__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) }) }),
841
+ /* @__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) }) }),
842
+ /* @__PURE__ */ jsx(TableCell, { align: "center", children: /* @__PURE__ */ jsx(Tooltip, { title: "Allow all permissions in this collections", children: /* @__PURE__ */ jsx(Button, { className: "color-inherit", onClick: () => {
843
+ setFieldValue(`collectionPermissions.${col.id}.create`, true);
844
+ setFieldValue(`collectionPermissions.${col.id}.read`, true);
845
+ setFieldValue(`collectionPermissions.${col.id}.edit`, true);
846
+ setFieldValue(`collectionPermissions.${col.id}.delete`, true);
847
+ }, disabled: isAdmin || !editable, variant: "text", children: "All" }) }) })
848
+ ] }, col.name));
849
+ $[75] = collections;
850
+ $[76] = defaultCreate;
851
+ $[77] = defaultDelete;
852
+ $[78] = defaultEdit;
853
+ $[79] = defaultRead;
854
+ $[80] = editable;
855
+ $[81] = isAdmin;
856
+ $[82] = setFieldValue;
857
+ $[83] = values_0;
858
+ $[84] = t42;
859
+ } else {
860
+ t42 = $[84];
861
+ }
862
+ let t43;
863
+ if ($[85] !== t41 || $[86] !== t42) {
864
+ t43 = /* @__PURE__ */ jsx(Paper, { className: "bg-inherit overflow-hidden", children: /* @__PURE__ */ jsxs(Table, { className: "w-full rounded-md", children: [
865
+ t22,
866
+ /* @__PURE__ */ jsxs(TableBody, { children: [
867
+ t41,
868
+ t42
869
+ ] })
870
+ ] }) });
871
+ $[85] = t41;
872
+ $[86] = t42;
873
+ $[87] = t43;
874
+ } else {
875
+ t43 = $[87];
876
+ }
877
+ let t44;
878
+ if ($[88] === Symbol.for("react.memo_cache_sentinel")) {
879
+ 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" });
880
+ $[88] = t44;
881
+ } else {
882
+ t44 = $[88];
883
+ }
884
+ let t45;
885
+ if ($[89] !== t43) {
886
+ t45 = /* @__PURE__ */ jsxs("div", { className: "col-span-12", children: [
887
+ t43,
888
+ t44
889
+ ] });
890
+ $[89] = t43;
891
+ $[90] = t45;
892
+ } else {
893
+ t45 = $[90];
894
+ }
895
+ const t46 = touched.config && Boolean(errors_0.config);
896
+ const t47 = isAdmin || !editable;
897
+ let t48;
898
+ if ($[91] !== setFieldValue) {
899
+ t48 = (event) => setFieldValue("config.createCollections", event.target.value === "true");
900
+ $[91] = setFieldValue;
901
+ $[92] = t48;
902
+ } else {
903
+ t48 = $[92];
904
+ }
905
+ const t49 = isAdmin || values_0.config?.createCollections ? "true" : "false";
906
+ let t50;
907
+ let t51;
908
+ if ($[93] === Symbol.for("react.memo_cache_sentinel")) {
909
+ t50 = /* @__PURE__ */ jsx(SelectItem, { value: "true", children: " Yes " });
910
+ t51 = /* @__PURE__ */ jsx(SelectItem, { value: "false", children: " No " });
911
+ $[93] = t50;
912
+ $[94] = t51;
913
+ } else {
914
+ t50 = $[93];
915
+ t51 = $[94];
916
+ }
917
+ let t52;
918
+ if ($[95] !== t46 || $[96] !== t47 || $[97] !== t48 || $[98] !== t49) {
919
+ 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: [
920
+ t50,
921
+ t51
922
+ ] });
923
+ $[95] = t46;
924
+ $[96] = t47;
925
+ $[97] = t48;
926
+ $[98] = t49;
927
+ $[99] = t52;
928
+ } else {
929
+ t52 = $[99];
930
+ }
931
+ const t53 = touched.config && Boolean(errors_0.config) ? errors_0.config : "Can the user create collections";
932
+ let t54;
933
+ if ($[100] !== t53) {
934
+ t54 = /* @__PURE__ */ jsx(FieldCaption, { children: t53 });
935
+ $[100] = t53;
936
+ $[101] = t54;
937
+ } else {
938
+ t54 = $[101];
939
+ }
940
+ let t55;
941
+ if ($[102] !== t52 || $[103] !== t54) {
942
+ t55 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
943
+ t52,
944
+ t54
945
+ ] });
946
+ $[102] = t52;
947
+ $[103] = t54;
948
+ $[104] = t55;
949
+ } else {
950
+ t55 = $[104];
951
+ }
952
+ const t56 = touched.config && Boolean(errors_0.config);
953
+ const t57 = isAdmin || !editable;
954
+ let t58;
955
+ if ($[105] !== setFieldValue) {
956
+ t58 = (event_0) => setFieldValue("config.editCollections", event_0.target.value === "own" ? "own" : event_0.target.value === "true");
957
+ $[105] = setFieldValue;
958
+ $[106] = t58;
959
+ } else {
960
+ t58 = $[106];
961
+ }
962
+ const t59 = isAdmin ? "true" : values_0.config?.editCollections === "own" ? "own" : values_0.config?.editCollections ? "true" : "false";
963
+ let t60;
964
+ let t61;
965
+ let t62;
966
+ if ($[107] === Symbol.for("react.memo_cache_sentinel")) {
967
+ t60 = /* @__PURE__ */ jsx(SelectItem, { value: "true", children: " Yes " });
968
+ t61 = /* @__PURE__ */ jsx(SelectItem, { value: "false", children: " No " });
969
+ t62 = /* @__PURE__ */ jsx(SelectItem, { value: "own", children: " Only his/her own " });
970
+ $[107] = t60;
971
+ $[108] = t61;
972
+ $[109] = t62;
973
+ } else {
974
+ t60 = $[107];
975
+ t61 = $[108];
976
+ t62 = $[109];
977
+ }
978
+ let t63;
979
+ if ($[110] !== t56 || $[111] !== t57 || $[112] !== t58 || $[113] !== t59) {
980
+ 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: [
981
+ t60,
982
+ t61,
983
+ t62
984
+ ] });
985
+ $[110] = t56;
986
+ $[111] = t57;
987
+ $[112] = t58;
988
+ $[113] = t59;
989
+ $[114] = t63;
990
+ } else {
991
+ t63 = $[114];
992
+ }
993
+ const t64 = touched.config && Boolean(errors_0.config) ? errors_0.config : "Can the user edit collections";
994
+ let t65;
995
+ if ($[115] !== t64) {
996
+ t65 = /* @__PURE__ */ jsx(FieldCaption, { children: t64 });
997
+ $[115] = t64;
998
+ $[116] = t65;
999
+ } else {
1000
+ t65 = $[116];
1001
+ }
1002
+ let t66;
1003
+ if ($[117] !== t63 || $[118] !== t65) {
1004
+ t66 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
1005
+ t63,
1006
+ t65
1007
+ ] });
1008
+ $[117] = t63;
1009
+ $[118] = t65;
1010
+ $[119] = t66;
1011
+ } else {
1012
+ t66 = $[119];
1013
+ }
1014
+ const t67 = touched.config && Boolean(errors_0.config);
1015
+ const t68 = isAdmin || !editable;
1016
+ let t69;
1017
+ if ($[120] !== setFieldValue) {
1018
+ t69 = (event_1) => setFieldValue("config.deleteCollections", event_1.target.value === "own" ? "own" : event_1.target.value === "true");
1019
+ $[120] = setFieldValue;
1020
+ $[121] = t69;
1021
+ } else {
1022
+ t69 = $[121];
1023
+ }
1024
+ const t70 = isAdmin ? "true" : values_0.config?.deleteCollections === "own" ? "own" : values_0.config?.deleteCollections ? "true" : "false";
1025
+ let t71;
1026
+ let t72;
1027
+ let t73;
1028
+ if ($[122] === Symbol.for("react.memo_cache_sentinel")) {
1029
+ t71 = /* @__PURE__ */ jsx(SelectItem, { value: "true", children: " Yes " });
1030
+ t72 = /* @__PURE__ */ jsx(SelectItem, { value: "false", children: " No " });
1031
+ t73 = /* @__PURE__ */ jsx(SelectItem, { value: "own", children: " Only his/her own " });
1032
+ $[122] = t71;
1033
+ $[123] = t72;
1034
+ $[124] = t73;
1035
+ } else {
1036
+ t71 = $[122];
1037
+ t72 = $[123];
1038
+ t73 = $[124];
1039
+ }
1040
+ let t74;
1041
+ if ($[125] !== t67 || $[126] !== t68 || $[127] !== t69 || $[128] !== t70) {
1042
+ 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: [
1043
+ t71,
1044
+ t72,
1045
+ t73
1046
+ ] });
1047
+ $[125] = t67;
1048
+ $[126] = t68;
1049
+ $[127] = t69;
1050
+ $[128] = t70;
1051
+ $[129] = t74;
1052
+ } else {
1053
+ t74 = $[129];
1054
+ }
1055
+ const t75 = touched.config && Boolean(errors_0.config) ? errors_0.config : "Can the user delete collections";
1056
+ let t76;
1057
+ if ($[130] !== t75) {
1058
+ t76 = /* @__PURE__ */ jsx(FieldCaption, { children: t75 });
1059
+ $[130] = t75;
1060
+ $[131] = t76;
1061
+ } else {
1062
+ t76 = $[131];
1063
+ }
1064
+ let t77;
1065
+ if ($[132] !== t74 || $[133] !== t76) {
1066
+ t77 = /* @__PURE__ */ jsxs("div", { className: "col-span-12 md:col-span-4", children: [
1067
+ t74,
1068
+ t76
1069
+ ] });
1070
+ $[132] = t74;
1071
+ $[133] = t76;
1072
+ $[134] = t77;
1073
+ } else {
1074
+ t77 = $[134];
1075
+ }
1076
+ let t78;
1077
+ if ($[135] !== t14 || $[136] !== t21 || $[137] !== t45 || $[138] !== t55 || $[139] !== t66 || $[140] !== t77) {
1078
+ t78 = /* @__PURE__ */ jsx(DialogContent, { className: "flex-grow", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-12 gap-8", children: [
1079
+ t14,
1080
+ t21,
1081
+ t45,
1082
+ t55,
1083
+ t66,
1084
+ t77
1085
+ ] }) });
1086
+ $[135] = t14;
1087
+ $[136] = t21;
1088
+ $[137] = t45;
1089
+ $[138] = t55;
1090
+ $[139] = t66;
1091
+ $[140] = t77;
1092
+ $[141] = t78;
1093
+ } else {
1094
+ t78 = $[141];
1095
+ }
1096
+ let t79;
1097
+ if ($[142] !== savingError) {
1098
+ t79 = savingError && /* @__PURE__ */ jsx(Typography, { className: "text-red-500 dark:text-red-500", children: savingError.message ?? "There was an error saving this role" });
1099
+ $[142] = savingError;
1100
+ $[143] = t79;
1101
+ } else {
1102
+ t79 = $[143];
1103
+ }
1104
+ let t80;
1105
+ if ($[144] !== handleClose) {
1106
+ t80 = /* @__PURE__ */ jsx(Button, { variant: "text", onClick: () => {
1107
+ handleClose();
1108
+ }, children: "Cancel" });
1109
+ $[144] = handleClose;
1110
+ $[145] = t80;
1111
+ } else {
1112
+ t80 = $[145];
1113
+ }
1114
+ const t81 = !dirty;
1115
+ let t82;
1116
+ if ($[146] === Symbol.for("react.memo_cache_sentinel")) {
1117
+ t82 = /* @__PURE__ */ jsx(CheckIcon, {});
1118
+ $[146] = t82;
1119
+ } else {
1120
+ t82 = $[146];
1121
+ }
1122
+ const t83 = isNewRole ? "Create role" : "Update";
1123
+ let t84;
1124
+ if ($[147] !== isSubmitting || $[148] !== t81 || $[149] !== t83) {
1125
+ t84 = /* @__PURE__ */ jsx(LoadingButton, { variant: "filled", color: "primary", type: "submit", disabled: t81, loading: isSubmitting, startIcon: t82, children: t83 });
1126
+ $[147] = isSubmitting;
1127
+ $[148] = t81;
1128
+ $[149] = t83;
1129
+ $[150] = t84;
1130
+ } else {
1131
+ t84 = $[150];
1132
+ }
1133
+ let t85;
1134
+ if ($[151] !== t79 || $[152] !== t80 || $[153] !== t84) {
1135
+ t85 = /* @__PURE__ */ jsxs(DialogActions, { position: "sticky", children: [
1136
+ t79,
1137
+ t80,
1138
+ t84
1139
+ ] });
1140
+ $[151] = t79;
1141
+ $[152] = t80;
1142
+ $[153] = t84;
1143
+ $[154] = t85;
1144
+ } else {
1145
+ t85 = $[154];
1146
+ }
1147
+ let t86;
1148
+ if ($[155] !== formex.handleSubmit || $[156] !== t78 || $[157] !== t85) {
1149
+ t86 = /* @__PURE__ */ jsxs("form", { noValidate: true, autoComplete: "off", onSubmit: formex.handleSubmit, style: t7, children: [
1150
+ t8,
1151
+ t78,
1152
+ t85
1153
+ ] });
1154
+ $[155] = formex.handleSubmit;
1155
+ $[156] = t78;
1156
+ $[157] = t85;
1157
+ $[158] = t86;
1158
+ } else {
1159
+ t86 = $[158];
1160
+ }
1161
+ let t87;
1162
+ if ($[159] !== formex || $[160] !== t86) {
1163
+ t87 = /* @__PURE__ */ jsx(Formex, { value: formex, children: t86 });
1164
+ $[159] = formex;
1165
+ $[160] = t86;
1166
+ $[161] = t87;
1167
+ } else {
1168
+ t87 = $[161];
1169
+ }
1170
+ let t88;
1171
+ if ($[162] !== open || $[163] !== t87) {
1172
+ t88 = /* @__PURE__ */ jsx(Dialog, { open, maxWidth: "4xl", children: t87 });
1173
+ $[162] = open;
1174
+ $[163] = t87;
1175
+ $[164] = t88;
1176
+ } else {
1177
+ t88 = $[164];
1178
+ }
1179
+ return t88;
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$2).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$2() {
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" })
892
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];
893
1450
  }
894
- ), rn = _.object().shape({
895
- displayName: _.string().required("Required"),
896
- email: _.string().email().required("Required"),
897
- roles: _.array().min(1)
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
1458
+ ] });
1459
+ $[10] = children;
1460
+ $[11] = t7;
1461
+ $[12] = t8;
1462
+ } else {
1463
+ t8 = $[12];
1464
+ }
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(74);
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$1)
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-8", 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", onClick: () => {
1725
+ handleClose();
1726
+ }, children: "Cancel" });
1727
+ $[53] = handleClose;
1728
+ $[54] = t25;
1729
+ } else {
1730
+ t25 = $[54];
1731
+ }
1732
+ const t26 = !dirty;
1733
+ let t27;
1734
+ if ($[55] === Symbol.for("react.memo_cache_sentinel")) {
1735
+ t27 = /* @__PURE__ */ jsx(CheckIcon, {});
1736
+ $[55] = t27;
1737
+ } else {
1738
+ t27 = $[55];
1739
+ }
1740
+ const t28 = isNewUser ? "Create user" : "Update";
1741
+ let t29;
1742
+ if ($[56] !== isSubmitting || $[57] !== t26 || $[58] !== t28) {
1743
+ t29 = /* @__PURE__ */ jsx(LoadingButton, { variant: "filled", color: "primary", type: "submit", disabled: t26, loading: isSubmitting, startIcon: t27, children: t28 });
1744
+ $[56] = isSubmitting;
1745
+ $[57] = t26;
1746
+ $[58] = t28;
1747
+ $[59] = t29;
1748
+ } else {
1749
+ t29 = $[59];
1750
+ }
1751
+ let t30;
1752
+ if ($[60] !== t25 || $[61] !== t29) {
1753
+ t30 = /* @__PURE__ */ jsxs(DialogActions, { children: [
1754
+ t25,
1755
+ t29
1756
+ ] });
1757
+ $[60] = t25;
1758
+ $[61] = t29;
1759
+ $[62] = t30;
1760
+ } else {
1761
+ t30 = $[62];
1762
+ }
1763
+ let t31;
1764
+ if ($[63] !== handleSubmit || $[64] !== t24 || $[65] !== t30) {
1765
+ t31 = /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, autoComplete: "off", noValidate: true, style: t6, children: [
1766
+ t7,
1767
+ t24,
1768
+ t30
1769
+ ] });
1770
+ $[63] = handleSubmit;
1771
+ $[64] = t24;
1772
+ $[65] = t30;
1773
+ $[66] = t31;
1774
+ } else {
1775
+ t31 = $[66];
1776
+ }
1777
+ let t32;
1778
+ if ($[67] !== formex || $[68] !== t31) {
1779
+ t32 = /* @__PURE__ */ jsx(Formex, { value: formex, children: t31 });
1780
+ $[67] = formex;
1781
+ $[68] = t31;
1782
+ $[69] = t32;
1783
+ } else {
1784
+ t32 = $[69];
1785
+ }
1786
+ let t33;
1787
+ if ($[70] !== open || $[71] !== t32 || $[72] !== t5) {
1788
+ t33 = /* @__PURE__ */ jsx(Dialog, { open, onOpenChange: t5, maxWidth: "4xl", children: t32 });
1789
+ $[70] = open;
1790
+ $[71] = t32;
1791
+ $[72] = t5;
1792
+ $[73] = t33;
1793
+ } else {
1794
+ t33 = $[73];
1795
+ }
1796
+ return t33;
1797
+ }
1798
+ function _temp7(userRole) {
1799
+ return /* @__PURE__ */ jsx(MultiSelectItem, { value: userRole.id, children: /* @__PURE__ */ jsx(RoleChip, { role: userRole }, userRole?.id) }, userRole.id);
1800
+ }
1801
+ function _temp6(r_0) {
1802
+ return r_0.id;
1803
+ }
1804
+ function _temp5(values) {
1805
+ return UserYupSchema.validate(values, {
1806
+ abortEarly: false
1807
+ }).then(_temp2).catch(_temp4);
1808
+ }
1809
+ function _temp4(e_0) {
1810
+ return e_0.inner.reduce(_temp3, {});
1811
+ }
1812
+ function _temp3(acc, error) {
1813
+ acc[error.path] = error.message;
1814
+ return acc;
1815
+ }
1816
+ function _temp2() {
1817
+ return {};
1070
1818
  }
1071
- function sn({ onUserClicked: t }) {
1819
+ function _temp$1(r) {
1820
+ return r.id === "editor";
1821
+ }
1822
+ function UsersTable(t0) {
1823
+ const $ = c(28);
1824
+ const {
1825
+ onUserClicked
1826
+ } = t0;
1072
1827
  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
- ] }) }) })
1828
+ users,
1829
+ saveUser,
1830
+ deleteUser
1831
+ } = useUserManagement();
1832
+ const authController = useAuthController();
1833
+ const snackbarController = useSnackbarController();
1834
+ const customizationController = useCustomizationController();
1835
+ const dateUtilsLocale = customizationController?.locale ? locales[customizationController?.locale] : void 0;
1836
+ const dateFormat = customizationController?.dateTimeFormat ?? defaultDateFormat;
1837
+ const [userToBeDeleted, setUserToBeDeleted] = useState(void 0);
1838
+ const [deleteInProgress, setDeleteInProgress] = useState(false);
1839
+ let t1;
1840
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
1841
+ t1 = /* @__PURE__ */ jsxs(TableHeader, { children: [
1842
+ /* @__PURE__ */ jsx(TableCell, { className: "truncate w-16" }),
1843
+ /* @__PURE__ */ jsx(TableCell, { children: "Email" }),
1844
+ /* @__PURE__ */ jsx(TableCell, { children: "Name" }),
1845
+ /* @__PURE__ */ jsx(TableCell, { children: "Roles" }),
1846
+ /* @__PURE__ */ jsx(TableCell, { children: "Created on" })
1847
+ ] });
1848
+ $[0] = t1;
1849
+ } else {
1850
+ t1 = $[0];
1851
+ }
1852
+ let t2;
1853
+ if ($[1] !== dateFormat || $[2] !== dateUtilsLocale || $[3] !== onUserClicked || $[4] !== users) {
1854
+ t2 = users && users.map((user) => {
1855
+ const userRoles = user.roles;
1856
+ const formattedDate = user.created_on ? format(user.created_on, dateFormat, {
1857
+ locale: dateUtilsLocale
1858
+ }) : "";
1859
+ return /* @__PURE__ */ jsxs(TableRow, { onClick: () => {
1860
+ onUserClicked(user);
1861
+ }, children: [
1862
+ /* @__PURE__ */ jsx(TableCell, { className: "w-10", children: /* @__PURE__ */ jsx(Tooltip, { asChild: true, title: "Delete this user", children: /* @__PURE__ */ jsx(IconButton, { size: "small", onClick: (event) => {
1863
+ event.stopPropagation();
1864
+ return setUserToBeDeleted(user);
1865
+ }, children: /* @__PURE__ */ jsx(DeleteIcon, {}) }) }) }),
1866
+ /* @__PURE__ */ jsx(TableCell, { children: user.email }),
1867
+ /* @__PURE__ */ jsx(TableCell, { className: "font-medium align-left", children: user.displayName }),
1868
+ /* @__PURE__ */ jsx(TableCell, { className: "align-left", children: userRoles ? /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-2", children: userRoles.map(_temp) }) : null }),
1869
+ /* @__PURE__ */ jsx(TableCell, { children: formattedDate })
1870
+ ] }, "row_" + user.uid);
1871
+ });
1872
+ $[1] = dateFormat;
1873
+ $[2] = dateUtilsLocale;
1874
+ $[3] = onUserClicked;
1875
+ $[4] = users;
1876
+ $[5] = t2;
1877
+ } else {
1878
+ t2 = $[5];
1879
+ }
1880
+ let t3;
1881
+ if ($[6] !== authController || $[7] !== saveUser || $[8] !== snackbarController || $[9] !== users) {
1882
+ 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: [
1883
+ /* @__PURE__ */ jsx(Typography, { variant: "label", children: "There are no users yet" }),
1884
+ /* @__PURE__ */ jsx(Button, { variant: "outlined", onClick: () => {
1885
+ if (!authController.user?.uid) {
1886
+ throw Error("UsersTable, authController misconfiguration");
1887
+ }
1888
+ saveUser({
1889
+ uid: authController.user?.uid,
1890
+ email: authController.user?.email,
1891
+ displayName: authController.user?.displayName,
1892
+ photoURL: authController.user?.photoURL,
1893
+ providerId: authController.user?.providerId,
1894
+ isAnonymous: authController.user?.isAnonymous,
1895
+ roles: [{
1896
+ id: "admin",
1897
+ name: "Admin"
1898
+ }],
1899
+ created_on: /* @__PURE__ */ new Date()
1900
+ }).then(() => {
1901
+ snackbarController.open({
1902
+ type: "success",
1903
+ message: "User added successfully"
1904
+ });
1905
+ }).catch((error) => {
1906
+ snackbarController.open({
1907
+ type: "error",
1908
+ message: "Error adding user: " + error.message
1909
+ });
1910
+ });
1911
+ }, children: "Add the logged user as an admin" })
1912
+ ] }) }) });
1913
+ $[6] = authController;
1914
+ $[7] = saveUser;
1915
+ $[8] = snackbarController;
1916
+ $[9] = users;
1917
+ $[10] = t3;
1918
+ } else {
1919
+ t3 = $[10];
1920
+ }
1921
+ let t4;
1922
+ if ($[11] !== t2 || $[12] !== t3) {
1923
+ t4 = /* @__PURE__ */ jsxs(Table, { className: "w-full", children: [
1924
+ t1,
1925
+ /* @__PURE__ */ jsxs(TableBody, { children: [
1926
+ t2,
1927
+ t3
1151
1928
  ] })
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?" })
1929
+ ] });
1930
+ $[11] = t2;
1931
+ $[12] = t3;
1932
+ $[13] = t4;
1933
+ } else {
1934
+ t4 = $[13];
1935
+ }
1936
+ const t5 = Boolean(userToBeDeleted);
1937
+ let t6;
1938
+ if ($[14] !== deleteUser || $[15] !== snackbarController || $[16] !== userToBeDeleted) {
1939
+ t6 = () => {
1940
+ if (userToBeDeleted) {
1941
+ setDeleteInProgress(true);
1942
+ deleteUser(userToBeDeleted).then(() => {
1943
+ setUserToBeDeleted(void 0);
1944
+ }).catch((error_0) => {
1945
+ snackbarController.open({
1946
+ type: "error",
1947
+ message: "Error deleting user: " + error_0.message
1948
+ });
1949
+ }).finally(() => {
1950
+ setDeleteInProgress(false);
1951
+ });
1175
1952
  }
1176
- )
1177
- ] });
1953
+ };
1954
+ $[14] = deleteUser;
1955
+ $[15] = snackbarController;
1956
+ $[16] = userToBeDeleted;
1957
+ $[17] = t6;
1958
+ } else {
1959
+ t6 = $[17];
1960
+ }
1961
+ let t7;
1962
+ let t8;
1963
+ let t9;
1964
+ if ($[18] === Symbol.for("react.memo_cache_sentinel")) {
1965
+ t7 = () => {
1966
+ setUserToBeDeleted(void 0);
1967
+ };
1968
+ t8 = /* @__PURE__ */ jsx(Fragment, { children: "Delete?" });
1969
+ t9 = /* @__PURE__ */ jsx(Fragment, { children: "Are you sure you want to delete this user?" });
1970
+ $[18] = t7;
1971
+ $[19] = t8;
1972
+ $[20] = t9;
1973
+ } else {
1974
+ t7 = $[18];
1975
+ t8 = $[19];
1976
+ t9 = $[20];
1977
+ }
1978
+ let t10;
1979
+ if ($[21] !== deleteInProgress || $[22] !== t5 || $[23] !== t6) {
1980
+ t10 = /* @__PURE__ */ jsx(ConfirmationDialog, { open: t5, loading: deleteInProgress, onAccept: t6, onCancel: t7, title: t8, body: t9 });
1981
+ $[21] = deleteInProgress;
1982
+ $[22] = t5;
1983
+ $[23] = t6;
1984
+ $[24] = t10;
1985
+ } else {
1986
+ t10 = $[24];
1987
+ }
1988
+ let t11;
1989
+ if ($[25] !== t10 || $[26] !== t4) {
1990
+ t11 = /* @__PURE__ */ jsxs("div", { className: "overflow-auto", children: [
1991
+ t4,
1992
+ t10
1993
+ ] });
1994
+ $[25] = t10;
1995
+ $[26] = t4;
1996
+ $[27] = t11;
1997
+ } else {
1998
+ t11 = $[27];
1999
+ }
2000
+ return t11;
1178
2001
  }
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
- ] });
2002
+ function _temp(userRole) {
2003
+ return /* @__PURE__ */ jsx(RoleChip, { role: userRole }, userRole?.id);
2004
+ }
2005
+ const UsersView = function UsersView2(t0) {
2006
+ const $ = c(12);
2007
+ const {
2008
+ children
2009
+ } = t0;
2010
+ const [dialogOpen, setDialogOpen] = useState();
2011
+ const [selectedUser, setSelectedUser] = useState();
2012
+ useUserManagement();
2013
+ let t1;
2014
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
2015
+ t1 = (user) => {
2016
+ setSelectedUser(user);
2017
+ setDialogOpen(true);
2018
+ };
2019
+ $[0] = t1;
2020
+ } else {
2021
+ t1 = $[0];
2022
+ }
2023
+ const onUserClicked = t1;
2024
+ let t2;
2025
+ if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
2026
+ t2 = () => {
2027
+ setDialogOpen(false);
2028
+ setSelectedUser(void 0);
2029
+ };
2030
+ $[1] = t2;
2031
+ } else {
2032
+ t2 = $[1];
2033
+ }
2034
+ const handleClose = t2;
2035
+ let t3;
2036
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
2037
+ t3 = /* @__PURE__ */ jsx(Typography, { gutterBottom: true, variant: "h4", className: "flex-grow", component: "h4", children: "Users" });
2038
+ $[2] = t3;
2039
+ } else {
2040
+ t3 = $[2];
2041
+ }
2042
+ let t4;
2043
+ let t5;
2044
+ if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
2045
+ t4 = /* @__PURE__ */ jsxs("div", { className: "flex items-center mt-12", children: [
2046
+ t3,
2047
+ /* @__PURE__ */ jsx(Button, { size: "large", startIcon: /* @__PURE__ */ jsx(AddIcon, {}), onClick: () => setDialogOpen(true), children: "Add user" })
2048
+ ] });
2049
+ t5 = /* @__PURE__ */ jsx(UsersTable, { onUserClicked });
2050
+ $[3] = t4;
2051
+ $[4] = t5;
2052
+ } else {
2053
+ t4 = $[3];
2054
+ t5 = $[4];
2055
+ }
2056
+ const t6 = selectedUser?.uid ?? "new";
2057
+ const t7 = dialogOpen ?? false;
2058
+ let t8;
2059
+ if ($[5] !== selectedUser || $[6] !== t6 || $[7] !== t7) {
2060
+ t8 = /* @__PURE__ */ jsx(UserDetailsForm, { open: t7, user: selectedUser, handleClose }, t6);
2061
+ $[5] = selectedUser;
2062
+ $[6] = t6;
2063
+ $[7] = t7;
2064
+ $[8] = t8;
2065
+ } else {
2066
+ t8 = $[8];
2067
+ }
2068
+ let t9;
2069
+ if ($[9] !== children || $[10] !== t8) {
2070
+ t9 = /* @__PURE__ */ jsxs(Container, { className: "w-full flex flex-col py-4 gap-4", maxWidth: "6xl", children: [
2071
+ children,
2072
+ t4,
2073
+ t5,
2074
+ t8
2075
+ ] });
2076
+ $[9] = children;
2077
+ $[10] = t8;
2078
+ $[11] = t9;
2079
+ } else {
2080
+ t9 = $[11];
2081
+ }
2082
+ return t9;
1226
2083
  };
1227
- function xn({ userManagement: t }) {
1228
- return {
1229
- key: "user_management",
1230
- loading: t.loading,
1231
- provider: {
1232
- Component: Qe,
2084
+ function useUserManagementPlugin(t0) {
2085
+ const $ = c(12);
2086
+ const {
2087
+ userManagement
2088
+ } = t0;
2089
+ const noUsers = userManagement.users.length === 0;
2090
+ const noRoles = userManagement.roles.length === 0;
2091
+ let t1;
2092
+ if ($[0] !== noRoles || $[1] !== noUsers || $[2] !== userManagement) {
2093
+ t1 = noUsers || noRoles ? /* @__PURE__ */ jsx(IntroWidget, { noUsers, noRoles, userManagement }) : void 0;
2094
+ $[0] = noRoles;
2095
+ $[1] = noUsers;
2096
+ $[2] = userManagement;
2097
+ $[3] = t1;
2098
+ } else {
2099
+ t1 = $[3];
2100
+ }
2101
+ let t2;
2102
+ if ($[4] !== t1) {
2103
+ t2 = {
2104
+ additionalChildrenStart: t1
2105
+ };
2106
+ $[4] = t1;
2107
+ $[5] = t2;
2108
+ } else {
2109
+ t2 = $[5];
2110
+ }
2111
+ let t3;
2112
+ if ($[6] !== userManagement) {
2113
+ t3 = {
2114
+ Component: UserManagementProvider,
1233
2115
  props: {
1234
- userManagement: t
2116
+ userManagement
1235
2117
  }
1236
- }
1237
- };
2118
+ };
2119
+ $[6] = userManagement;
2120
+ $[7] = t3;
2121
+ } else {
2122
+ t3 = $[7];
2123
+ }
2124
+ let t4;
2125
+ if ($[8] !== t2 || $[9] !== t3 || $[10] !== userManagement.loading) {
2126
+ t4 = {
2127
+ key: "user_management",
2128
+ loading: userManagement.loading,
2129
+ homePage: t2,
2130
+ provider: t3
2131
+ };
2132
+ $[8] = t2;
2133
+ $[9] = t3;
2134
+ $[10] = userManagement.loading;
2135
+ $[11] = t4;
2136
+ } else {
2137
+ t4 = $[11];
2138
+ }
2139
+ return t4;
1238
2140
  }
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
- ];
2141
+ function IntroWidget(t0) {
2142
+ const $ = c(17);
2143
+ const {
2144
+ noUsers,
2145
+ noRoles,
2146
+ userManagement
2147
+ } = t0;
2148
+ const authController = useAuthController();
2149
+ const snackbarController = useSnackbarController();
2150
+ 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;
2151
+ let t1;
2152
+ let t2;
2153
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
2154
+ t1 = /* @__PURE__ */ jsx(Typography, { variant: "subtitle2", className: "uppercase", children: "Create your users and roles" });
2155
+ t2 = /* @__PURE__ */ jsx(Typography, { children: "You have no users or roles defined. You can create default roles and add the current user as admin." });
2156
+ $[0] = t1;
2157
+ $[1] = t2;
2158
+ } else {
2159
+ t1 = $[0];
2160
+ t2 = $[1];
2161
+ }
2162
+ let t3;
2163
+ 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) {
2164
+ t3 = () => {
2165
+ if (!authController.user?.uid) {
2166
+ throw Error("UsersTable, authController misconfiguration");
2167
+ }
2168
+ if (noUsers) {
2169
+ userManagement.saveUser({
2170
+ uid: authController.user?.uid,
2171
+ email: authController.user?.email,
2172
+ displayName: authController.user?.displayName,
2173
+ photoURL: authController.user?.photoURL,
2174
+ providerId: authController.user?.providerId,
2175
+ isAnonymous: authController.user?.isAnonymous,
2176
+ roles: [{
2177
+ id: "admin",
2178
+ name: "Admin"
2179
+ }],
2180
+ created_on: /* @__PURE__ */ new Date()
2181
+ }).then(() => {
2182
+ snackbarController.open({
2183
+ type: "success",
2184
+ message: "User added successfully"
2185
+ });
2186
+ }).catch((error) => {
2187
+ snackbarController.open({
2188
+ type: "error",
2189
+ message: "Error adding user: " + error.message
2190
+ });
2191
+ });
2192
+ }
2193
+ if (noRoles) {
2194
+ DEFAULT_ROLES.forEach((role) => {
2195
+ userManagement.saveRole(role);
2196
+ });
2197
+ }
2198
+ };
2199
+ $[2] = authController.user?.displayName;
2200
+ $[3] = authController.user?.email;
2201
+ $[4] = authController.user?.isAnonymous;
2202
+ $[5] = authController.user?.photoURL;
2203
+ $[6] = authController.user?.providerId;
2204
+ $[7] = authController.user?.uid;
2205
+ $[8] = noRoles;
2206
+ $[9] = noUsers;
2207
+ $[10] = snackbarController;
2208
+ $[11] = userManagement;
2209
+ $[12] = t3;
2210
+ } else {
2211
+ t3 = $[12];
2212
+ }
2213
+ let t4;
2214
+ if ($[13] === Symbol.for("react.memo_cache_sentinel")) {
2215
+ t4 = /* @__PURE__ */ jsx(AddIcon, {});
2216
+ $[13] = t4;
2217
+ } else {
2218
+ t4 = $[13];
2219
+ }
2220
+ let t5;
2221
+ if ($[14] !== buttonLabel || $[15] !== t3) {
2222
+ t5 = /* @__PURE__ */ jsxs(Paper, { className: "my-4 flex flex-col px-4 py-6 bg-white dark:bg-surface-accent-800 gap-2", children: [
2223
+ t1,
2224
+ t2,
2225
+ /* @__PURE__ */ jsxs(Button, { onClick: t3, children: [
2226
+ t4,
2227
+ buttonLabel
2228
+ ] })
2229
+ ] });
2230
+ $[14] = buttonLabel;
2231
+ $[15] = t3;
2232
+ $[16] = t5;
2233
+ } else {
2234
+ t5 = $[16];
2235
+ }
2236
+ return t5;
2237
+ }
2238
+ const userManagementAdminViews = [{
2239
+ path: "users",
2240
+ name: "CMS Users",
2241
+ group: "Admin",
2242
+ icon: "face",
2243
+ view: /* @__PURE__ */ jsx(UsersView, {})
2244
+ }, {
2245
+ path: "roles",
2246
+ name: "Roles",
2247
+ group: "Admin",
2248
+ icon: "gpp_good",
2249
+ view: /* @__PURE__ */ jsx(RolesView, {})
2250
+ }];
1255
2251
  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
2252
+ IntroWidget,
2253
+ RESERVED_GROUPS,
2254
+ RoleChip,
2255
+ RoleYupSchema,
2256
+ RolesDetailsForm,
2257
+ RolesTable,
2258
+ RolesView,
2259
+ UserDetailsForm,
2260
+ UserManagementContext,
2261
+ UserManagementProvider,
2262
+ UserYupSchema,
2263
+ UsersTable,
2264
+ UsersView,
2265
+ areRolesEqual,
2266
+ cacheDelegatedLoginToken,
2267
+ clearDelegatedLoginTokensCache,
2268
+ darkenColor,
2269
+ getDelegatedLoginTokenFromCache,
2270
+ getUserRoles,
2271
+ hexToRgbaWithOpacity,
2272
+ resolveUserRolePermissions,
2273
+ useBuildUserManagement,
2274
+ useUserManagement,
2275
+ useUserManagementPlugin,
2276
+ userManagementAdminViews
1280
2277
  };
1281
2278
  //# sourceMappingURL=index.es.js.map