Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
AWerbrouck committed May 18, 2024
2 parents 851bdac + 149f70f commit ad77b1f
Show file tree
Hide file tree
Showing 62 changed files with 1,346 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public ResponseEntity<ApiErrorReponse> handleNoHandlerFoundException(HttpServlet
"Resource/endpoint doesn't exist", path));
}

@ExceptionHandler(NoResourceFoundException.class)
public ResponseEntity<ApiErrorReponse> handleNoResourceFoundException(HttpServletRequest request, Exception ex) {
logError(ex);
String path = request.getRequestURI();
HttpStatus status = HttpStatus.NOT_FOUND;
return ResponseEntity.status(status).body(new ApiErrorReponse(OffsetDateTime.now(), status.value(), status.getReasonPhrase(),
"Resource/endpoint doesn't exist", path));
}

/* Gets thrown when the method is not allowed */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<ApiErrorReponse> handleMethodNotSupportedException(HttpServletRequest request, Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void doFilterInternal(HttpServletRequest request, HttpServletResponse res
String lastName;
String email;
String oid;
String studentnumber;

String version = jwt.getClaim("ver").asString();

Expand All @@ -92,21 +93,21 @@ public void doFilterInternal(HttpServletRequest request, HttpServletResponse res
lastName = jwt.getClaim("family_name").asString();
email = jwt.getClaim("unique_name").asString();
oid = jwt.getClaim("oid").asString();
studentnumber = jwt.getClaim("ugentStudentID").asString();
} else if (version.startsWith("2.0")) {
displayName = jwt.getClaim("name").asString();
lastName = jwt.getClaim("surname").asString();
firstName = displayName.replace(lastName, "").strip();
email = jwt.getClaim("mail").asString();
oid = jwt.getClaim("oid").asString();
studentnumber = jwt.getClaim("ugentStudentID").asString();
} else {
throw new JwkException("Invalid OAuth version");
}
// print full object
// logger.info(jwt.getClaims());
logger.info(jwt.getClaims());



User user = new User(displayName, firstName,lastName, email, oid);
User user = new User(displayName, firstName,lastName, email, oid, studentnumber);

Auth authUser = new Auth(user, new ArrayList<>());
SecurityContextHolder.getContext().setAuthentication(authUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons

if(userEntity == null) {
System.out.println("User does not exist, creating new one. user_id: " + auth.getOid());
userEntity = new UserEntity(auth.getUser().firstName,auth.getUser().lastName, auth.getEmail(), UserRole.student, auth.getOid());
userEntity = new UserEntity(auth.getUser().firstName,auth.getUser().lastName, auth.getEmail(), UserRole.student, auth.getOid(), auth.getStudentNumber());
OffsetDateTime now = OffsetDateTime.now();
userEntity.setCreatedAt(now);
userEntity = userRepository.save(userEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ public ResponseEntity<?> getClustersForCourse(@PathVariable("courseid") Long cou
if (checkResult.getStatus() != HttpStatus.OK) {
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}

CourseRelation courseRelation = checkResult.getData().getSecond();
boolean hideStudentNumber = courseRelation.equals(CourseRelation.enrolled);

// Get the clusters for the course
List<GroupClusterEntity> clusters = groupClusterRepository.findClustersWithoutInvidualByCourseId(courseid);
List<GroupClusterJson> clusterJsons = clusters.stream().map(
entityToJsonConverter::clusterEntityToClusterJson).toList();
g -> entityToJsonConverter.clusterEntityToClusterJson(g, hideStudentNumber)
).toList();
// Return the clusters
return ResponseEntity.ok(clusterJsons);
}
Expand Down Expand Up @@ -107,13 +112,14 @@ public ResponseEntity<?> createClusterForCourse(@PathVariable("courseid") Long c
clusterJson.groupCount()
);
cluster.setCreatedAt(OffsetDateTime.now());
cluster.setLockGroupsAfter(clusterJson.lockGroupsAfter());
GroupClusterEntity clusterEntity = groupClusterRepository.save(cluster);

for (int i = 0; i < clusterJson.groupCount(); i++) {
groupRepository.save(new GroupEntity("Group " + (i + 1), cluster.getId()));
}

GroupClusterJson clusterJsonResponse = entityToJsonConverter.clusterEntityToClusterJson(clusterEntity);
GroupClusterJson clusterJsonResponse = entityToJsonConverter.clusterEntityToClusterJson(clusterEntity, false);

// Return the cluster
return ResponseEntity.status(HttpStatus.CREATED).body(clusterJsonResponse);
Expand All @@ -138,8 +144,11 @@ public ResponseEntity<?> getCluster(@PathVariable("clusterid") Long clusterid, A
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}
GroupClusterEntity cluster = checkResult.getData();

CheckResult<CourseEntity> courseAdmin = courseUtil.getCourseIfAdmin(cluster.getCourseId(), auth.getUserEntity());
boolean hideStudentNumber = !courseAdmin.getStatus().equals(HttpStatus.OK);
// Return the cluster
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(cluster));
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(cluster, hideStudentNumber));
}


Expand Down Expand Up @@ -174,8 +183,9 @@ public ResponseEntity<?> doGroupClusterUpdate(GroupClusterEntity clusterEntity,
}
clusterEntity.setMaxSize(clusterJson.getCapacity());
clusterEntity.setName(clusterJson.getName());
clusterEntity.setLockGroupsAfter(clusterJson.getLockGroupsAfter());
clusterEntity = groupClusterRepository.save(clusterEntity);
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(clusterEntity));
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(clusterEntity, false));
}

