Skip to content

Commit

Permalink
Add studentnumber to user(reference)json
Browse files Browse the repository at this point in the history
Hide if the user requesting it is not an admin of the course
  • Loading branch information
Aqua-sc committed May 17, 2024
1 parent e106cd8 commit a48ade6
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,20 @@ 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("studentnumber").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("studentnumber").asString();
studentnumber = jwt.getClaim("ugentStudentID").asString();
} else {
throw new JwkException("Invalid OAuth version");
}
// print full object
logger.info(jwt.getClaims());



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

Auth authUser = new Auth(user, new ArrayList<>());
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 @@ -113,7 +118,7 @@ public ResponseEntity<?> createClusterForCourse(@PathVariable("courseid") Long c
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 +143,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 @@ -175,7 +183,7 @@ public ResponseEntity<?> doGroupClusterUpdate(GroupClusterEntity clusterEntity,
clusterEntity.setMaxSize(clusterJson.getCapacity());
clusterEntity.setName(clusterJson.getName());
clusterEntity = groupClusterRepository.save(clusterEntity);
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(clusterEntity));
return ResponseEntity.ok(entityToJsonConverter.clusterEntityToClusterJson(clusterEntity, false));
}

/**
Expand Down Expand Up @@ -226,7 +234,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 Down Expand Up @@ -317,6 +325,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 @@ -312,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
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ResponseEntity<?> getSubmissions(@PathVariable("projectid") long projecti
if (group == null) {
throw new RuntimeException("Group not found");
}
GroupJson groupjson = entityToJsonConverter.groupEntityToJson(group);
GroupJson groupjson = entityToJsonConverter.groupEntityToJson(group, false);
GroupFeedbackEntity groupFeedbackEntity = groupFeedbackRepository.getGroupFeedback(groupId, projectid);
GroupFeedbackJson groupFeedbackJson;
if (groupFeedbackEntity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class UserJson {
private String surname;
private String email;
private UserRole role;
private String studentNumber;

private OffsetDateTime createdAt;

Expand All @@ -29,6 +30,7 @@ public UserJson(UserEntity entity) {
this.email = entity.getEmail();
this.role = entity.getRole();
this.createdAt = entity.getCreatedAt();
this.studentNumber = entity.getStudentNumber();
// this.courses = new ArrayList<>();
}

Expand Down Expand Up @@ -96,4 +98,11 @@ public String getProjectUrl() {
}
public void setProjectUrl(String s){}

public String getStudentNumber() {
return studentNumber;
}

public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ public class UserReferenceJson {
private String name;
private String email;
private Long userId;
private String studentNumber;

public UserReferenceJson(String name, String email, Long userId) {
public UserReferenceJson(String name, String email, Long userId, String studentNumber) {
this.name = name;
this.email = email;
this.userId = userId;
this.studentNumber = studentNumber;
}

public String getEmail() {
Expand Down Expand Up @@ -39,4 +41,11 @@ public void setName(String name) {
}


public String getStudentNumber() {
return studentNumber;
}

public void setStudentNumber(String studentNumber) {
this.studentNumber = studentNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public interface UserReference {
Long getUserId();
String getName();
String getEmail();
String getStudentNumber();
}
@Query(value= """
SELECT gu.userId as userId, u.name, CONCAT(u.name, ' ', u.surname) as name, u.email as email
SELECT gu.userId as userId, u.name, CONCAT(u.name, ' ', u.surname) as name, u.email as email, u.studentNumber as studentNumber
FROM GroupUserEntity gu JOIN UserEntity u ON u.id = gu.userId
WHERE gu.groupId = ?1""")
List<UserReference> findGroupUsersReferencesByGroupId(long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class EntityToJsonConverter {
private TestRepository testRepository;


public GroupJson groupEntityToJson(GroupEntity groupEntity) {
public GroupJson groupEntityToJson(GroupEntity groupEntity, boolean hideStudentNumber) {
GroupClusterEntity cluster = groupClusterRepository.findById(groupEntity.getClusterId()).orElse(null);
if (cluster == null) {
throw new RuntimeException("Cluster not found");
Expand All @@ -54,7 +54,7 @@ public GroupJson groupEntityToJson(GroupEntity groupEntity) {
}
// Get the members of the group
List<UserReferenceJson> members = groupRepository.findGroupUsersReferencesByGroupId(groupEntity.getId()).stream().map(user ->
new UserReferenceJson(user.getName(), user.getEmail(), user.getUserId())
new UserReferenceJson(user.getName(), user.getEmail(), user.getUserId(), hideStudentNumber ? null : user.getStudentNumber())
).toList();

// Return the group with its members
Expand All @@ -63,9 +63,9 @@ public GroupJson groupEntityToJson(GroupEntity groupEntity) {
}


public GroupClusterJson clusterEntityToClusterJson(GroupClusterEntity cluster) {
public GroupClusterJson clusterEntityToClusterJson(GroupClusterEntity cluster, boolean hideStudentNumber) {
List<GroupJson> groups = groupRepository.findAllByClusterId(cluster.getId()).stream().map(
this::groupEntityToJson
g -> groupEntityToJson(g, hideStudentNumber)
).toList();
return new GroupClusterJson(
cluster.getId(),
Expand All @@ -78,20 +78,26 @@ public GroupClusterJson clusterEntityToClusterJson(GroupClusterEntity cluster) {
);
}

public UserReferenceJson userEntityToUserReference(UserEntity user) {
return new UserReferenceJson(user.getName() + " " + user.getSurname(), user.getEmail(), user.getId());
public UserReferenceJson userEntityToUserReference(UserEntity user, boolean hideStudentNumber) {
return new UserReferenceJson(
user.getName() + " " + user.getSurname(),
user.getEmail(), user.getId(),
hideStudentNumber ? null : user.getStudentNumber()
);
}

public UserReferenceWithRelation userEntityToUserReferenceWithRelation(UserEntity user, CourseRelation relation) {
return new UserReferenceWithRelation(userEntityToUserReference(user), relation.toString());
public UserReferenceWithRelation userEntityToUserReferenceWithRelation(UserEntity user, CourseRelation relation, boolean hideStudentNumber) {
return new UserReferenceWithRelation(userEntityToUserReference(user, hideStudentNumber), relation.toString());
}

public CourseWithInfoJson courseEntityToCourseWithInfo(CourseEntity course, String joinLink, boolean hideKey) {
UserEntity teacher = courseRepository.findTeacherByCourseId(course.getId());
UserReferenceJson teacherJson = userEntityToUserReference(teacher);
UserReferenceJson teacherJson = userEntityToUserReference(teacher, true);

List<UserEntity> assistants = courseRepository.findAssistantsByCourseId(course.getId());
List<UserReferenceJson> assistantsJson = assistants.stream().map(this::userEntityToUserReference).toList();
List<UserReferenceJson> assistantsJson = assistants.stream().map(
u -> userEntityToUserReference(u, true)
).toList();

return new CourseWithInfoJson(
course.getId(),
Expand Down

0 comments on commit a48ade6

Please sign in to comment.