diff --git a/backend/models/user.model.js b/backend/models/user.model.js index 5ff3ddacd..4c2005cc6 100644 --- a/backend/models/user.model.js +++ b/backend/models/user.model.js @@ -11,7 +11,7 @@ const userSchema = mongoose.Schema({ email: { type: String, unique: true }, accessLevel: { type: String, - enum: ["user", "admin"], // restricts values to "user" and "admin" + enum: ["user", "admin", "superadmin"], // restricts values to "user", "admin" and "superadmin" default: "user" }, createdDate: { type: Date, default: Date.now }, diff --git a/client/src/components/user-admin/EditUsers.jsx b/client/src/components/user-admin/EditUsers.jsx index 929ec9055..894a3628c 100644 --- a/client/src/components/user-admin/EditUsers.jsx +++ b/client/src/components/user-admin/EditUsers.jsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; import '../../sass/UserAdmin.scss'; -import { FormGroup, FormControlLabel, Switch } from '@mui/material' +import { FormGroup, FormControlLabel, Switch } from '@mui/material'; // child of UserAdmin. Displays form to update users. const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUserActiveStatus, updateUserAccessLevel }) => { @@ -9,6 +9,9 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse const [isActive, setIsActive] = useState(userToEdit.isActive); const [isAdmin, setIsAdmin] = useState(userToEdit.accessLevel === "admin"); + // Boolean to check if the current user is the super admin + const isSuperAdmin = userToEdit.accessLevel === "superadmin"; + // Prepare data for display const userName = `${userToEdit.name?.firstName} ${userToEdit.name?.lastName}`; const userEmail = userToEdit.email; @@ -18,7 +21,6 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse const activeProjects = Object.values(projects) .filter((project) => project.projectStatus === 'Active') .sort((a, b) => a.name?.localeCompare(b.name)) - // eslint-disable-next-line no-underscore-dangle .map((p) => [p._id, p.name]); // add user projects to state @@ -35,11 +37,7 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse const onSubmit = (event) => { event.preventDefault(); - if ( - projectValue.length > 0 && - projectValue !== 'default' && - !userManagedProjects.includes(projectValue) - ) { + if (!isSuperAdmin && projectValue.length > 0 && projectValue !== 'default' && !userManagedProjects.includes(projectValue)) { const newProjects = [...userManagedProjects, projectValue]; updateUserDb(userToEdit, newProjects); setUserManagedProjects(newProjects); @@ -51,24 +49,26 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse // Remove projects from db const handleRemoveProject = (projectToRemove) => { - if (userManagedProjects.length > 0) { - const newProjects = userManagedProjects.filter( - (p) => p !== projectToRemove - ); + if (!isSuperAdmin && userManagedProjects.length > 0) { + const newProjects = userManagedProjects.filter((p) => p !== projectToRemove); updateUserDb(userToEdit, newProjects); setUserManagedProjects(newProjects); } }; const handleSetIsActive = () => { - setIsActive(!isActive) - updateUserActiveStatus(userToEdit, !isActive) - } + if (!isSuperAdmin) { + setIsActive(!isActive); + updateUserActiveStatus(userToEdit, !isActive); + } + }; const handleSetAccessLevel = () => { - const newAccessLevel = isAdmin ? "user" : "admin"; - setIsAdmin(!isAdmin); - updateUserAccessLevel(userToEdit, newAccessLevel); + if (!isSuperAdmin) { + const newAccessLevel = isAdmin ? "user" : "admin"; + setIsAdmin(!isAdmin); + updateUserAccessLevel(userToEdit, newAccessLevel); + } }; return ( @@ -86,16 +86,24 @@ const EditUsers = ({ userToEdit, backToSearch, updateUserDb, projects, updateUse