/**
Expand Down Expand Up @@ -226,7 +236,7 @@ public ResponseEntity<?> fillCluster(@PathVariable("clusterid") Long clusterid,

groupCluster.setGroupAmount(clusterFillJson.getClusterGroupMembers().size());
groupClusterRepository.save(groupCluster);
return ResponseEntity.status(HttpStatus.OK).body(entityToJsonConverter.clusterEntityToClusterJson(groupCluster));
return ResponseEntity.status(HttpStatus.OK).body(entityToJsonConverter.clusterEntityToClusterJson(groupCluster, false));
} catch (Exception e) {
Logger.getGlobal().severe(e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong");
Expand All @@ -253,6 +263,10 @@ public ResponseEntity<?> patchCluster(@PathVariable("clusterid") Long clusterid,
clusterJson.setName(cluster.getName());
}

if (clusterJson.getLockGroupsAfter() == null) {
clusterJson.setLockGroupsAfter(cluster.getLockGroupsAfter());
}

return doGroupClusterUpdate(cluster, clusterJson);
}

Expand Down Expand Up @@ -317,6 +331,6 @@ public ResponseEntity<?> createGroupForCluster(@PathVariable("clusterid") Long c

cluster.setGroupAmount(cluster.getGroupAmount() + 1);
groupClusterRepository.save(cluster);
return ResponseEntity.status(HttpStatus.CREATED).body(entityToJsonConverter.groupEntityToJson(group));
return ResponseEntity.status(HttpStatus.CREATED).body(entityToJsonConverter.groupEntityToJson(group, false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,16 @@ public ResponseEntity<?> getCourseMembers(Auth auth, @PathVariable Long courseId
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}

boolean hideStudentNumber = checkResult.getData().getSecond().equals(CourseRelation.enrolled);

List<CourseUserEntity> members = courseUserRepository.findAllMembers(courseId);
List<UserReferenceWithRelation> memberJson = members.stream().
map(cue -> {
UserEntity user = userUtil.getUserIfExists(cue.getUserId());
if (user == null) {
return null;
}
return entityToJsonConverter.userEntityToUserReferenceWithRelation(user, cue.getRelation());
return entityToJsonConverter.userEntityToUserReferenceWithRelation(user, cue.getRelation(), hideStudentNumber);
}).
filter(Objects::nonNull).toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import com.ugent.pidgeon.postgre.models.types.UserRole;
import com.ugent.pidgeon.postgre.repository.GroupRepository;
import com.ugent.pidgeon.util.CheckResult;
import com.ugent.pidgeon.util.ClusterUtil;
import com.ugent.pidgeon.util.CommonDatabaseActions;
import com.ugent.pidgeon.util.CourseUtil;
import com.ugent.pidgeon.util.EntityToJsonConverter;
import com.ugent.pidgeon.util.GroupUtil;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,6 +30,10 @@ public class GroupController {
private EntityToJsonConverter entityToJsonConverter;
@Autowired
private CommonDatabaseActions commonDatabaseActions;
@Autowired
private ClusterUtil clusterUtil;
@Autowired
private CourseUtil courseUtil;


/**
Expand All @@ -50,8 +56,14 @@ public ResponseEntity<?> getGroupById(@PathVariable("groupid") Long groupid, Aut
return ResponseEntity.status(checkResult1.getStatus()).body(checkResult1.getMessage());
}

boolean hideStudentNumber = true;
CheckResult<Void> adminCheck = groupUtil.isAdminOfGroup(groupid, auth.getUserEntity());
if (adminCheck.getStatus().equals(HttpStatus.OK)) {
hideStudentNumber = false;
}

// Return the group
GroupJson groupJson = entityToJsonConverter.groupEntityToJson(group);
GroupJson groupJson = entityToJsonConverter.groupEntityToJson(group, hideStudentNumber);
return ResponseEntity.ok(groupJson);
}

Expand Down Expand Up @@ -113,7 +125,7 @@ private ResponseEntity<?> doGroupNameUpdate(Long groupid, NameRequest nameReques
groupRepository.save(group);

// Return the updated group
GroupJson groupJson = entityToJsonConverter.groupEntityToJson(group);
GroupJson groupJson = entityToJsonConverter.groupEntityToJson(group, false);
return ResponseEntity.ok(groupJson);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public ResponseEntity<Object> addMemberToGroup(@PathVariable("groupid") long gro
try {
groupMemberRepository.addMemberToGroup(groupId, memberid);
List<UserEntity> members = groupMemberRepository.findAllMembersByGroupId(groupId);
List<UserReferenceJson> response = members.stream().map(entityToJsonConverter::userEntityToUserReference).toList();
List<UserReferenceJson> response = members.stream().map(
u -> entityToJsonConverter.userEntityToUserReference(u, false)
).toList();
return ResponseEntity.ok(response);
} catch (Exception e) {
Logger.getGlobal().severe(e.getMessage());
Expand Down Expand Up @@ -143,7 +145,9 @@ public ResponseEntity<Object> addMemberToGroupInferred(@PathVariable("groupid")
try {
groupMemberRepository.addMemberToGroup(groupId,user.getId());
List<UserEntity> members = groupMemberRepository.findAllMembersByGroupId(groupId);
List<UserReferenceJson> response = members.stream().map(entityToJsonConverter::userEntityToUserReference).toList();
List<UserReferenceJson> response = members.stream().map(
u -> entityToJsonConverter.userEntityToUserReference(u, true)
).toList();
return ResponseEntity.ok(response);
} catch (Exception e) {
Logger.getGlobal().severe(e.getMessage());
Expand Down Expand Up @@ -171,8 +175,12 @@ public ResponseEntity<Object> findAllMembersByGroupId(@PathVariable("groupid") l
return ResponseEntity.status(checkResult.getStatus()).body(checkResult.getMessage());
}

boolean hideStudentnumber = !groupUtil.isAdminOfGroup(groupId, user).getStatus().equals(HttpStatus.OK);

List<UserEntity> members = groupMemberRepository.findAllMembersByGroupId(groupId);
List<UserReferenceJson> response = members.stream().map((UserEntity e) -> entityToJsonConverter.userEntityToUserReference(e)).toList();
List<UserReferenceJson> response = members.stream().map(
(UserEntity e) -> entityToJsonConverter.userEntityToUserReference(e, hideStudentnumber))
.toList();
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.ugent.pidgeon.postgre.models.types.UserRole;
import com.ugent.pidgeon.postgre.repository.*;
import com.ugent.pidgeon.util.*;
import java.time.OffsetDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -74,6 +75,10 @@ public ResponseEntity<?> getProjects(Auth auth) {
CourseRelation relation = courseCheck.getData().getSecond();

if (relation.equals(CourseRelation.enrolled)) {
if (project.getVisibleAfter() != null && project.getVisibleAfter().isBefore(OffsetDateTime.now())) {
project.setVisible(true);
projectRepository.save(project);
}
if (project.isVisible()) {
enrolledProjects.add(entityToJsonConverter.projectEntityToProjectResponseJsonWithStatus(project, course, user));
}
Expand Down Expand Up @@ -113,6 +118,11 @@ public ResponseEntity<?> getProjectById(@PathVariable Long projectId, Auth auth)
}
CourseEntity course = courseCheck.getData().getFirst();
CourseRelation relation = courseCheck.getData().getSecond();

if (project.getVisibleAfter() != null && project.getVisibleAfter().isBefore(OffsetDateTime.now())) {
project.setVisible(true);
projectRepository.save(project);
}
if (!project.isVisible() && relation.equals(CourseRelation.enrolled)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project not found");
}
Expand Down Expand Up @@ -161,6 +171,8 @@ public ResponseEntity<Object> createProject(
projectJson.getGroupClusterId(), null, projectJson.isVisible(),
projectJson.getMaxScore(), projectJson.getDeadline());

project.setVisibleAfter(projectJson.getVisibleAfter());

// Save the project entity
ProjectEntity savedProject = projectRepository.save(project);
CourseEntity courseEntity = checkAcces.getData();
Expand All @@ -180,6 +192,10 @@ private ResponseEntity<?> doProjectUpdate(ProjectEntity project, ProjectJson pro
project.setDeadline(projectJson.getDeadline());
project.setMaxScore(projectJson.getMaxScore());
project.setVisible(projectJson.isVisible());
project.setVisibleAfter(projectJson.getVisibleAfter());
if (project.getVisibleAfter() != null && project.getVisibleAfter().isBefore(OffsetDateTime.now())) {
project.setVisible(true);
}
projectRepository.save(project);
return ResponseEntity.ok(entityToJsonConverter.projectEntityToProjectResponseJson(project, courseRepository.findById(project.getCourseId()).get(), user));
}
Expand Down Expand Up @@ -261,6 +277,10 @@ public ResponseEntity<?> patchProjectById(@PathVariable Long projectId, @Request
projectJson.setVisible(project.isVisible());
}

if (projectJson.getVisibleAfter() == null) {
projectJson.setVisibleAfter(project.getVisibleAfter());
}

CheckResult<Void> checkProject = projectUtil.checkProjectJson(projectJson, project.getCourseId());
if (checkProject.getStatus() != HttpStatus.OK) {
return ResponseEntity.status(checkProject.getStatus()).body(checkProject.getMessage());
Expand Down Expand Up @@ -292,11 +312,15 @@ public ResponseEntity<?> getGroupsOfProject(@PathVariable Long projectId, Auth a
"No groups for this project: use " + memberUrl + " to get the members of the course");
}

boolean hideStudentNumber;
CheckResult<Void> adminCheck = projectUtil.isProjectAdmin(projectId, auth.getUserEntity());
hideStudentNumber = !adminCheck.getStatus().equals(HttpStatus.OK);

List<Long> groups = projectRepository.findGroupIdsByProjectId(projectId);
List<GroupJson> groupjsons = groups.stream()
.map((Long id) -> {
return groupRepository.findById(id).orElse(null);
}).filter(Objects::nonNull).map(entityToJsonConverter::groupEntityToJson).toList();
.map((Long id) -> groupRepository.findById(id).orElse(null)).filter(Objects::nonNull).map(
g -> entityToJsonConverter.groupEntityToJson(g, hideStudentNumber))
.toList();
return ResponseEntity.ok(groupjsons);
}

Expand Down
Loading

0 comments on commit ad77b1f

Please sign in to comment.