Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit f77a618

Browse files
committed
Added some privileges
1 parent fc3c08f commit f77a618

7 files changed

Lines changed: 74 additions & 49 deletions

File tree

EEDU-Backend/src/main/java/de/gaz/eedu/course/CourseController.java

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
import java.util.Set;
1919

20-
//TODO manage access. Yes, I'll do it later
21-
2220
@Slf4j
2321
@RestController
2422
@RequestMapping("/api/v1/course")
@@ -36,60 +34,65 @@ public class CourseController extends EntityController<Long, CourseService, Cour
3634
}
3735

3836
@PostMapping("/{course}/attach")
37+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_ATTACH_USER.toString())")
3938
public @NotNull ResponseEntity<Void> attachUser(@PathVariable long course, @NotNull @RequestBody Long... users)
4039
{
4140
log.info("Received incoming request for attaching user(s) {} to course {}.", users, course);
4241
return empty(getService().attachUser(course, users) ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
4342
}
4443

4544
@GetMapping("{course}/detach")
45+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_DETACH_USER.toString())")
4646
public @NotNull ResponseEntity<Void> detachUser(@PathVariable long course, @NotNull @RequestBody Long... users)
4747
{
4848
log.info("Received incoming request for detaching user(s) {} from course {}.", users, course);
4949
return empty(getService().detachUser(course, users) ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
5050
}
5151

5252
@PostMapping("/create")
53-
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_CREATE.toString())")
54-
@Override public @NotNull ResponseEntity<CourseModel[]> create(@NotNull @RequestBody CourseCreateModel[] model)
53+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_CREATE.toString())") @Override
54+
public @NotNull ResponseEntity<CourseModel[]> create(@NotNull @RequestBody CourseCreateModel[] model)
5555
{
5656
return super.create(model);
5757
}
5858

5959
@DeleteMapping("/delete/{id}")
60-
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_DELETE.toString())")
61-
@Override public @NotNull ResponseEntity<Void> delete(@NotNull @PathVariable Long[] id)
60+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_DELETE.toString())") @Override
61+
public @NotNull ResponseEntity<Void> delete(@NotNull @PathVariable Long[] id)
6262
{
6363
return super.delete(id);
6464
}
6565

66+
@GetMapping("/users/{course}")
67+
public @NotNull ResponseEntity<ReducedUserModel[]> getUsers(@PathVariable long course)
68+
{
69+
return ResponseEntity.ok(getService().loadReducedModelsByCourse(course).toArray(new ReducedUserModel[0]));
70+
}
71+
6672
@GetMapping("/get/{id}")
67-
@Override public @NotNull ResponseEntity<CourseModel> getData(@NotNull @PathVariable Long id)
73+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_GET.toString())") @Override
74+
public @NotNull ResponseEntity<CourseModel> getData(@NotNull @PathVariable Long id)
6875
{
6976
return super.getData(id);
7077
}
7178

72-
@GetMapping("/get/courses/{user}")
73-
public @NotNull ResponseEntity<CourseModel[]> getCourses(@PathVariable long user)
79+
@GetMapping("/get/all")
80+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_GET.toString())") @Override
81+
public @NotNull ResponseEntity<Set<CourseModel>> fetchAll()
7482
{
75-
return ResponseEntity.ok(getService().getCourses(user));
83+
return super.fetchAll();
7684
}
7785

78-
@GetMapping("/get")
86+
@GetMapping("/get") @PreAuthorize("@verificationService.fullyAuthenticated()")
7987
public @NotNull ResponseEntity<CourseModel[]> getOwnCourses(@AuthenticationPrincipal long user)
8088
{
8189
return getCourses(user);
8290
}
8391

