Skip to content

Commit

Permalink
feat(admin): ajout de vérifications de rôle pour les référents et mis…
Browse files Browse the repository at this point in the history
…e à jour des imports
  • Loading branch information
naustra committed Nov 14, 2024
1 parent 69341dc commit 2642662
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
5 changes: 3 additions & 2 deletions admin/src/scenes/volontaires/view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Switch } from "react-router-dom";
import { useSelector } from "react-redux";
import { useQuery } from "@tanstack/react-query";

import { YoungDto, ROLES } from "snu-lib";
import { YoungDto, isReferentReg, isAdmin } from "snu-lib";
import { AuthState } from "@/redux/auth/reducer";
import useDocumentTitle from "@/hooks/useDocumentTitle";
import { SentryRoute } from "@/sentry";
Expand All @@ -23,6 +23,7 @@ import FormEquivalence from "./FormEquivalence";
import VolontairePhase0View from "../../phase0/view";
import ProposeMission from "./proposalMission";
import CustomMission from "./customMission";
import { isAfter } from "date-fns";

export default function Index({ ...props }) {
const cohorts = useSelector((state: CohortState) => state.Cohorts);
Expand All @@ -43,7 +44,7 @@ export default function Index({ ...props }) {
? "correction"
: "readonly";
const cohort = cohorts.find(({ _id, name }) => _id === young?.cohortId || name === young?.cohort);
if (!isAdmin(user) && !isReferentRegion(user) && cohort.instructionEndDate && isAfter(new Date(), new Date(cohort.instructionEndDate)) {
if (!isAdmin(user) && !isReferentReg(user) && cohort?.instructionEndDate && isAfter(new Date(), new Date(cohort.instructionEndDate))) {
mode = "readonly";
}

Expand Down
42 changes: 26 additions & 16 deletions packages/lib/src/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ function isAdmin(user: UserRoles) {
return ROLES.ADMIN === user.role;
}

function isReferentReg(user: UserRoles) {
return ROLES.REFERENT_REGION === user.role;
}

function isReferentDep(user: UserRoles) {
return ROLES.REFERENT_DEPARTMENT === user.role;
}

function isReferentRegDep(user: UserRoles) {
return [ROLES.REFERENT_DEPARTMENT, ROLES.REFERENT_REGION].includes(user.role || "");
}
Expand Down Expand Up @@ -683,22 +691,22 @@ function canDownloadYoungDocuments(actor: UserDto, target?: UserDto, type?: stri
}

function canInviteYoung(actor: UserDto, cohort: CohortDto | null) {
if (!cohort) return false;

switch (actor.role) {
case ROLES.ADMIN:
return true;
case ROLES.REFERENT_DEPARTMENT:
return cohort.inscriptionOpenForReferentDepartment === true;
case ROLES.REFERENT_REGION:
return cohort.inscriptionOpenForReferentRegion === true;
case ROLES.REFERENT_CLASSE:
return cohort.inscriptionOpenForReferentClasse === true;
case ROLES.ADMINISTRATEUR_CLE:
return cohort.inscriptionOpenForAdministrateurCle === true;
default:
return false;
}
if (!cohort) return false;

switch (actor.role) {
case ROLES.ADMIN:
return true;
case ROLES.REFERENT_DEPARTMENT:
return cohort.inscriptionOpenForReferentDepartment === true;
case ROLES.REFERENT_REGION:
return cohort.inscriptionOpenForReferentRegion === true;
case ROLES.REFERENT_CLASSE:
return cohort.inscriptionOpenForReferentClasse === true;
case ROLES.ADMINISTRATEUR_CLE:
return cohort.inscriptionOpenForAdministrateurCle === true;
default:
return false;
}
}

function canSendTemplateToYoung(actor, young) {
Expand Down Expand Up @@ -1193,6 +1201,8 @@ export {
canCreateTags,
isSuperAdmin,
isAdmin,
isReferentReg,
isReferentDep,
isAdminCle,
isChefEtablissement,
isCoordinateurEtablissement,
Expand Down

0 comments on commit 2642662

Please sign in to comment.