From 53ded2fff78c0491080e4313420a3ae8feef1d98 Mon Sep 17 00:00:00 2001 From: Lucas Magnus Date: Tue, 30 Jan 2024 15:33:39 -0300 Subject: [PATCH] feat: block standard roles --- .../role-permissions-template/index.tsx | 12 +++++++++++- .../roles-manage-template/item-role/index.tsx | 16 ++++++++++++++-- frontend/src/hooks/useAuth/context.tsx | 6 +++++- frontend/src/hooks/useAuth/use-auth-types.d.ts | 1 + 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/templates/role-permissions-template/index.tsx b/frontend/src/components/templates/role-permissions-template/index.tsx index 664b775d..7c4ec6fa 100644 --- a/frontend/src/components/templates/role-permissions-template/index.tsx +++ b/frontend/src/components/templates/role-permissions-template/index.tsx @@ -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 { @@ -70,6 +71,10 @@ export const RolePermissionsTemplate: React.FC = ({ setChanges([...filtered, newChange]) } + const isDisabled = (role: Hooks.UseAuthTypes.IRole): boolean => { + return role.admin === 1 || role.created_by != Authentication.getUser()?.id + } + return ( @@ -137,7 +142,12 @@ export const RolePermissionsTemplate: React.FC = ({ 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, diff --git a/frontend/src/components/templates/roles-manage-template/item-role/index.tsx b/frontend/src/components/templates/roles-manage-template/item-role/index.tsx index d0ded8c7..a9ba37dd 100644 --- a/frontend/src/components/templates/roles-manage-template/item-role/index.tsx +++ b/frontend/src/components/templates/roles-manage-template/item-role/index.tsx @@ -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' @@ -35,6 +37,10 @@ export const ItemRole: React.FC = ({ onClose: onCloseDelete, } = useDisclosure() + const isDisabled = (role: Hooks.UseAuthTypes.IRole): boolean => { + return role.admin === 1 || role.created_by != Authentication.getUser()?.id + } + return ( <> = ({ @@ -69,7 +78,10 @@ export const ItemRole: React.FC = ({ variant="primary" onClick={onOpenDelete} bg="red.500" - isDisabled={role.admin === 1} + title={ + isDisabled(role) ? 'You can only edit roles you created' : '' + } + isDisabled={isDisabled(role)} > diff --git a/frontend/src/hooks/useAuth/context.tsx b/frontend/src/hooks/useAuth/context.tsx index 65244e7e..fbeb5176 100644 --- a/frontend/src/hooks/useAuth/context.tsx +++ b/frontend/src/hooks/useAuth/context.tsx @@ -307,7 +307,11 @@ export const AuthProvider: React.FC = ({ children }) => { const createRole = async (name: string): Promise => { 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() } diff --git a/frontend/src/hooks/useAuth/use-auth-types.d.ts b/frontend/src/hooks/useAuth/use-auth-types.d.ts index bee59dc2..0a0767a9 100644 --- a/frontend/src/hooks/useAuth/use-auth-types.d.ts +++ b/frontend/src/hooks/useAuth/use-auth-types.d.ts @@ -37,6 +37,7 @@ declare namespace Hooks { id: number name: string admin: id + created_by: number } interface IUserRole {