@firecms/user_management 3.0.0-canary.16 → 3.0.0-canary.161

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