@firecms/user_management 3.0.0-canary.19 → 3.0.0-canary.191

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