grey out main admin and the user himself in avoidance of deleting the account by accident
This commit is contained in:
parent
3a5c203664
commit
2cc8eebf37
|
|
@ -375,7 +375,8 @@ public static class SessionMethods
|
||||||
var emailOwner = Db.GetUserByEmail(editedUser.Email);
|
var emailOwner = Db.GetUserByEmail(editedUser.Email);
|
||||||
|
|
||||||
return sessionUser.UserType != 0
|
return sessionUser.UserType != 0
|
||||||
&& originalUser.Id != 0 // never edit the root user
|
&& originalUser.Id != 0 // belt: legacy root-id sentinel
|
||||||
|
&& originalUser.ParentId > 0 // never edit the main/root admin (parentless top user; ParentId<=0, Id NOT necessarily 0)
|
||||||
&& (sessionUser.UserType == 2 || sessionUser.HasAccessTo(originalUser)) // admins may edit any user
|
&& (sessionUser.UserType == 2 || sessionUser.HasAccessTo(originalUser)) // admins may edit any user
|
||||||
&& (emailOwner is null || emailOwner.Id == editedUser.Id) // email not taken by another user
|
&& (emailOwner is null || emailOwner.Id == editedUser.Id) // email not taken by another user
|
||||||
&& editedUser
|
&& editedUser
|
||||||
|
|
@ -402,8 +403,9 @@ public static class SessionMethods
|
||||||
return sessionUser is not null
|
return sessionUser is not null
|
||||||
&& userToDelete is not null
|
&& userToDelete is not null
|
||||||
&& sessionUser.UserType !=0
|
&& sessionUser.UserType !=0
|
||||||
&& userToDelete.Id != 0 // never delete the root user
|
&& userToDelete.Id != 0 // belt: legacy root-id sentinel
|
||||||
&& userToDelete.Id != sessionUser.Id // never self-delete (avoid lockout)
|
&& userToDelete.ParentId > 0 // never delete the main/root admin (the parentless top user; ParentId<=0, Id NOT necessarily 0)
|
||||||
|
&& userToDelete.Id != sessionUser.Id // never self-delete (avoid lockout)
|
||||||
&& (sessionUser.UserType == 2 || sessionUser.HasAccessTo(userToDelete)) // admins may delete any user
|
&& (sessionUser.UserType == 2 || sessionUser.HasAccessTo(userToDelete)) // admins may delete any user
|
||||||
&& Db.Delete(userToDelete);
|
&& Db.Delete(userToDelete);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ import {
|
||||||
Tab,
|
Tab,
|
||||||
Tabs,
|
Tabs,
|
||||||
TextField,
|
TextField,
|
||||||
|
Tooltip,
|
||||||
Typography,
|
Typography,
|
||||||
useTheme
|
useTheme
|
||||||
} from '@mui/material';
|
} from '@mui/material';
|
||||||
|
|
@ -24,6 +25,7 @@ import Button from '@mui/material/Button';
|
||||||
import axiosConfig from 'src/Resources/axiosConfig';
|
import axiosConfig from 'src/Resources/axiosConfig';
|
||||||
import { InnovEnergyUser } from 'src/interfaces/UserTypes';
|
import { InnovEnergyUser } from 'src/interfaces/UserTypes';
|
||||||
import { TokenContext } from 'src/contexts/tokenContext';
|
import { TokenContext } from 'src/contexts/tokenContext';
|
||||||
|
import { UserContext } from 'src/contexts/userContext';
|
||||||
import { TabsContainerWrapper } from 'src/layouts/TabsContainerWrapper';
|
import { TabsContainerWrapper } from 'src/layouts/TabsContainerWrapper';
|
||||||
import { FormattedMessage, useIntl } from 'react-intl';
|
import { FormattedMessage, useIntl } from 'react-intl';
|
||||||
import UserAccess from '../ManageAccess/UserAccess';
|
import UserAccess from '../ManageAccess/UserAccess';
|
||||||
|
|
@ -43,6 +45,8 @@ function User(props: singleUserProps) {
|
||||||
const [formValues, setFormValues] = useState(props.current_user);
|
const [formValues, setFormValues] = useState(props.current_user);
|
||||||
const tokencontext = useContext(TokenContext);
|
const tokencontext = useContext(TokenContext);
|
||||||
const { removeToken } = tokencontext;
|
const { removeToken } = tokencontext;
|
||||||
|
const userContext = useContext(UserContext);
|
||||||
|
const loggedInUser = userContext?.currentUser;
|
||||||
const tabs = [
|
const tabs = [
|
||||||
{ value: 'user', label: intl.formatMessage({ id: 'user' }) },
|
{ value: 'user', label: intl.formatMessage({ id: 'user' }) },
|
||||||
{ value: 'manage', label: intl.formatMessage({ id: 'accessManagement' }) }
|
{ value: 'manage', label: intl.formatMessage({ id: 'accessManagement' }) }
|
||||||
|
|
@ -161,6 +165,17 @@ function User(props: singleUserProps) {
|
||||||
|
|
||||||
const isMobile = window.innerWidth <= 1490;
|
const isMobile = window.innerWidth <= 1490;
|
||||||
|
|
||||||
|
// Mirror the backend delete guards: the main/root admin (the parentless top
|
||||||
|
// user — parentId 0; its id is NOT necessarily 0) and your own account can
|
||||||
|
// never be deleted, so disable the button and explain why.
|
||||||
|
const isMainAdmin = formValues.parentId <= 0 || formValues.id === 0;
|
||||||
|
const deleteDisabledReason = isMainAdmin
|
||||||
|
? intl.formatMessage({ id: 'cannotDeleteMainAdmin' })
|
||||||
|
: loggedInUser?.id === formValues.id
|
||||||
|
? intl.formatMessage({ id: 'cannotDeleteSelf' })
|
||||||
|
: '';
|
||||||
|
const cannotDelete = deleteDisabledReason !== '';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{openModalDeleteUser && (
|
{openModalDeleteUser && (
|
||||||
|
|
@ -350,18 +365,20 @@ function User(props: singleUserProps) {
|
||||||
defaultMessage="Apply Changes"
|
defaultMessage="Apply Changes"
|
||||||
/>
|
/>
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Tooltip title={deleteDisabledReason}>
|
||||||
variant="contained"
|
<span style={{ marginLeft: '10px' }}>
|
||||||
onClick={handleDelete}
|
<Button
|
||||||
sx={{
|
variant="contained"
|
||||||
marginLeft: '10px'
|
onClick={handleDelete}
|
||||||
}}
|
disabled={cannotDelete}
|
||||||
>
|
>
|
||||||
<FormattedMessage
|
<FormattedMessage
|
||||||
id="delete_user"
|
id="delete_user"
|
||||||
defaultMessage="Delete User"
|
defaultMessage="Delete User"
|
||||||
/>
|
/>
|
||||||
</Button>
|
</Button>
|
||||||
|
</span>
|
||||||
|
</Tooltip>
|
||||||
|
|
||||||
{loading && (
|
{loading && (
|
||||||
<CircularProgress
|
<CircularProgress
|
||||||
|
|
|
||||||
|
|
@ -502,6 +502,8 @@
|
||||||
"connectingToDevice": "Verbindung zum Gerät wird hergestellt...",
|
"connectingToDevice": "Verbindung zum Gerät wird hergestellt...",
|
||||||
"fetchingData": "Daten werden abgerufen...",
|
"fetchingData": "Daten werden abgerufen...",
|
||||||
"confirmDeleteUser": "Möchten Sie diesen Benutzer löschen?",
|
"confirmDeleteUser": "Möchten Sie diesen Benutzer löschen?",
|
||||||
|
"cannotDeleteSelf": "Sie können Ihr eigenes Konto nicht löschen",
|
||||||
|
"cannotDeleteMainAdmin": "Das Hauptadministrator-Konto kann nicht gelöscht werden",
|
||||||
"accessManagement": "Zugriffsverwaltung",
|
"accessManagement": "Zugriffsverwaltung",
|
||||||
"power": "Leistung",
|
"power": "Leistung",
|
||||||
"voltage": "Spannung",
|
"voltage": "Spannung",
|
||||||
|
|
|
||||||
|
|
@ -250,6 +250,8 @@
|
||||||
"connectingToDevice": "Connecting to the device...",
|
"connectingToDevice": "Connecting to the device...",
|
||||||
"fetchingData": "Fetching data...",
|
"fetchingData": "Fetching data...",
|
||||||
"confirmDeleteUser": "Do you want to delete this user?",
|
"confirmDeleteUser": "Do you want to delete this user?",
|
||||||
|
"cannotDeleteSelf": "You cannot delete your own account",
|
||||||
|
"cannotDeleteMainAdmin": "The main admin account cannot be deleted",
|
||||||
"accessManagement": "Access Management",
|
"accessManagement": "Access Management",
|
||||||
"power": "Power",
|
"power": "Power",
|
||||||
"voltage": "Voltage",
|
"voltage": "Voltage",
|
||||||
|
|
|
||||||
|
|
@ -502,6 +502,8 @@
|
||||||
"connectingToDevice": "Connexion à l'appareil en cours...",
|
"connectingToDevice": "Connexion à l'appareil en cours...",
|
||||||
"fetchingData": "Récupération des données...",
|
"fetchingData": "Récupération des données...",
|
||||||
"confirmDeleteUser": "Voulez-vous supprimer cet utilisateur ?",
|
"confirmDeleteUser": "Voulez-vous supprimer cet utilisateur ?",
|
||||||
|
"cannotDeleteSelf": "Vous ne pouvez pas supprimer votre propre compte",
|
||||||
|
"cannotDeleteMainAdmin": "Le compte de l'administrateur principal ne peut pas être supprimé",
|
||||||
"accessManagement": "Gestion des accès",
|
"accessManagement": "Gestion des accès",
|
||||||
"power": "Puissance",
|
"power": "Puissance",
|
||||||
"voltage": "Tension",
|
"voltage": "Tension",
|
||||||
|
|
|
||||||
|
|
@ -502,6 +502,8 @@
|
||||||
"connectingToDevice": "Connessione al dispositivo in corso...",
|
"connectingToDevice": "Connessione al dispositivo in corso...",
|
||||||
"fetchingData": "Recupero dati in corso...",
|
"fetchingData": "Recupero dati in corso...",
|
||||||
"confirmDeleteUser": "Vuoi eliminare questo utente?",
|
"confirmDeleteUser": "Vuoi eliminare questo utente?",
|
||||||
|
"cannotDeleteSelf": "Non puoi eliminare il tuo account",
|
||||||
|
"cannotDeleteMainAdmin": "L'account dell'amministratore principale non può essere eliminato",
|
||||||
"accessManagement": "Gestione accessi",
|
"accessManagement": "Gestione accessi",
|
||||||
"power": "Potenza",
|
"power": "Potenza",
|
||||||
"voltage": "Tensione",
|
"voltage": "Tensione",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue