Skip to content

Commit

Permalink
changed permission logic
Browse files Browse the repository at this point in the history
  • Loading branch information
guipeeix7 committed Jan 17, 2025
1 parent 3638496 commit 5502a24
Show file tree
Hide file tree
Showing 24 changed files with 139 additions and 101 deletions.
5 changes: 4 additions & 1 deletion src/Context/auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Check failure on line 13 in src/Context/auth.jsx

View workflow job for this annotation

GitHub Actions / lint

'storagedPermissions' is assigned a value but never used

if (storagedToken && storagedUser) {
setUser(JSON.parse(storagedUser));
Expand All @@ -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) {
Expand All @@ -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);
};

Expand Down
12 changes: 7 additions & 5 deletions src/Pages/Protected/Benefit/Benefits/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Benefits = () => {
const navigate = useNavigate();
const { user } = useAuth();
const permissions = usePermissions();

Check failure on line 12 in src/Pages/Protected/Benefit/Benefits/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'permissions' is assigned a value but never used
const canCreate = checkAction(permissions, "create");
const canCreate = checkAction( "beneficios_criar");

const handleBenefitsList = () => {
navigate("/beneficios/lista");
Expand Down Expand Up @@ -38,10 +38,12 @@ const Benefits = () => {
onClick={handleBenefitsCreate}
/>
)}
<SecondaryButton
text="LISTA DE BENEFÍCIOS"
onClick={handleBenefitsList}
/>
{ checkAction( "beneficios_visualizar") && (
<SecondaryButton
text="LISTA DE BENEFÍCIOS"
onClick={handleBenefitsList}
/>
)}
</div>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/Pages/Protected/Benefit/BenefitsCreate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isValidCelular,
isValidSite,
} from "../../../../Utils/validators";
import { checkAction, usePermissions } from "../../../../Utils/permission";

