Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development: Restrict Course Overview Access #9834

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

This file was deleted.

89 changes: 89 additions & 0 deletions src/main/webapp/app/overview/course-overview-guard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Injectable, inject } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router } from '@angular/router';
import { Observable, of, switchMap } from 'rxjs';
import { CourseStorageService } from 'app/course/manage/course-storage.service';
import { CourseManagementService } from 'app/course/manage/course-management.service';
import { Course } from 'app/entities/course.model';
import dayjs from 'dayjs/esm';
import { ArtemisServerDateService } from 'app/shared/server-date.service';

@Injectable({
providedIn: 'root',
})
export class CourseOverviewGuardService implements CanActivate {
private courseStorageService = inject(CourseStorageService);
private courseManagementService = inject(CourseManagementService);
private router = inject(Router);
private serverDateService = inject(ArtemisServerDateService);

/**
* Check if the client can activate a course overview route.
* @return true if the client is allowed to access the route, false otherwise
*/
canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
const courseIdString = route.parent?.paramMap.get('courseId');
if (!courseIdString) {
return of(false);
}
const courseIdNumber = parseInt(courseIdString, 10);

cremertim marked this conversation as resolved.
Show resolved Hide resolved
const path = route.routeConfig?.path;

const course = this.courseStorageService.getCourse(courseIdNumber);
if (course) {
return this.handleReturn(course, path);
}

return this.courseManagementService.find(courseIdNumber).pipe(
switchMap((res) => {
if (res.body) {
// Store course in cache
this.courseStorageService.updateCourse(res.body);
}
// Flatten the result to return Observable<boolean> directly
return this.handleReturn(this.courseStorageService.getCourse(courseIdNumber), path);
}),
);
}
cremertim marked this conversation as resolved.
Show resolved Hide resolved

handleReturn = (course?: Course, type?: string): Observable<boolean> => {
let hasAccess: boolean;
switch (type) {
case 'exams':
hasAccess = this.hasVisibleExams(course);
break;
case 'competencies':
hasAccess = !!(course?.numberOfCompetencies || course?.numberOfPrerequisites);
break;
case 'tutorial-groups':
hasAccess = !!course?.numberOfTutorialGroups;
break;
case 'dashboard':
hasAccess = course?.studentCourseAnalyticsDashboardEnabled ?? false;
break;
case 'faq':
hasAccess = course?.faqEnabled ?? false;
break;
case 'learning-path':
hasAccess = course?.learningPathsEnabled ?? false;
break;
default:
hasAccess = false;
}
if (!hasAccess) {
this.router.navigate([`/courses/${course?.id}/exercises`]);
cremertim marked this conversation as resolved.
Show resolved Hide resolved
}
return of(hasAccess);
};

hasVisibleExams(course?: Course): boolean {
if (course?.exams) {
for (const exam of course.exams) {
if (exam.visibleDate && dayjs(exam.visibleDate).isBefore(this.serverDateService.now())) {
return true;
}
}
}
return false;
}
}
11 changes: 7 additions & 4 deletions src/main/webapp/app/overview/courses-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { CourseTutorialGroupsComponent } from './course-tutorial-groups/course-t
import { CourseTutorialGroupDetailComponent } from './tutorial-group-details/course-tutorial-group-detail/course-tutorial-group-detail.component';
import { ExamParticipationComponent } from 'app/exam/participate/exam-participation.component';
import { PendingChangesGuard } from 'app/shared/guard/pending-changes.guard';
import { CourseDashboardGuard } from 'app/overview/course-dashboard/course-dashboard-guard.service';
import { CourseArchiveComponent } from './course-archive/course-archive.component';
import { CourseOverviewGuardService } from 'app/overview/course-overview-guard.service';

const routes: Routes = [
{
Expand Down Expand Up @@ -167,6 +167,7 @@ const routes: Routes = [
pageTitle: 'overview.competencies',
showRefreshButton: true,
},
canActivate: [CourseOverviewGuardService],
children: [
{
path: '',
Expand All @@ -185,7 +186,7 @@ const routes: Routes = [
authorities: [Authority.USER],
pageTitle: 'overview.dashboard',
},
canActivate: [UserRouteAccessService, CourseDashboardGuard],
canActivate: [UserRouteAccessService, CourseOverviewGuardService],
},
{
path: 'learning-path',
Expand All @@ -196,6 +197,7 @@ const routes: Routes = [
pageTitle: 'overview.learningPath',
showRefreshButton: true,
},
canActivate: [CourseOverviewGuardService],
},
{
path: 'communication',
Expand All @@ -216,7 +218,7 @@ const routes: Routes = [
hasSidebar: true,
showRefreshButton: true,
},
canActivate: [UserRouteAccessService],
canActivate: [UserRouteAccessService, CourseOverviewGuardService],
children: [
{
path: ':tutorialGroupId',
Expand All @@ -241,7 +243,7 @@ const routes: Routes = [
hasSidebar: true,
showRefreshButton: true,
},
canActivate: [UserRouteAccessService],
canActivate: [UserRouteAccessService, CourseOverviewGuardService],
children: [
{
path: ':examId',
Expand Down Expand Up @@ -275,6 +277,7 @@ const routes: Routes = [
hasSidebar: false,
showRefreshButton: true,
},
canActivate: [CourseOverviewGuardService],
},
{
path: '',
Expand Down
Loading