84-
@GetMapping("/get/all")
85-
@Override public @NotNull ResponseEntity<Set<CourseModel>> fetchAll()
86-
{
87-
return super.fetchAll();
88-
}
89-
90-
@GetMapping("/users/{course}")
91-
public @NotNull ResponseEntity<ReducedUserModel[]> getUsers(@PathVariable long course)
92+
@GetMapping("/get/courses/{user}")
93+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_GET.toString())")
94+
public @NotNull ResponseEntity<CourseModel[]> getCourses(@PathVariable long user)
9295
{
93-
return ResponseEntity.ok(getService().loadReducedModelsByCourse(course).toArray(new ReducedUserModel[0]));
96+
return ResponseEntity.ok(getService().getCourses(user));
9497
}
9598
}

EEDU-Backend/src/main/java/de/gaz/eedu/course/classroom/ClassRoomController.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.gaz.eedu.course.classroom;
22

3-
import de.gaz.eedu.course.CourseService;
43
import de.gaz.eedu.course.classroom.model.ClassRoomCreateModel;
54
import de.gaz.eedu.course.classroom.model.ClassRoomModel;
65
import de.gaz.eedu.course.model.CourseModel;
@@ -28,14 +27,17 @@ public class ClassRoomController extends EntityController<String, ClassRoomServi
2827
private final ClassRoomService service;
2928

3029
@PostMapping("{course}/link/{classroom}")
30+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).CLASS_LINK_COURSE.toString())")
3131
public @NotNull ResponseEntity<Void> linkClass(@PathVariable long course, @PathVariable String classroom)
3232
{
3333
log.info("Received incoming request for linking the class {} to course {}.", classroom, course);
3434

3535
return empty(getService().linkClass(course, classroom) ? HttpStatus.OK : HttpStatus.NOT_MODIFIED);
3636
}
3737

38-
@PostMapping("{course}/unlink") public @NotNull ResponseEntity<Void> unlinkClass(@PathVariable long course)
38+
@PostMapping("{course}/unlink")
39+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).CLASS_UNLINK_CLASS.toString())")
40+
public @NotNull ResponseEntity<Void> unlinkClass(@PathVariable long course)
3941
{
4042
log.info("Received incoming request for unlinking the current class from course {}.", course);
4143
return empty(getService().unlinkClass(course) ? HttpStatus.OK : HttpStatus.NOT_MODIFIED);
@@ -60,18 +62,24 @@ public class ClassRoomController extends EntityController<String, ClassRoomServi
6062
{
6163
return super.delete(id);
6264
}
63-
6465
@GetMapping("/get/{id}")
65-
@PreAuthorize("@verificationService.isFullyAuthenticated()")
66+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).CLASS_GET.toString())")
6667
@Override public @NotNull ResponseEntity<ClassRoomModel> getData(@NotNull @PathVariable String id)
6768
{
6869
return super.getData(id);
6970
}
7071

7172
@GetMapping("/get/all")
72-
@PreAuthorize("@verificationService.isFullyAuthenticated()")
73+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_GET.toString())")
7374
@Override public @NotNull ResponseEntity<Set<ClassRoomModel>> fetchAll()
7475
{
7576
return super.fetchAll();
7677
}
78+
79+
@GetMapping("/get")
80+
@PreAuthorize("@verificationService.fullyAuthenticated()")
81+
public @NotNull ResponseEntity<ClassRoomModel[]> getOwn(@AuthenticationPrincipal long user)
82+
{
83+
return ResponseEntity.ok(getService().loadClassesByUser(user).toArray(new ClassRoomModel[0]));
84+
}
7785
}

EEDU-Backend/src/main/java/de/gaz/eedu/course/classroom/ClassRoomRepository.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.gaz.eedu.course.classroom;
22

3-
import de.gaz.eedu.course.model.CourseModel;
3+
import de.gaz.eedu.course.CourseEntity;
44
import de.gaz.eedu.user.model.ReducedUserModel;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Unmodifiable;
@@ -22,8 +22,11 @@ public interface ClassRoomRepository extends JpaRepository<ClassRoomEntity, Stri
2222
@Query("SELECT COUNT(u) > 0 FROM CourseEntity c JOIN c.users u WHERE c.id = :courseId AND u.id = :userId")
2323
boolean existsUserInCourse(long userId, long courseId);
2424

