From f58f60937e398ce96207223ad74712c41b2d4cf8 Mon Sep 17 00:00:00 2001 From: Shubham Vaidya Date: Tue, 10 Sep 2024 20:49:08 +0530 Subject: [PATCH] fix: block user from removing own admin roles (#987) https://github.com/eclipse-tractusx/portal-frontend/pull/987#issuecomment-2293159831 --- src/assets/locales/de/main.json | 3 ++- src/assets/locales/en/main.json | 3 ++- .../overlays/EditPortalRoles/index.tsx | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/assets/locales/de/main.json b/src/assets/locales/de/main.json index 82eb3a19f..7fd30f72e 100644 --- a/src/assets/locales/de/main.json +++ b/src/assets/locales/de/main.json @@ -2295,7 +2295,8 @@ }, "userRoles": { "title": "Assigned Catena-X Portal Roles", - "changeRoleBtn": "Change Portal Role" + "changeRoleBtn": "Change Portal Role", + "errorMsg": "Sie sind nicht berechtigt, Ihre eigenen Administrator-Rollen zu ändern. Bitte wenden Sie sich an einen anderen Administrator." } }, "global": { diff --git a/src/assets/locales/en/main.json b/src/assets/locales/en/main.json index 45a5c7ef2..3804d30ce 100644 --- a/src/assets/locales/en/main.json +++ b/src/assets/locales/en/main.json @@ -2268,7 +2268,8 @@ }, "userRoles": { "title": "Assigned Catena-X Portal Roles", - "changeRoleBtn": "Change Portal Role" + "changeRoleBtn": "Change Portal Role", + "errorMsg": "You are not authorized to change your own admin roles. Please contact another admin." } }, "global": { diff --git a/src/components/overlays/EditPortalRoles/index.tsx b/src/components/overlays/EditPortalRoles/index.tsx index 2f08f5a57..f1896ff40 100644 --- a/src/components/overlays/EditPortalRoles/index.tsx +++ b/src/components/overlays/EditPortalRoles/index.tsx @@ -24,6 +24,7 @@ import { DialogActions, DialogContent, DialogHeader, + Typography, } from '@catena-x/portal-shared-components' import { type AppRole, @@ -40,6 +41,7 @@ import { useTranslation } from 'react-i18next' import { useDispatch } from 'react-redux' import { OVERLAYS } from 'types/Constants' import './style.scss' +import UserService from 'services/UserService' export default function EditPortalRoles({ id }: { id: string }) { const { t } = useTranslation() @@ -58,6 +60,7 @@ export default function EditPortalRoles({ id }: { id: string }) { const [allRoles, setAllRoles] = useState([]) const [selectedRoles, setSelectedRoles] = useState([]) const [offerId, setOfferId] = useState('') + const [allAdminRoles, setAllAdminRoles] = useState([]) const [updatePortalRoles] = useUpdatePortalRolesMutation() @@ -68,6 +71,13 @@ export default function EditPortalRoles({ id }: { id: string }) { } }, [appRoles]) + useEffect(() => { + if (allRoles) { + const adminRoles = allRoles.filter((item) => item.role.includes('Admin')) + setAllAdminRoles(adminRoles) + } + }, [allRoles]) + useEffect(() => { setSelectedRoles(assignedRoles ?? []) }, [assignedRoles]) @@ -112,6 +122,11 @@ export default function EditPortalRoles({ id }: { id: string }) { assignedRoles.length === selectedRoles.length && assignedRoles.every((value) => selectedRoles.includes(value))) + const disabledCheckbox = (currentRole: AppRole) => + UserService.getUsername() === id + ? allAdminRoles.includes(currentRole) + : false + return ( <>
@@ -132,6 +147,7 @@ export default function EditPortalRoles({ id }: { id: string }) { allRoles.map((role) => (
  • { @@ -142,6 +158,11 @@ export default function EditPortalRoles({ id }: { id: string }) { ))}
  • + {UserService.getUsername() === id && ( + + {t('shared.userRoles.errorMsg')} + + )}