Skip to content

Commit

Permalink
feat: block standard roles
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmagnus committed Jan 30, 2024
1 parent f5a2f83 commit 53ded2f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import React, { Dispatch, SetStateAction } from 'react'

import { MAX_PAGE_WIDTH } from 'utils/constants/sizes'

import Authentication from 'app/auth/services/auth'
import { IChange } from 'app/core/pages/role-permissions'

interface IRolePermissionsTemplate {
Expand Down Expand Up @@ -70,6 +71,10 @@ export const RolePermissionsTemplate: React.FC<IRolePermissionsTemplate> = ({
setChanges([...filtered, newChange])
}

const isDisabled = (role: Hooks.UseAuthTypes.IRole): boolean => {
return role.admin === 1 || role.created_by != Authentication.getUser()?.id
}

return (
<Flex flexDir="column" w="full">
<Flex maxW={MAX_PAGE_WIDTH} alignSelf="center" flexDir="column" w="full">
Expand Down Expand Up @@ -137,7 +142,12 @@ export const RolePermissionsTemplate: React.FC<IRolePermissionsTemplate> = ({
permission.id,
role.id
)}
isDisabled={role.admin === 1}
isDisabled={isDisabled(role)}
title={
isDisabled(role)
? 'You can only edit roles you created'
: ''
}
onChange={(event): void => {
onChange(
permission.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import React from 'react'

import { DeleteIcon } from 'components/icons'

import Authentication from 'app/auth/services/auth'

import { ModalRoleDelete } from '../modal-role-delete'
import { ModalRoleManage } from '../modal-role-manage'

Expand Down Expand Up @@ -35,6 +37,10 @@ export const ItemRole: React.FC<IItemRole> = ({
onClose: onCloseDelete,
} = useDisclosure()

const isDisabled = (role: Hooks.UseAuthTypes.IRole): boolean => {
return role.admin === 1 || role.created_by != Authentication.getUser()?.id
}

return (
<>
<ModalRoleManage
Expand All @@ -61,15 +67,21 @@ export const ItemRole: React.FC<IItemRole> = ({
<Button
variant="secondary"
onClick={onOpenUpdate}
isDisabled={role.admin === 1}
title={
isDisabled(role) ? 'You can only edit roles you created' : ''
}
isDisabled={isDisabled(role)}
>
Rename
</Button>
<Button
variant="primary"
onClick={onOpenDelete}
bg="red.500"
isDisabled={role.admin === 1}
title={
isDisabled(role) ? 'You can only edit roles you created' : ''
}
isDisabled={isDisabled(role)}
>
<DeleteIcon />
</Button>
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/hooks/useAuth/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ export const AuthProvider: React.FC<IProps> = ({ children }) => {
const createRole = async (name: string): Promise<boolean> => {
setCreatingRole(true)
try {
const response = await http.post(`role`, { name: name })
const user = Authentication.getUser()
const response = await http.post(`role`, {
name: name,
created_by: user?.id ? Number(user.id) : null,
})
if (response.status !== 200) {
throw new Error()
}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/hooks/useAuth/use-auth-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare namespace Hooks {
id: number
name: string
admin: id
created_by: number
}

interface IUserRole {
Expand Down

0 comments on commit 53ded2f

Please sign in to comment.