Skip to content

Commit

Permalink
fix: block user from removing own admin roles (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamv-ss authored Sep 10, 2024
1 parent 4e559e1 commit f58f609
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/assets/locales/de/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/assets/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
21 changes: 21 additions & 0 deletions src/components/overlays/EditPortalRoles/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
DialogActions,
DialogContent,
DialogHeader,
Typography,
} from '@catena-x/portal-shared-components'
import {
type AppRole,
Expand All @@ -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()
Expand All @@ -58,6 +60,7 @@ export default function EditPortalRoles({ id }: { id: string }) {
const [allRoles, setAllRoles] = useState<AppRole[]>([])
const [selectedRoles, setSelectedRoles] = useState<string[]>([])
const [offerId, setOfferId] = useState<string>('')
const [allAdminRoles, setAllAdminRoles] = useState<AppRole[]>([])

const [updatePortalRoles] = useUpdatePortalRolesMutation()

Expand All @@ -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])
Expand Down Expand Up @@ -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 (
<>
<div className="roles-heading">
Expand All @@ -132,6 +147,7 @@ export default function EditPortalRoles({ id }: { id: string }) {
allRoles.map((role) => (
<li key={role.roleId}>
<Checkbox
disabled={disabledCheckbox(role)}
label={role.role}
checked={selectedRoles.indexOf(role.role) !== -1}
onChange={(e) => {
Expand All @@ -142,6 +158,11 @@ export default function EditPortalRoles({ id }: { id: string }) {
))}
</ul>
</div>
{UserService.getUsername() === id && (
<Typography variant="body3" sx={{ mt: 3 }}>
{t('shared.userRoles.errorMsg')}
</Typography>
)}
</DialogContent>

<DialogActions>
Expand Down

0 comments on commit f58f609

Please sign in to comment.