25-
@Query("SELECT co FROM ClassRoomEntity c JOIN c.courses co WHERE c.id = :id")
26-
@NotNull @Unmodifiable Set<CourseModel> findAllCoursesById(long id);
25+
@Query("SELECT c FROM ClassRoomEntity c JOIN c.users u WHERE u.id = :id")
26+
@NotNull @Unmodifiable Set<ClassRoomEntity> findAllByUserId(long id);
27+
28+
@Query("SELECT co FROM ClassRoomEntity c JOIN c.courses co WHERE co.id = :id")
29+
@NotNull @Unmodifiable Set<CourseEntity> findAllCoursesById(long id);
2730

2831
@Query(
2932
"SELECT new de.gaz.eedu.user.model.ReducedUserModel(u.id, u.firstName, u.lastName, u.accountType) " +

EEDU-Backend/src/main/java/de/gaz/eedu/course/classroom/ClassRoomService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
import org.jetbrains.annotations.Contract;
2323
import org.jetbrains.annotations.NotNull;
2424
import org.jetbrains.annotations.Unmodifiable;
25-
import org.springframework.http.HttpStatus;
26-
import org.springframework.http.ResponseEntity;
2725
import org.springframework.stereotype.Service;
2826
import org.springframework.web.bind.annotation.PathVariable;
29-
import org.springframework.web.bind.annotation.PostMapping;
3027
import org.springframework.web.server.ResponseStatusException;
3128

3229
import java.util.*;
3330
import java.util.function.Function;
31+
import java.util.stream.Collectors;
3432
import java.util.stream.Stream;
3533

3634
/**
@@ -137,12 +135,19 @@ public class ClassRoomService extends EntityService<String, ClassRoomRepository,
137135
{
138136
if (hasRole("ADMINISTRATOR") || getRepository().existsUserInCourse(user, classroomId))
139137
{
140-
return getRepository().findAllCoursesById(classroomId);
138+
Stream<CourseEntity> courseEntities = getRepository().findAllCoursesById(classroomId).stream();
139+
return courseEntities.map(CourseEntity::toModel).collect(Collectors.toUnmodifiableSet());
141140
}
142141

143142
throw unauthorizedThrowable();
144143
}
145144

145+
public @NotNull @Unmodifiable Set<ClassRoomModel> loadClassesByUser(long user)
146+
{
147+
Stream<ClassRoomEntity> classRoomEntityStream = getRepository().findAllByUserId(user).stream();
148+
return classRoomEntityStream.map(ClassRoomEntity::toModel).collect(Collectors.toUnmodifiableSet());
149+
}
150+
146151
/**
147152
* Fetches a {@link UserEntity} and validates that its account type is {@link AccountType#TEACHER}.
148153
* <p>

EEDU-Backend/src/main/java/de/gaz/eedu/course/room/RoomController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public class RoomController extends EntityController<String, RoomService, RoomMo
2323
private final RoomService service;
2424

2525
@PostMapping("/create")
26-
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).ROOM_CREATE.toString())")
27-
@Override
26+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).ROOM_CREATE.toString())") @Override
2827
public @NotNull ResponseEntity<RoomModel[]> create(@NotNull @RequestBody RoomCreateModel[] model) throws CreationException
2928
{
3029
return super.create(model);
3130
}
3231

3332
@DeleteMapping("/delete/{id}")
34-
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).ROOM_DELETE.toString())")
35-
@Override public @NotNull ResponseEntity<Void> delete(@NotNull @PathVariable String[] id)
33+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).ROOM_DELETE.toString())") @Override
34+
public @NotNull ResponseEntity<Void> delete(@NotNull @PathVariable String[] id)
3635
{
3736
return super.delete(id);
3837
}
3938

4039
@GetMapping("/get/all")
41-
@Override public @NotNull ResponseEntity<Set<RoomModel>> fetchAll() {return super.fetchAll();}
40+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).ROOM_GET.toString())") @Override
41+
public @NotNull ResponseEntity<Set<RoomModel>> fetchAll() {return super.fetchAll();}
4242
}

EEDU-Backend/src/main/java/de/gaz/eedu/course/subject/SubjectController.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import java.util.Set;
1818

19-
//TODO manage access
20-
2119
@Slf4j
2220
@RestController
2321
@RequestMapping("/api/v1/course/subject")
@@ -27,34 +25,36 @@ public class SubjectController extends EntityController<String, SubjectService,
2725
@Getter(AccessLevel.PROTECTED) private final SubjectService service;
2826

2927
@PostMapping("/create")
30-
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_CREATE.toString())")
31-
@Override public @NotNull ResponseEntity<SubjectModel[]> create(@NotNull @RequestBody SubjectCreateModel[] model) throws CreationException
28+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_CREATE.toString())") @Override
29+
public @NotNull ResponseEntity<SubjectModel[]> create(@NotNull @RequestBody SubjectCreateModel[] model) throws CreationException
3230
{
3331
return super.create(model);
3432
}
3533

3634
@DeleteMapping("/delete/{id}")
37-
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_DELETE.toString())")
38-
@Override public @NotNull ResponseEntity<Void> delete(@NotNull @PathVariable String[] id)
35+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_DELETE.toString())") @Override
36+
public @NotNull ResponseEntity<Void> delete(@NotNull @PathVariable String[] id)
3937
{
4038
return super.delete(id);
4139
}
4240

43-
//@PreAuthorize("isAuthenticated()")
44-
4541
@GetMapping("/get/{id}")
46-
@Override public @NotNull ResponseEntity<SubjectModel> getData(@NotNull @PathVariable String id)
42+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_GET.toString())") @Override
43+
public @NotNull ResponseEntity<SubjectModel> getData(@NotNull @PathVariable String id)
4744
{
4845
return super.getData(id);
4946
}
47+
5048
@GetMapping("/get/all")
51-
@Override public @NotNull ResponseEntity<Set<SubjectModel>> fetchAll()
49+
@PreAuthorize("hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_GET.toString())") @Override
50+
public @NotNull ResponseEntity<Set<SubjectModel>> fetchAll()
5251
{
5352
return super.fetchAll();
5453
}
5554

56-
@GetMapping("/courses/{subjects}")
57-
public @NotNull ResponseEntity<CourseModel[]> getCourses(@NotNull @PathVariable String[] subjects)
55+
@GetMapping("/courses/{subjects}") @PreAuthorize(
56+
"hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).SUBJECT_GET.toString()) and hasAuthority(T(de.gaz.eedu.user.privileges.SystemPrivileges).COURSE_GET.toString())"
57+
) public @NotNull ResponseEntity<CourseModel[]> getCourses(@NotNull @PathVariable String[] subjects)
5858
{
5959
return ResponseEntity.ok(getService().loadCourses(subjects));
6060
}

EEDU-Backend/src/main/java/de/gaz/eedu/user/privileges/SystemPrivileges.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
public enum SystemPrivileges
1111
{
1212
CLASS_CREATE,
13+
CLASS_ATTACH_USER,
14+
CLASS_DETACH_USER,
15+
CLASS_LINK_COURSE,
16+
CLASS_UNLINK_CLASS,
1317
CLASS_DELETE,
1418
CLASS_GET,
1519

@@ -22,6 +26,8 @@ public enum SystemPrivileges
2226
ROOM_GET,
2327

2428
COURSE_CREATE,
29+
COURSE_ATTACH_USER,
30+
COURSE_DETACH_USER,
2531
COURSE_DELETE,
2632
COURSE_GET,
2733

0 commit comments

Comments
 (0)