Skip to content

Commit cc89da4

Browse files
committed
Add check that student can only view own courses
1 parent 535fb45 commit cc89da4

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main/java/net/hackyourfuture/coursehub/web/StudentController.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package net.hackyourfuture.coursehub.web;
22

33
import jakarta.validation.constraints.Positive;
4+
import net.hackyourfuture.coursehub.data.AuthenticatedUser;
45
import net.hackyourfuture.coursehub.service.CourseService;
56
import net.hackyourfuture.coursehub.web.model.CourseListResponse;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.HttpStatusCode;
9+
import org.springframework.security.core.annotation.AuthenticationPrincipal;
610
import org.springframework.web.bind.annotation.GetMapping;
711
import org.springframework.web.bind.annotation.PathVariable;
812
import org.springframework.web.bind.annotation.RequestMapping;
913
import org.springframework.web.bind.annotation.RestController;
14+
import org.springframework.web.client.HttpClientErrorException;
15+
import org.springframework.web.client.HttpServerErrorException;
16+
import org.springframework.web.client.HttpStatusCodeException;
1017

1118
@RestController
1219
@RequestMapping("/students")
@@ -19,7 +26,11 @@ public StudentController(CourseService courseService) {
1926
}
2027

2128
@GetMapping("/{studentId}/courses")
22-
public CourseListResponse getCoursesForStudent(@PathVariable @Positive Integer studentId) {
29+
public CourseListResponse getCoursesForStudent(
30+
@PathVariable @Positive Integer studentId, @AuthenticationPrincipal AuthenticatedUser user) {
31+
if (!user.getUserId().equals(studentId)) {
32+
throw new HttpClientErrorException(HttpStatus.FORBIDDEN);
33+
}
2334
var courses = courseService.getCoursesForStudent(studentId);
2435
return new CourseListResponse(courses);
2536
}

0 commit comments

Comments
 (0)