Skip to content

Commit

Permalink
fix: add any course allowed role to Session context
Browse files Browse the repository at this point in the history
  • Loading branch information
valerydluski authored Jan 31, 2024
1 parent bd4b1c7 commit 6259c7b
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
9 changes: 7 additions & 2 deletions client/src/modules/Course/contexts/SessionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Router from 'next/router';
import React, { useEffect } from 'react';
import { useAsync } from 'react-use';
import { useActiveCourseContext } from './ActiveCourseContext';
import { hasRoleInAnyCourse } from 'domain/user';

export const SessionContext = React.createContext<Session>({} as Session);

Expand All @@ -17,10 +18,11 @@ type Props = React.PropsWithChildren<{
allowedRoles?: CourseRole[];
course?: ProfileCourseDto;
adminOnly?: boolean;
anyCoursePowerUser?: boolean;
}>;

export function SessionProvider(props: Props) {
const { allowedRoles } = props;
const { allowedRoles, anyCoursePowerUser } = props;
const activeCourse = useActiveCourseContext().course;
const course = props.course ?? activeCourse;

Expand Down Expand Up @@ -66,7 +68,10 @@ export function SessionProvider(props: Props) {

if (!isAdmin) {
const roles = courses?.[id]?.roles ?? [];
if (!allowedRoles.some(role => roles.includes(role))) {
const hasRoleInCurrentCourse = allowedRoles.some(role => roles.includes(role));
const isAnyCoursePowerUser = anyCoursePowerUser && allowedRoles.some(role => hasRoleInAnyCourse(session, role));

if (!hasRoleInCurrentCourse && !isAnyCoursePowerUser) {
return (
<Result
status="warning"
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/courses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ function getInitialValues(modalData: Partial<Course>) {
export default function () {
return (
<ActiveCourseProvider>
<SessionProvider allowedRoles={[CourseRole.Manager]}>
<SessionProvider allowedRoles={[CourseRole.Manager]} anyCoursePowerUser>
<Page />
</SessionProvider>
</ActiveCourseProvider>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function getInitialValues(modalData: Partial<EventDto>) {
export default function () {
return (
<ActiveCourseProvider>
<SessionProvider allowedRoles={[CourseRole.Manager]}>
<SessionProvider allowedRoles={[CourseRole.Manager]} anyCoursePowerUser>
<Page />
</SessionProvider>
</ActiveCourseProvider>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/mentor-registry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function filterData(data: MentorRegistryDto[], showAll: boolean) {
export default function () {
return (
<ActiveCourseProvider>
<SessionProvider allowedRoles={[CourseRole.Manager, CourseRole.Supervisor]}>
<SessionProvider allowedRoles={[CourseRole.Manager, CourseRole.Supervisor]} anyCoursePowerUser>
<Page />
</SessionProvider>
</ActiveCourseProvider>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/tasks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TasksPage } from 'modules/Tasks/pages';
export default function () {
return (
<ActiveCourseProvider>
<SessionProvider allowedRoles={[CourseRole.Manager]}>
<SessionProvider allowedRoles={[CourseRole.Manager]} anyCoursePowerUser>
<TasksPage />
</SessionProvider>
</ActiveCourseProvider>
Expand Down
2 changes: 1 addition & 1 deletion client/src/pages/admin/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function Page() {
export default function () {
return (
<ActiveCourseProvider>
<SessionProvider allowedRoles={[CourseRole.Manager]}>
<SessionProvider allowedRoles={[CourseRole.Manager]} anyCoursePowerUser>
<Page />
</SessionProvider>
</ActiveCourseProvider>
Expand Down

0 comments on commit 6259c7b

Please sign in to comment.