@firecms/user_management 3.0.0-canary.23 → 3.0.0-canary.231

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