diff --git a/src/Pages/Protected/Organ/ListOrgan/index.jsx b/src/Pages/Protected/Organ/ListOrgan/index.jsx index 29eece90..bfde3deb 100644 --- a/src/Pages/Protected/Organ/ListOrgan/index.jsx +++ b/src/Pages/Protected/Organ/ListOrgan/index.jsx @@ -17,7 +17,7 @@ export default function OrganList() { const [organs, setOrgans] = useState([]); const navigate = useNavigate(); - const canCreate = checkAction(permissions,"create"); + const canCreate = checkAction(permissions,"orgaos_criar"); useEffect(() => { const getOrgansInfo = async () => { const response = await listOrgans(); diff --git a/src/Pages/Protected/Organ/OrganUpdate/index.jsx b/src/Pages/Protected/Organ/OrganUpdate/index.jsx index 6b866b06..9c208ca1 100644 --- a/src/Pages/Protected/Organ/OrganUpdate/index.jsx +++ b/src/Pages/Protected/Organ/OrganUpdate/index.jsx @@ -23,8 +23,8 @@ export const OrganId = () => { const organsId = state?.organsId; const navigate = useNavigate(); const permissions = usePermissions(); - const canUpdate = checkAction(permissions, "update"); - const canDelete = checkAction(permissions, "delete"); + const canUpdate = checkAction(permissions, "orgaos_editar"); + const canDelete = checkAction(permissions, "orgaos_deletar"); const [openSave, setOpenSave] = useState(false); const [openDeleteOrgan, setOpenDeleteOrgan] = useState(false); diff --git a/src/Pages/Protected/Permissions/permissionsHandler.jsx b/src/Pages/Protected/Permissions/permissionsHandler.jsx index d2c7ce89..828494e2 100644 --- a/src/Pages/Protected/Permissions/permissionsHandler.jsx +++ b/src/Pages/Protected/Permissions/permissionsHandler.jsx @@ -4,6 +4,7 @@ import { Box, Button, TextField, Typography, Table, TableBody, TableCell, TableC import { Delete, Edit, Search } from '@mui/icons-material'; import { APIUsers } from "./../../../Services/BaseService/index"; import { getToken, getUser } from "./../../../Services/Functions/loader"; +import { checkAction } from '../../../Utils/permission'; const PermissionCRUD = () => { @@ -13,13 +14,23 @@ const PermissionCRUD = () => { const [search, setSearch] = useState(""); const [editId, setEditId] = useState(null); + const canCreate = checkAction(permissions, "permissoes_criar"); + const canUpdate = checkAction(permissions, "permissoes_editar"); + const canDelete = checkAction(permissions, "permissoes_deletar"); + const canView = checkAction(permissions, "permissoes_visualizar"); + useEffect(() => { fetchPermissions(); }, []); const fetchPermissions = async () => { try { - const response = await APIUsers.get('permission'); + const response = await APIUsers.get('permission', + { + headers: { + Authorization: `Bearer ${getToken()}`, + } + }); setPermissions(response.data); } catch (error) { console.error('Error fetching permissions:', error); @@ -30,7 +41,12 @@ const PermissionCRUD = () => { e.preventDefault(); try { if (editId) { - await APIUsers.patch(`permission/patch/${editId}`, form); + await APIUsers.patch(`permission/patch/${editId}`, form, + { + headers: { + Authorization: `Bearer ${getToken()}`, + } + }); } else { await APIUsers.post('permission/create/', form); } @@ -49,7 +65,12 @@ const PermissionCRUD = () => { const handleDelete = async (id) => { try { - await APIUsers.delete(`permission/delete/${id}`); + await APIUsers.delete(`permission/delete/${id}`, + { + headers: { + Authorization: `Bearer ${getToken()}`, + } + }); fetchPermissions(); } catch (error) { console.error('Error deleting permission:', error); @@ -58,11 +79,13 @@ const PermissionCRUD = () => { const handleSearch = async () => { try { - const response = await APIUsers.get(`permissions/search`, - { name: searchQuery }, - {headers: { - Authorization: `Bearer ${getToken()}`, - }} + const response = await APIUsers.get(`permissions/search`, + { name: searchQuery }, + { + headers: { + Authorization: `Bearer ${getToken()}`, + } + } ); setPermissions(response.data); } catch (error) { @@ -79,27 +102,30 @@ const PermissionCRUD = () => { Permission Management + {canCreate && ( - - setForm({ ...form, name: e.target.value })} - required - fullWidth - margin="normal" - /> - - - + + setForm({ ...form, name: e.target.value })} + required + fullWidth + margin="normal" + /> + + + )} - +
Name - Actions + {(canUpdate || canDelete) && ( + Actions + )} @@ -107,12 +133,16 @@ const PermissionCRUD = () => { {permission.name} - handleEdit(permission)}> - - - handleDelete(permission._id)}> - - + {canUpdate && ( + handleEdit(permission)}> + + + )} + {canDelete && ( + handleDelete(permission._id)}> + + + )} ))} diff --git a/src/Pages/Protected/Roles/RolesListPage/index.jsx b/src/Pages/Protected/Roles/RolesListPage/index.jsx index 3da43012..6d17c3db 100644 --- a/src/Pages/Protected/Roles/RolesListPage/index.jsx +++ b/src/Pages/Protected/Roles/RolesListPage/index.jsx @@ -24,11 +24,7 @@ export default function RolesListPage() { const storagedUserString = localStorage.getItem("@App:user"); const storagedUser = JSON.parse(storagedUserString); - const response = await APIUsers.get("role", { - headers: { - Authorization: `Bearer ${storagedUser.token}`, - }, - }); + const response = await APIUsers.get("role"); const data = response.data; if (Array.isArray(data)) { @@ -44,7 +40,7 @@ export default function RolesListPage() { fetchRoleForm(); }, []); - const hasPermission = checkAction(permissions, "create"); + const hasPermission = checkAction(permissions, "perfis_criar"); const handleSubmit = () => { navigate("/perfis/criar"); diff --git a/src/Pages/Protected/Roles/RolesUpdatePage/index.jsx b/src/Pages/Protected/Roles/RolesUpdatePage/index.jsx index 5b1afa5f..777f4e3a 100644 --- a/src/Pages/Protected/Roles/RolesUpdatePage/index.jsx +++ b/src/Pages/Protected/Roles/RolesUpdatePage/index.jsx @@ -154,7 +154,7 @@ export default function RolesUpdatePage() { placeholder="Digite o nome da permissão" /> */} - + {/* Lista de permissões */}

Lista de Permissões

diff --git a/src/Pages/Protected/Users/userUpdatePage/index.jsx b/src/Pages/Protected/Users/userUpdatePage/index.jsx index 69013558..26369dbf 100644 --- a/src/Pages/Protected/Users/userUpdatePage/index.jsx +++ b/src/Pages/Protected/Users/userUpdatePage/index.jsx @@ -38,8 +38,8 @@ export default function UserUpdatePage() { const [isEmailValid, setIsEmailValid] = useState(true); const [isCelularValid, setIsCelularValid] = useState(true); - const canDelete = checkAction(permissions, "delete"); - const canUpdate = checkAction(permissions, "update"); + const canDelete = checkAction(permissions, "usuarios_deletar"); + const canUpdate = checkAction(permissions, "usuarios_editar"); useEffect(() => { const loadRoles = async () => { diff --git a/src/Services/BaseService/index.js b/src/Services/BaseService/index.js index 514ecaff..4f2041e1 100644 --- a/src/Services/BaseService/index.js +++ b/src/Services/BaseService/index.js @@ -1,4 +1,6 @@ import axios from "axios"; +import { getToken } from "../Functions/loader"; + const baseBenefitsURL = import.meta.env.VITE_BENEFIT_DB_URL || "http://localhost:3003/"; @@ -9,14 +11,23 @@ const baseUserURL = const APIUsers = axios.create({ baseURL: baseUserURL, + headers: { + Authorization: `Bearer ${getToken()}`, + }, }); const APIBank = axios.create({ baseURL: baseBankURL, + headers: { + Authorization: `Bearer ${getToken()}`, + }, }); const APIBenefits = axios.create({ baseURL: baseBenefitsURL, + headers: { + Authorization: `Bearer ${getToken()}`, + }, }); export { APIUsers, APIBank, APIBenefits }; diff --git a/src/Services/Permissions/permissionsService.js b/src/Services/Permissions/permissionsService.js index 8b5aa85b..e2d6bcbf 100644 --- a/src/Services/Permissions/permissionsService.js +++ b/src/Services/Permissions/permissionsService.js @@ -27,11 +27,7 @@ export const getAllPermissions = async () => { if (!token) { throw new Error("No token found"); } - const response = await APIUsers.get("/permission", { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + const response = await APIUsers.get("/permission"); return response.data; } catch (error) { console.error("Erro ao buscar permissões:", error.response?.data || error); diff --git a/src/Services/RoleService/roleService.js b/src/Services/RoleService/roleService.js index 143d4e0b..7a246cba 100644 --- a/src/Services/RoleService/roleService.js +++ b/src/Services/RoleService/roleService.js @@ -27,9 +27,7 @@ export const createRole = async (roleData) => { moduleName: "users", action: "create", }, - headers: { - Authorization: `Bearer ${token}`, - }, + }); return response.data; } catch (error) { @@ -45,9 +43,7 @@ export const getAllRoles = async () => { throw new Error("No token found"); } const response = await APIUsers.get("/role", { - headers: { - Authorization: `Bearer ${token}`, - }, + }); return response.data; } catch (error) { @@ -62,11 +58,7 @@ export const getRoleById = async (id) => { if (!token) { throw new Error("No token found"); } - const response = await APIUsers.get(`/role/${id}`, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); + const response = await APIUsers.get(`/role/${id}`, ); return response.data; } catch (error) { console.error("Erro ao buscar role:", error); @@ -83,9 +75,7 @@ export const assignPermissionsToRole = async (roleId, permissions) => { const response = await APIUsers.put(`/roles/${roleId}/permissions`, { permissions, }, { - headers: { - Authorization: `Bearer ${token}`, - }, + }); return response.data; @@ -121,9 +111,7 @@ export const updateRole = async (id, roleData) => { moduleName: "users", action: "update", }, - headers: { - Authorization: `Bearer ${token}`, - }, + }); return response.data; } catch (error) { @@ -158,9 +146,7 @@ export const deleteRole = async (id) => { moduleName: "users", action: "delete", }, - headers: { - Authorization: `Bearer ${token}`, - }, + }); return response.data; } catch (error) {