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

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