From 5502a2440238b3f09233baac2a434bf7f42ad884 Mon Sep 17 00:00:00 2001 From: Guilherme Peixoto Date: Fri, 17 Jan 2025 16:35:13 -0300 Subject: [PATCH] changed permission logic --- src/Context/auth.jsx | 5 +- .../Protected/Benefit/Benefits/index.jsx | 12 +-- .../Benefit/BenefitsCreate/index.jsx | 5 ++ .../Protected/Benefit/BenefitsList/index.jsx | 4 +- .../Benefit/BenefitsUpdate/index.jsx | 7 +- .../Protected/Benefit/BenefitsValue/index.jsx | 3 + src/Pages/Protected/Carteirinha/index.jsx | 3 + .../Finance/BankAccountCreate/index.jsx | 2 + .../Finance/BankAccountList/index.jsx | 88 ++++++++++--------- .../Finance/BankAccountUpdate/index.jsx | 8 +- .../Finance/FinanceHubPage/index.jsx | 42 +++++---- .../FinancialList/index.jsx | 2 +- .../FinancialUpdate/index.jsx | 4 +- .../Protected/Organ/OrganUpdate/index.jsx | 4 +- .../Permissions/permissionsHandler.jsx | 8 +- .../Protected/Roles/RolesListPage/index.jsx | 2 +- .../Protected/Supplier/ListSupplier/index.jsx | 2 +- .../Supplier/UpdateSupplier/index.jsx | 4 +- .../Protected/Users/userHubPage/index.jsx | 2 +- .../Protected/Users/userListPage/index.jsx | 2 +- .../Protected/Users/userUpdatePage/index.jsx | 4 +- src/Routes/protectedRoutes.jsx | 1 + src/Utils/permission.jsx | 12 +-- src/Utils/permissions.test.jsx | 14 +-- 24 files changed, 139 insertions(+), 101 deletions(-) diff --git a/src/Context/auth.jsx b/src/Context/auth.jsx index ea6d71d0..e8fbe31d 100644 --- a/src/Context/auth.jsx +++ b/src/Context/auth.jsx @@ -10,6 +10,7 @@ export const AuthProvider = ({ children }) => { useEffect(() => { const storagedUser = localStorage.getItem("@App:user"); const storagedToken = localStorage.getItem("@App:token"); + const storagedPermissions = localStorage.getItem("@App:permissions"); if (storagedToken && storagedUser) { setUser(JSON.parse(storagedUser)); @@ -22,6 +23,7 @@ export const AuthProvider = ({ children }) => { setUser(response.data); localStorage.setItem("@App:user", JSON.stringify(response.data.user)); localStorage.setItem("@App:token", JSON.stringify(response.data.token)); + localStorage.setItem("@App:permissions", JSON.stringify(response.data.permissions)); setFlag(!flag); return false; } catch (err) { @@ -33,7 +35,8 @@ export const AuthProvider = ({ children }) => { setUser(null); localStorage.removeItem("@App:user"); - localStorage.removeItem("App:token"); + localStorage.removeItem("@App:token"); + localStorage.removeItem("@App:permissions"); setFlag(!flag); }; diff --git a/src/Pages/Protected/Benefit/Benefits/index.jsx b/src/Pages/Protected/Benefit/Benefits/index.jsx index 13f13ef7..ba17ffd5 100644 --- a/src/Pages/Protected/Benefit/Benefits/index.jsx +++ b/src/Pages/Protected/Benefit/Benefits/index.jsx @@ -10,7 +10,7 @@ const Benefits = () => { const navigate = useNavigate(); const { user } = useAuth(); const permissions = usePermissions(); - const canCreate = checkAction(permissions, "create"); + const canCreate = checkAction( "beneficios_criar"); const handleBenefitsList = () => { navigate("/beneficios/lista"); @@ -38,10 +38,12 @@ const Benefits = () => { onClick={handleBenefitsCreate} /> )} - + { checkAction( "beneficios_visualizar") && ( + + )} diff --git a/src/Pages/Protected/Benefit/BenefitsCreate/index.jsx b/src/Pages/Protected/Benefit/BenefitsCreate/index.jsx index 65f395ec..bd5982f1 100644 --- a/src/Pages/Protected/Benefit/BenefitsCreate/index.jsx +++ b/src/Pages/Protected/Benefit/BenefitsCreate/index.jsx @@ -17,6 +17,7 @@ import { isValidCelular, isValidSite, } from "../../../../Utils/validators"; +import { checkAction, usePermissions } from "../../../../Utils/permission"; export default function BenefitsCreate() { const navigate = useNavigate(); @@ -44,6 +45,7 @@ export default function BenefitsCreate() { const [isChecked, setIsChecked] = useState(false); const [showModal, setShowModal] = useState(false); const [openError, setOpenError] = useState(false); + const permissions = usePermissions(); const tipoPessoaList = ["Jurídica", "Física"]; const categoriaList = [ @@ -194,8 +196,10 @@ export default function BenefitsCreate() { console.error("Erro ao criar benefício:", error); } }; + return ( + checkAction( "beneficios_criar") && (

Cadastro de benefícios

@@ -361,5 +365,6 @@ export default function BenefitsCreate() {
+ ) ); } diff --git a/src/Pages/Protected/Benefit/BenefitsList/index.jsx b/src/Pages/Protected/Benefit/BenefitsList/index.jsx index 878bc2ef..2f4bdab2 100644 --- a/src/Pages/Protected/Benefit/BenefitsList/index.jsx +++ b/src/Pages/Protected/Benefit/BenefitsList/index.jsx @@ -16,7 +16,7 @@ export default function BenefitsList() { const [benefits, setBenefits] = useState([]); const navigate = useNavigate(); const permissions = usePermissions(); - const canCreate = checkAction(permissions, "create"); + // const canCreate = checkAction( "beneficios_criar"); const handleSubmit = () => { navigate("/beneficios/criar"); @@ -46,7 +46,7 @@ export default function BenefitsList() {

Lista de benefícios

- {canCreate && ( + {checkAction( "beneficios_criar") && ( )}
diff --git a/src/Pages/Protected/Benefit/BenefitsUpdate/index.jsx b/src/Pages/Protected/Benefit/BenefitsUpdate/index.jsx index 3dd89524..686e2c02 100644 --- a/src/Pages/Protected/Benefit/BenefitsUpdate/index.jsx +++ b/src/Pages/Protected/Benefit/BenefitsUpdate/index.jsx @@ -55,8 +55,8 @@ export default function BenefitsUpdate() { const [showDeletedModal, setShowDeletedModal] = useState(false); const [openError, setOpenError] = useState(false); const permissions = usePermissions(); - const canUpdate = checkAction(permissions,"update"); - const canDelete = checkAction(permissions, "delete"); + const canUpdate = checkAction(permissions,"beneficios_editar"); + const canDelete = checkAction( "beneficios_deletar"); const tipoPessoaList = ["Jurídica", "Física"]; const categoriaList = [ @@ -248,7 +248,9 @@ export default function BenefitsUpdate() { }; return ( + checkAction( "beneficios_visualizar") && (
+

Visualização de benefícios

@@ -454,5 +456,6 @@ export default function BenefitsUpdate() {
+ ) ); } diff --git a/src/Pages/Protected/Benefit/BenefitsValue/index.jsx b/src/Pages/Protected/Benefit/BenefitsValue/index.jsx index 6351cbe4..de0f6d00 100644 --- a/src/Pages/Protected/Benefit/BenefitsValue/index.jsx +++ b/src/Pages/Protected/Benefit/BenefitsValue/index.jsx @@ -28,7 +28,9 @@ export default function BenefitsValue() { }, []); return ( + checkAction( "beneficios_visualizar") && (
+

Valores dos benefícios

@@ -93,5 +95,6 @@ export default function BenefitsValue() {
+ ) ); } diff --git a/src/Pages/Protected/Carteirinha/index.jsx b/src/Pages/Protected/Carteirinha/index.jsx index 24d846b2..b3867746 100644 --- a/src/Pages/Protected/Carteirinha/index.jsx +++ b/src/Pages/Protected/Carteirinha/index.jsx @@ -74,7 +74,9 @@ const Carteirinha = () => { const { name, birthDate, cpf, expeditionDate, hiringDate } = membershipData; return ( + checkAction( "beneficios_criar") && (
+

SINDPOL-DF

@@ -182,6 +184,7 @@ const Carteirinha = () => { BAIXAR CARTEIRINHA
+ ) ); }; diff --git a/src/Pages/Protected/Finance/BankAccountCreate/index.jsx b/src/Pages/Protected/Finance/BankAccountCreate/index.jsx index 3fad83da..31455350 100644 --- a/src/Pages/Protected/Finance/BankAccountCreate/index.jsx +++ b/src/Pages/Protected/Finance/BankAccountCreate/index.jsx @@ -101,6 +101,7 @@ const BankAccount = () => { }; return ( + checkAction( "contas_bancarias_criar") && (

Cadastro de Conta Bancária

@@ -187,6 +188,7 @@ const BankAccount = () => {
+ ) ); }; diff --git a/src/Pages/Protected/Finance/BankAccountList/index.jsx b/src/Pages/Protected/Finance/BankAccountList/index.jsx index 16a4ba58..595b7aa8 100644 --- a/src/Pages/Protected/Finance/BankAccountList/index.jsx +++ b/src/Pages/Protected/Finance/BankAccountList/index.jsx @@ -18,7 +18,7 @@ export default function ListBankAccount() { const { user } = useAuth(); const [bankAccounts, setBankAccounts] = useState([]); const permissions = usePermissions(); - const canCreate = checkAction(permissions, "create"); + const canCreate = checkAction( "create"); useEffect(() => { const fetchBankAccounts = async () => { @@ -46,52 +46,54 @@ export default function ListBankAccount() { ); return ( - user && ( -
-
-
-

Lista de Contas Bancárias

- {canCreate && ( - +
+
+

Lista de Contas Bancárias

+ {canCreate && ( + + )} +
+
+ setBusca(e.target.value)} /> - )} -
-
- setBusca(e.target.value)} - /> - - {filteredBankAccounts.map((bankAccount, index) => ( -
- - + + {filteredBankAccounts.map((bankAccount, index) => ( +
+ + (e.currentTarget.style.backgroundColor = "rgba(0, 0, 0, 0.1)") - } - onMouseLeave={(e) => - (e.currentTarget.style.backgroundColor = "transparent") - } - onClick={() => handleItemClick(bankAccount)} - > - - - - {index < filteredBankAccounts.length - 1 && } -
- ))} -
+ } + onMouseLeave={(e) => + (e.currentTarget.style.backgroundColor = "transparent") + } + onClick={() => handleItemClick(bankAccount)} + > + +
+
+ {index < filteredBankAccounts.length - 1 && } +
+ ))} +
+
-
-
+ + ) ) ); } diff --git a/src/Pages/Protected/Finance/BankAccountUpdate/index.jsx b/src/Pages/Protected/Finance/BankAccountUpdate/index.jsx index c54d7770..e4f0b209 100644 --- a/src/Pages/Protected/Finance/BankAccountUpdate/index.jsx +++ b/src/Pages/Protected/Finance/BankAccountUpdate/index.jsx @@ -27,8 +27,8 @@ const BankAccountId = () => { const [agency, setAgency] = useState(""); const [openError, setOpenError] = useState(false); const permissions = usePermissions(); - const canUpdate = checkAction(permissions, "update"); - const canDelete = checkAction(permissions, "delete"); + const canUpdate = checkAction("update"); + const canDelete = checkAction("delete"); const { user } = useAuth(); const { id } = useParams(); // Pega o ID da URL @@ -131,6 +131,8 @@ const BankAccountId = () => { }; return user ? ( + checkAction( "contas_bancarias_visualizar") && ( +

Visualização de Conta Bancária

@@ -256,7 +258,9 @@ const BankAccountId = () => { />
+ ) ) : null; + }; export default BankAccountId; diff --git a/src/Pages/Protected/Finance/FinanceHubPage/index.jsx b/src/Pages/Protected/Finance/FinanceHubPage/index.jsx index af3e1b3d..9959cb41 100644 --- a/src/Pages/Protected/Finance/FinanceHubPage/index.jsx +++ b/src/Pages/Protected/Finance/FinanceHubPage/index.jsx @@ -4,6 +4,7 @@ import SecondaryButton from "../../../../Components/SecondaryButton"; import sindpolLogo from "../../../../assets/sindpol-logo.png"; import sentinelaLogo from "../../../../assets/sentinela-logo.png"; import "./index.css"; +import { checkAction } from "../../../../Utils/permission"; export default function Finance() { const { user } = useAuth(); @@ -36,22 +37,31 @@ export default function Finance() { src={sentinelaLogo} alt="Sentinela Logo" /> - - - - + + {checkAction( "fornecedores_visualizar") && ( + + )} + {checkAction( "contas_bancarias_visualizar") && ( + + )} + {checkAction( "movimentacao_financeira_visualizar") && ( + + )} + {checkAction( "movimentacao_financeira_visualizar") && ( + + )}
diff --git a/src/Pages/Protected/FinancialMovements/FinancialList/index.jsx b/src/Pages/Protected/FinancialMovements/FinancialList/index.jsx index 165c0031..b7837b59 100644 --- a/src/Pages/Protected/FinancialMovements/FinancialList/index.jsx +++ b/src/Pages/Protected/FinancialMovements/FinancialList/index.jsx @@ -19,7 +19,7 @@ export default function FinancialList() { const [dataInicio, setDataInicio] = useState(null); const [dataFinal, setDataFinal] = useState(null); const permissions = usePermissions(); - const canCreate = checkAction(permissions, "create"); + const canCreate = checkAction( "create"); const storagedUser = JSON.parse(localStorage.getItem("@App:user")); diff --git a/src/Pages/Protected/FinancialMovements/FinancialUpdate/index.jsx b/src/Pages/Protected/FinancialMovements/FinancialUpdate/index.jsx index 1fac9ebd..81e3a182 100644 --- a/src/Pages/Protected/FinancialMovements/FinancialUpdate/index.jsx +++ b/src/Pages/Protected/FinancialMovements/FinancialUpdate/index.jsx @@ -40,8 +40,8 @@ export default function FinancialUpdate() { const [nomesDestino, setNomesDestino] = useState([]); const maxDescricaoLength = 130; const permissions = usePermissions(); - const canUpdate = checkAction(permissions, "update"); - const canDelete = checkAction(permissions, "delete"); + const canUpdate = checkAction( "update"); + const canDelete = checkAction( "delete"); const navigate = useNavigate(); const location = useLocation(); diff --git a/src/Pages/Protected/Organ/OrganUpdate/index.jsx b/src/Pages/Protected/Organ/OrganUpdate/index.jsx index 9c208ca1..caffc11b 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, "orgaos_editar"); - const canDelete = checkAction(permissions, "orgaos_deletar"); + const canUpdate = checkAction( "orgaos_editar"); + const canDelete = checkAction( "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 828494e2..ce2f0dcb 100644 --- a/src/Pages/Protected/Permissions/permissionsHandler.jsx +++ b/src/Pages/Protected/Permissions/permissionsHandler.jsx @@ -14,10 +14,10 @@ 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"); + const canCreate = checkAction( "permissoes_criar"); + const canUpdate = checkAction( "permissoes_editar"); + const canDelete = checkAction( "permissoes_deletar"); + const canView = checkAction( "permissoes_visualizar"); useEffect(() => { fetchPermissions(); diff --git a/src/Pages/Protected/Roles/RolesListPage/index.jsx b/src/Pages/Protected/Roles/RolesListPage/index.jsx index 6d17c3db..995f9a17 100644 --- a/src/Pages/Protected/Roles/RolesListPage/index.jsx +++ b/src/Pages/Protected/Roles/RolesListPage/index.jsx @@ -40,7 +40,7 @@ export default function RolesListPage() { fetchRoleForm(); }, []); - const hasPermission = checkAction(permissions, "perfis_criar"); + const hasPermission = checkAction( "perfis_criar"); const handleSubmit = () => { navigate("/perfis/criar"); diff --git a/src/Pages/Protected/Supplier/ListSupplier/index.jsx b/src/Pages/Protected/Supplier/ListSupplier/index.jsx index 05f080f1..5d53aefe 100644 --- a/src/Pages/Protected/Supplier/ListSupplier/index.jsx +++ b/src/Pages/Protected/Supplier/ListSupplier/index.jsx @@ -16,7 +16,7 @@ export default function ListSupplier() { const [search, setSearch] = useState(""); const navigate = useNavigate(); const permissions = usePermissions(); - const canCreate = checkAction(permissions, "create"); + const canCreate = checkAction( "create"); useEffect(() => { const fetchSupplierForm = async () => { const response = await getSupplierForm(); diff --git a/src/Pages/Protected/Supplier/UpdateSupplier/index.jsx b/src/Pages/Protected/Supplier/UpdateSupplier/index.jsx index f84d858c..cfa50e67 100644 --- a/src/Pages/Protected/Supplier/UpdateSupplier/index.jsx +++ b/src/Pages/Protected/Supplier/UpdateSupplier/index.jsx @@ -17,8 +17,8 @@ import { checkAction, usePermissions } from "../../../../Utils/permission"; export default function UpdateSupplier() { const permissions = usePermissions(); - const canUpdate = checkAction(permissions, "update"); - const canDelete = checkAction(permissions, "delete"); + const canUpdate = checkAction( "update"); + const canDelete = checkAction( "delete"); const [nome, setNome] = useState(""); const [tipoPessoa, setTipoPessoa] = useState(""); const [cpfCnpj, setCpfCnpj] = useState(""); diff --git a/src/Pages/Protected/Users/userHubPage/index.jsx b/src/Pages/Protected/Users/userHubPage/index.jsx index 33445eb3..dce18ddc 100644 --- a/src/Pages/Protected/Users/userHubPage/index.jsx +++ b/src/Pages/Protected/Users/userHubPage/index.jsx @@ -10,7 +10,7 @@ import { usePermissions, checkAction } from "../../../../Utils/permission"; export default function UserHubPage() { const navigate = useNavigate(); const permissions = usePermissions(); - const canAprove = checkAction(permissions, "create"); + const canAprove = checkAction( "create"); const handleListaClick = () => { navigate("/usuarios"); diff --git a/src/Pages/Protected/Users/userListPage/index.jsx b/src/Pages/Protected/Users/userListPage/index.jsx index 7961b34e..c99a5cdd 100644 --- a/src/Pages/Protected/Users/userListPage/index.jsx +++ b/src/Pages/Protected/Users/userListPage/index.jsx @@ -47,7 +47,7 @@ export default function UserListPage() { return
Carregando...
; } - const hasPermission = checkAction(permissions, "create"); + const hasPermission = checkAction("usuarios_criar"); const handleRegisterClick = () => { navigate("/usuarios/criar"); diff --git a/src/Pages/Protected/Users/userUpdatePage/index.jsx b/src/Pages/Protected/Users/userUpdatePage/index.jsx index 26369dbf..637d9f1d 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, "usuarios_deletar"); - const canUpdate = checkAction(permissions, "usuarios_editar"); + const canDelete = checkAction( "usuarios_deletar"); + const canUpdate = checkAction( "usuarios_editar"); useEffect(() => { const loadRoles = async () => { diff --git a/src/Routes/protectedRoutes.jsx b/src/Routes/protectedRoutes.jsx index 4e0d2453..3c4ac923 100644 --- a/src/Routes/protectedRoutes.jsx +++ b/src/Routes/protectedRoutes.jsx @@ -190,6 +190,7 @@ const ProtectedRoutes = () => { /> } /> + { return modulePermissions ? modulePermissions.access.length > 0 : false; }; -export const checkAction = (permissions, action) => { - console.log(permissions, action) +export const checkAction = (action) => { + const permissionsString = localStorage.getItem("@App:permissions"); + const permissions = JSON.parse(permissionsString); const modulePermissions = permissions.find( - (permission) => permission.name === action + (permission) => permission === action ); - console.log('arribaa', modulePermissions ? true : false) - - return modulePermissions ? true : false ; + + return (modulePermissions) ? true : false ; }; export const usePermissions = () => { diff --git a/src/Utils/permissions.test.jsx b/src/Utils/permissions.test.jsx index 52cf5d47..cfc6e097 100644 --- a/src/Utils/permissions.test.jsx +++ b/src/Utils/permissions.test.jsx @@ -38,30 +38,30 @@ describe("checkModule", () => { describe("checkAction", () => { it('should return true for action "read" in module "users"', () => { - expect(checkAction(permissions, "read")).toBe(true); + expect(checkAction( "read")).toBe(true); }); it('should return true for action "write" in module "users"', () => { - expect(checkAction(permissions, "write")).toBe(true); + expect(checkAction( "write")).toBe(true); }); it('should return true for action "read" in module "settings"', () => { - expect(checkAction(permissions, "read")).toBe(true); + expect(checkAction( "read")).toBe(true); }); it('should return false for action "write" in module "settings"', () => { - expect(checkAction(permissions, "write")).toBe(false); + expect(checkAction( "write")).toBe(false); }); it('should return true for action "view" in module "dashboard"', () => { - expect(checkAction(permissions, "view")).toBe(true); + expect(checkAction( "view")).toBe(true); }); it('should return true for action "edit" in module "dashboard"', () => { - expect(checkAction(permissions, "edit")).toBe(true); + expect(checkAction( "edit")).toBe(true); }); it('should return false for action "delete" in module "dashboard"', () => { - expect(checkAction(permissions, "delete")).toBe(false); + expect(checkAction( "delete")).toBe(false); }); });