export default function BenefitsCreate() {
const navigate = useNavigate();
Expand Down Expand Up @@ -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();

Check failure on line 48 in src/Pages/Protected/Benefit/BenefitsCreate/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'permissions' is assigned a value but never used

const tipoPessoaList = ["Jurídica", "Física"];
const categoriaList = [
Expand Down Expand Up @@ -194,8 +196,10 @@ export default function BenefitsCreate() {
console.error("Erro ao criar benefício:", error);
}
};


return (
checkAction( "beneficios_criar") && (
<div className="container-benefits">
<div className="forms-container-benefits">
<h1>Cadastro de benefícios</h1>
Expand Down Expand Up @@ -361,5 +365,6 @@ export default function BenefitsCreate() {
</Modal>
</div>
</div>
)
);
}
4 changes: 2 additions & 2 deletions src/Pages/Protected/Benefit/BenefitsList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function BenefitsList() {
const [benefits, setBenefits] = useState([]);
const navigate = useNavigate();
const permissions = usePermissions();

Check failure on line 18 in src/Pages/Protected/Benefit/BenefitsList/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'permissions' is assigned a value but never used
const canCreate = checkAction(permissions, "create");
// const canCreate = checkAction( "beneficios_criar");

const handleSubmit = () => {
navigate("/beneficios/criar");
Expand Down Expand Up @@ -46,7 +46,7 @@ export default function BenefitsList() {
<div className="forms-container-list-benefits">
<div className="double-box-list-benefits">
<h1> Lista de benefícios</h1>
{canCreate && (
{checkAction( "beneficios_criar") && (
<PrimaryButton text="Cadastrar benefício" onClick={handleSubmit} />
)}
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/Pages/Protected/Benefit/BenefitsUpdate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -248,7 +248,9 @@ export default function BenefitsUpdate() {
};

return (
checkAction( "beneficios_visualizar") && (
<div className="container-benefits">

<div className="forms-container-benefits">
<h1>Visualização de benefícios</h1>

Expand Down Expand Up @@ -454,5 +456,6 @@ export default function BenefitsUpdate() {
</Modal>
</div>
</div>
)
);
}
3 changes: 3 additions & 0 deletions src/Pages/Protected/Benefit/BenefitsValue/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default function BenefitsValue() {
}, []);

return (
checkAction( "beneficios_visualizar") && (

Check failure on line 31 in src/Pages/Protected/Benefit/BenefitsValue/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'checkAction' is not defined
<section className="benefits-container">

<div className="benefits-list">
<div className="benefits-header">
<h1>Valores dos benefícios</h1>
Expand Down Expand Up @@ -93,5 +95,6 @@ export default function BenefitsValue() {
</div>
</div>
</section>
)
);
}
3 changes: 3 additions & 0 deletions src/Pages/Protected/Carteirinha/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ const Carteirinha = () => {
const { name, birthDate, cpf, expeditionDate, hiringDate } = membershipData;

return (

Check failure on line 76 in src/Pages/Protected/Carteirinha/index.jsx

View workflow job for this annotation

GitHub Actions / test

Unhandled error

ReferenceError: checkAction is not defined ❯ Carteirinha src/Pages/Protected/Carteirinha/index.jsx:76:3 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom.development.js:19617:20 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21640:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22 This error originated in "src/Pages/Protected/Carteirinha/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "should render membership data correctly". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 76 in src/Pages/Protected/Carteirinha/index.jsx

View workflow job for this annotation

GitHub Actions / test

Unhandled error

ReferenceError: checkAction is not defined ❯ Carteirinha src/Pages/Protected/Carteirinha/index.jsx:76:3 ❯ renderWithHooks node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ updateFunctionComponent node_modules/react-dom/cjs/react-dom.development.js:19617:20 ❯ beginWork node_modules/react-dom/cjs/react-dom.development.js:21640:16 ❯ beginWork$1 node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performConcurrentWorkOnRoot node_modules/react-dom/cjs/react-dom.development.js:25789:22 This error originated in "src/Pages/Protected/Carteirinha/index.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "should call downloadPDF when clicking on 'BAIXAR CARTEIRINHA' button". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
checkAction( "beneficios_criar") && (

Check failure on line 77 in src/Pages/Protected/Carteirinha/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'checkAction' is not defined
<div className="carteirinha-container" ref={cardRef}>

<div className="carteirinha">
<header className="carteirinha-header">
<h1>SINDPOL-DF</h1>
Expand Down Expand Up @@ -182,6 +184,7 @@ const Carteirinha = () => {
BAIXAR CARTEIRINHA
</button>
</div>
)
);
};

Expand Down
2 changes: 2 additions & 0 deletions src/Pages/Protected/Finance/BankAccountCreate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const BankAccount = () => {
};

return (
checkAction( "contas_bancarias_criar") && (

Check failure on line 104 in src/Pages/Protected/Finance/BankAccountCreate/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'checkAction' is not defined
<div className="container-benefits">
<div className="forms-container-benefits">
<h1>Cadastro de Conta Bancária</h1>
Expand Down Expand Up @@ -187,6 +188,7 @@ const BankAccount = () => {
</Modal>
</div>
</div>
)
);
};

Expand Down
88 changes: 45 additions & 43 deletions src/Pages/Protected/Finance/BankAccountList/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function ListBankAccount() {
const { user } = useAuth();
const [bankAccounts, setBankAccounts] = useState([]);
const permissions = usePermissions();

Check failure on line 20 in src/Pages/Protected/Finance/BankAccountList/index.jsx

View workflow job for this annotation

GitHub Actions / lint

'permissions' is assigned a value but never used
const canCreate = checkAction(permissions, "create");
const canCreate = checkAction( "create");

useEffect(() => {
const fetchBankAccounts = async () => {
Expand Down Expand Up @@ -46,52 +46,54 @@ export default function ListBankAccount() {
);

return (
user && (
<section className="container-list-bankAccount">
<div className="forms-container-list-bankAccount">
<div className="double-box-list-bankAccount">
<h1>Lista de Contas Bancárias</h1>
{canCreate && (
<PrimaryButtom
text="Cadastrar contas bancárias"
onClick={handleRegisterClick}
checkAction( "contas_bancarias_visualizar") && (
user && (
<section className="container-list-bankAccount">
<div className="forms-container-list-bankAccount">
<div className="double-box-list-bankAccount">
<h1>Lista de Contas Bancárias</h1>
{canCreate && (
<PrimaryButtom
text="Cadastrar contas bancárias"
onClick={handleRegisterClick}
/>
)}
</div>
<div className="search-box-bankAccount">
<FieldText
label="Pesquisar conta bancária"
value={busca}
onChange={(e) => setBusca(e.target.value)}
/>
)}
</div>
<div className="search-box-bankAccount">
<FieldText
label="Pesquisar conta bancária"
value={busca}
onChange={(e) => setBusca(e.target.value)}
/>
<List>
{filteredBankAccounts.map((bankAccount, index) => (
<div key={bankAccount._id}>
<ListItem>
<ListItemButton
className="list-item-bankAccount"
style={{
transition: "background-color 0.3s ease",
}}
onMouseEnter={(e) =>
<List>
{filteredBankAccounts.map((bankAccount, index) => (
<div key={bankAccount._id}>
<ListItem>
<ListItemButton
className="list-item-bankAccount"
style={{
transition: "background-color 0.3s ease",
}}
onMouseEnter={(e) =>
(e.currentTarget.style.backgroundColor =
"rgba(0, 0, 0, 0.1)")
}
onMouseLeave={(e) =>
(e.currentTarget.style.backgroundColor = "transparent")
}
onClick={() => handleItemClick(bankAccount)}
>
<ListItemText primary={bankAccount.name} />
</ListItemButton>
</ListItem>
{index < filteredBankAccounts.length - 1 && <Divider />}
</div>
))}
</List>
}
onMouseLeave={(e) =>
(e.currentTarget.style.backgroundColor = "transparent")
}
onClick={() => handleItemClick(bankAccount)}
>
<ListItemText primary={bankAccount.name} />
</ListItemButton>
</ListItem>
{index < filteredBankAccounts.length - 1 && <Divider />}
</div>
))}
</List>
</div>
</div>
</div>
</section>
</section>
)
)
);
}
8 changes: 6 additions & 2 deletions src/Pages/Protected/Finance/BankAccountUpdate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -131,6 +131,8 @@ const BankAccountId = () => {
};

return user ? (
checkAction( "contas_bancarias_visualizar") && (

<div className="container-benefits">
<div className="forms-container-benefits">
<h1>Visualização de Conta Bancária</h1>
Expand Down Expand Up @@ -256,7 +258,9 @@ const BankAccountId = () => {
/>
</Modal>
</div>
)
) : null;

};

export default BankAccountId;
42 changes: 26 additions & 16 deletions src/Pages/Protected/Finance/FinanceHubPage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -36,22 +37,31 @@ export default function Finance() {
src={sentinelaLogo}
alt="Sentinela Logo"
/>
<SecondaryButton
text="Lista de Fornecedores"
onClick={handleFornecedoresListaClick}
/>
<SecondaryButton
text="Lista de Contas Bancárias"
onClick={handleListaClick}
/>
<SecondaryButton
text="Lista de Movimentações Financeiras"
onClick={handleMovimentacoesClick}
/>
<SecondaryButton
text="Gerar relatório"
onClick={handleGeradorRelatorio}
/>

{checkAction( "fornecedores_visualizar") && (
<SecondaryButton
text="Lista de Fornecedores"
onClick={handleFornecedoresListaClick}
/>
)}
{checkAction( "contas_bancarias_visualizar") && (
<SecondaryButton
text="Lista de Contas Bancárias"
onClick={handleListaClick}
/>
)}
{checkAction( "movimentacao_financeira_visualizar") && (
<SecondaryButton
text="Lista de Movimentações Financeiras"
onClick={handleMovimentacoesClick}
/>
)}
{checkAction( "movimentacao_financeira_visualizar") && (
<SecondaryButton
text="Gerar relatório"
onClick={handleGeradorRelatorio}
/>
)}
</div>
</div>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 5502a24

Please sign in to comment.