Skip to content

Commit

Permalink
fix access issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ryo committed May 28, 2024
1 parent 4da51f9 commit 5ba423f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 90 deletions.
43 changes: 27 additions & 16 deletions src/components/admin/NewAdminNavigation.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';

import { Container, List, ListItem, ListItemIcon, ListItemText } from '@mui/material';

import { UserContext } from 'src/contexts/userContext';
import AnalyserIcon from 'src/svg/analyser.svg';
import CreerIcon from 'src/svg/creer.svg';
import GererIcon from 'src/svg/gerer.svg';
import MediathequeIcon from 'src/svg/mediatheque.svg';
import PublierIcon from 'src/svg/publier.svg';
import { UserType } from 'types/user.type';

interface NavItemProps {
key?: number;
Expand All @@ -22,6 +25,7 @@ interface Tab {
path: string;
label: string;
Icon: React.ElementType;
rights: number[];
}

const containerStyle = {
Expand All @@ -47,33 +51,40 @@ const NavItem = ({ path, selected, onClick, Icon, primary }: NavItemProps) => (
);

export const NewAdminNavigation = () => {
const router = useRouter();
const { user } = React.useContext(UserContext);

const [selectedTab, setSelectedTab] = React.useState('Créer');

const tabs: Tab[] = [
{ path: '/admin/newportal/create', label: 'Créer', Icon: CreerIcon },
{ path: '/admin/newportal/publish', label: 'Publier', Icon: PublierIcon },
{ path: '/admin/newportal/manage', label: 'Gérer', Icon: GererIcon },
{ path: '/admin/newportal/analyze', label: 'Analyser', Icon: AnalyserIcon },
{ path: '/admin/newportal/medialibrary', label: 'Médiathèque', Icon: MediathequeIcon },
{ path: '/admin/newportal/create', label: 'Créer', Icon: CreerIcon, rights: [UserType.ADMIN] },
{ path: '/admin/newportal/publish', label: 'Publier', Icon: PublierIcon, rights: [UserType.ADMIN] },
{ path: '/admin/newportal/manage', label: 'Gérer', Icon: GererIcon, rights: [UserType.ADMIN] },
{ path: '/admin/newportal/analyze', label: 'Analyser', Icon: AnalyserIcon, rights: [UserType.ADMIN, UserType.OBSERVATOR, UserType.MEDIATOR] },
{ path: '/admin/newportal/medialibrary', label: 'Médiathèque', Icon: MediathequeIcon, rights: [UserType.ADMIN, UserType.MEDIATOR] },
];

const onTabClick = (label: string) => {
setSelectedTab(label);
};

if (!user) {
router.push('/');
}
return (
<Container className="container-admin-nav" sx={containerStyle}>
<List sx={{ padding: 0, margin: 0 }}>
{tabs?.map((tab, id) => (
<NavItem
key={id}
selected={selectedTab === tab.label}
path={tab.path}
onClick={() => onTabClick(tab.label)}
Icon={tab.Icon}
primary={tab.label}
/>
))}
{tabs
?.filter((tab) => tab.rights.includes(user?.type ?? 10))
.map((tab, id) => (
<NavItem
key={id}
selected={selectedTab === tab.label}
path={tab.path}
onClick={() => onTabClick(tab.label)}
Icon={tab.Icon}
primary={tab.label}
/>
))}
</List>
</Container>
);
Expand Down
80 changes: 41 additions & 39 deletions src/pages/admin/newportal/create/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import Link from 'next/link';
import { useRouter } from 'next/router';
import React from 'react';
import React, { useEffect } from 'react';

import { List, ListItem, ListItemIcon, ListItemText } from '@mui/material';

import { UserContext } from 'src/contexts/userContext';
import { VillageContext } from 'src/contexts/villageContext';
import { useActivity } from 'src/hooks/useActivity';
import DoubleChevronRightIcon from 'src/svg/mdi-light_chevron-double-right.svg';
import { ActivityType } from 'types/activity.type';
import { UserType } from 'types/user.type';

type Link = {
name: string;
Expand All @@ -21,8 +23,33 @@ interface NavItemProps {
action?: (() => void) | undefined;
}

const NavItem = ({ link, primary, action }: NavItemProps) => (
<Link href={link} passHref onClick={action}>
<ListItem
className="like-button grey"
button
component="a"
onClick={
action
? (e: React.MouseEvent) => {
e.preventDefault();
action();
}
: undefined
}
>
<ListItemText primary={primary} />
<ListItemIcon>
<DoubleChevronRightIcon />
</ListItemIcon>
</ListItem>
</Link>
);

const Creer = () => {
const router = useRouter();
const { user } = React.useContext(UserContext);

const { createNewActivity } = useActivity();
const { selectedPhase } = React.useContext(VillageContext);

Expand All @@ -39,57 +66,32 @@ const Creer = () => {
{ name: 'Paramétrer l’hymne', link: 'https://' },
{ name: 'Mixer l’hymne', link: 'https://' },
];
useEffect(() => {
if (user?.type === UserType.OBSERVATOR) {
router.push('/admin/newportal/analyze');
}
}, [router, user]);
const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN];

const NavItem = ({ link, primary, action }: NavItemProps) => (
<Link href={link} passHref onClick={action}>
<ListItem
className="like-button grey"
button
component="a"
onClick={
action
? (e: React.MouseEvent) => {
e.preventDefault();
action();
}
: undefined
}
>
<ListItemText primary={primary} />
<ListItemIcon>
<DoubleChevronRightIcon />
</ListItemIcon>
</ListItem>
</Link>
);

const renderTitle = () => {
return (
if (!hasAccess) {
return <h1>Vous n&apos;avez pas accès à cette page, vous devez être médiateur ou admin.</h1>;
}
return (
<>
<div>
<h1>Créer</h1>
<p>
C’est dans cet espace, que les administrateurs et administratrices du site vont pouvoir gérer les droits d’accès, la composition des
villages-mondes et accéder à la liste complète des utilisateurs.{' '}
</p>
</div>
);
};

const renderLinks = () => {
return (
links && (
{links && (
<List sx={{ padding: 0, margin: 0 }}>
{links?.map((item, id) => (
<NavItem key={id} link={item.link} primary={item.name} action={item.action} />
))}
</List>
)
);
};
return (
<>
{renderTitle()}
{renderLinks()}
)}
</>
);
};
Expand Down
42 changes: 24 additions & 18 deletions src/pages/admin/newportal/manage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ import { UserType } from 'types/user.type';
type Link = {
name: string;
link: string;
rights: number[];
};

const Gerer = () => {
const { user } = React.useContext(UserContext);
const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN];
const hasAccess = user !== null && user.type in [UserType.ADMIN, UserType.SUPER_ADMIN];

if (!hasAccess) {
return <h1>Vous n&apos;avez pas accès à cette page, vous devez être médiateur, modérateur ou super admin.</h1>;
return <h1>Vous n&apos;avez pas accès à cette page, vous devez être super admin.</h1>;
}

const links: Link[] = [
{ name: 'Les villages-mondes', link: '/admin/newportal/manage/villages' },
{ name: 'Les utilisateurs', link: '/admin/newportal/manage/users' },
{ name: 'Les consignes des activités', link: '/admin/newportal/manage/activities' },
{ name: 'Paramétrer 1Village', link: '/admin/newportal/manage/settings' },
{ name: "Les droits d'accès", link: '/admin/newportal/manage/access' },
{ name: 'Les villages-mondes', link: '/admin/newportal/manage/villages', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] },
{ name: 'Les utilisateurs', link: '/admin/newportal/manage/users', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] },
{
name: 'Les consignes des activités',
link: '/admin/newportal/manage/activities',
rights: [UserType.SUPER_ADMIN],
},
{ name: 'Paramétrer 1Village', link: '/admin/newportal/manage/settings', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] },
{ name: "Les droits d'accès", link: '/admin/newportal/manage/access', rights: [UserType.SUPER_ADMIN] },
];

return (
Expand All @@ -38,16 +42,18 @@ const Gerer = () => {
</p>
</div>
<List sx={{ padding: 0, margin: 0 }}>
{links?.map((item, id) => (
<Link href={item.link} passHref key={id}>
<ListItem className="like-button grey" button component="a">
<ListItemText primary={item.name} />
<ListItemIcon>
<DoubleChevronRightIcon />
</ListItemIcon>
</ListItem>
</Link>
))}
{links
?.filter((link) => link.rights.includes(user.type))
.map((item, id) => (
<Link href={item.link} passHref key={id}>
<ListItem className="like-button grey" button component="a">
<ListItemText primary={item.name} />
<ListItemIcon>
<DoubleChevronRightIcon />
</ListItemIcon>
</ListItem>
</Link>
))}
</List>
</>
);
Expand Down
35 changes: 19 additions & 16 deletions src/pages/admin/newportal/manage/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import { UserType } from 'types/user.type';
type Link = {
name: string;
link: string;
rights: number[];
};

const Gerer = () => {
const { user } = React.useContext(UserContext);
const hasAccess = user !== null && user.type in [UserType.MEDIATOR, UserType.ADMIN, UserType.SUPER_ADMIN];
const hasAccess = user !== null && user.type in [UserType.ADMIN, UserType.SUPER_ADMIN];

if (!hasAccess) {
return <h1>Vous n&apos;avez pas accès à cette page, vous devez être médiateur, modérateur ou super admin.</h1>;
return <h1>Vous n&apos;avez pas accès à cette page, vous devez être modérateur ou super admin.</h1>;
}

const links: Link[] = [
{ name: 'Archiver', link: '/admin/newportal/manage/settings/archive' },
{ name: 'Présenatation de Pélico', link: '/admin/newportal/manage/settings/pelico' },
{ name: 'Paramétrer la home', link: '/admin/newportal/manage/settings/home' },
{ name: 'Paramétrer les phases', link: '/admin/newportal/manage/settings/phases' },
{ name: 'Archiver', link: '/admin/newportal/manage/settings/archive', rights: [UserType.SUPER_ADMIN] },
{ name: 'Présenatation de Pélico', link: '/admin/newportal/manage/settings/pelico', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] },
{ name: 'Paramétrer la home', link: '/admin/newportal/manage/settings/home', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] },
{ name: 'Paramétrer les phases', link: '/admin/newportal/manage/settings/phases', rights: [UserType.ADMIN, UserType.SUPER_ADMIN] },
];

return (
Expand All @@ -42,16 +43,18 @@ const Gerer = () => {
</p>
</div>
<List sx={{ padding: 0, margin: 0 }}>
{links?.map((item, id) => (
<Link href={item.link} passHref key={id}>
<ListItem className="like-button grey" button component="a">
<ListItemText primary={item.name} />
<ListItemIcon>
<DoubleChevronRightIcon />
</ListItemIcon>
</ListItem>
</Link>
))}
{links
?.filter((link) => link.rights.includes(user.type))
.map((item, id) => (
<Link href={item.link} passHref key={id}>
<ListItem className="like-button grey" button component="a">
<ListItemText primary={item.name} />
<ListItemIcon>
<DoubleChevronRightIcon />
</ListItemIcon>
</ListItem>
</Link>
))}
</List>
</>
);
Expand Down
11 changes: 10 additions & 1 deletion src/pages/admin/newportal/publish/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { useRouter } from 'next/router';
import React from 'react';
import React, { useEffect } from 'react';

import type { GridColDef, GridRowsProp } from '@mui/x-data-grid';
import { DataGrid } from '@mui/x-data-grid';

import { useGetActivities } from 'src/api/activities/activities.get';
import ActivityCardAdminList from 'src/components/activities/ActivityCard/activity-admin/ActivityCardAdminList';
import { UserContext } from 'src/contexts/userContext';
import PelicoStar from 'src/svg/pelico/pelico_star.svg';
import PelicoVacances from 'src/svg/pelico/pelico_vacances.svg';
import { UserType } from 'types/user.type';

const rows: GridRowsProp = [
// A row example of how it should look
Expand All @@ -16,12 +18,19 @@ const rows: GridRowsProp = [
const columns: GridColDef[] = [];

const Publier = () => {
const { user } = React.useContext(UserContext);
const draftActivities = useGetActivities({ limit: 2, isDraft: true, isPelico: true });
const publishedActivities = useGetActivities({ limit: 2, isDraft: false, isPelico: true });
const router = useRouter();

useEffect(() => {
if (user?.type === UserType.OBSERVATOR) {
router.push('/admin/newportal/analyze');
}
}, [router, user]);
if (draftActivities.isError) return <p>Error!</p>;
if (draftActivities.isLoading || draftActivities.isIdle) return <p>Loading...</p>;

return (
<div
style={{
Expand Down

0 comments on commit 5ba423f

Please sign in to comment.