Skip to content

Commit

Permalink
Merge branch 'main' into Engine
Browse files Browse the repository at this point in the history
Signed-off-by: lunarmint <[email protected]>
  • Loading branch information
lunarmint committed May 11, 2022
2 parents b10756c + bdd50db commit ccbd421
Show file tree
Hide file tree
Showing 106 changed files with 15,422 additions and 2,056 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Optionally, in `scripts` directory:
- If you wish to change the user password, execute `mongo-changepwd.sh`.
- To delete everything from all databases as well as saved files, execute `delete-db.sh`.

**Step 5:** To add users with elevated privileges (a.k.a professors), include the user's email on separate lines in `professor-list.txt`.

## Local Development Environment

For local development environment, a Docker setup is not necessary. Make sure that the following softwares and dependencies are installed:
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package edu.oswego.cs.services;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;

import edu.oswego.cs.util.CPRException;
import org.bson.Document;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

public class IdentifyingService {
public void identifyingStudentService(SecurityContext securityContext, String studentID) {
if (!securityContext.isUserInRole("professor")) {
String userID = securityContext.getUserPrincipal().getName().split("@")[0];
if (!studentID.equals(userID))
throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).entity("User principal name doesn't match.").build());
throw new CPRException(Response.Status.FORBIDDEN, "User principal name doesn't match");
}
}

public void identifyingProfessorService(SecurityContext securityContext, MongoCollection<Document> courseCollection, String courseID) {
if (securityContext.isUserInRole("professor")) {
String userID = securityContext.getUserPrincipal().getName().split("@")[0];
Document courseDocument = courseCollection.find(Filters.eq("course_id", courseID)).first();
if (courseDocument == null) throw new CPRException(Response.Status.NOT_FOUND, "This professor does not exist.");
String professorID = courseDocument.getString("professor_id");
if (!userID.equals(professorID))
throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN).entity("User principal name doesn't match.").build());
if (!userID.equals(professorID))
throw new CPRException(Response.Status.FORBIDDEN, "User principal name doesn't match");
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,32 @@

import edu.oswego.cs.database.TeamInterface;
import edu.oswego.cs.requests.TeamParam;
import edu.oswego.cs.util.CPRException;
import org.bson.Document;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TeamService {

public String generateTeamID(@Context SecurityContext securityContext, String courseID){
List<Document> teamDocuments = new TeamInterface().getAllTeams(securityContext, courseID);

Set<String> teamIDs = new HashSet<>();
for (Document teamDocument : teamDocuments)
teamIDs.add(teamDocument.getString("team_id"));
for (int i = 0; i < teamIDs.size(); i++)
if (!teamIDs.contains(String.valueOf(i)))
return String.valueOf(i);
return String.valueOf(teamIDs.size());
}

public int getTeamSize(Document courseDocument) {
int teamSize = Integer.parseInt(courseDocument.getInteger("team_size").toString());
if (teamSize == 0) throw new WebApplicationException(Response.status(Response.Status.CONFLICT).entity("Team size not initialized.").build());
if (teamSize == 0) throw new CPRException(Response.Status.CONFLICT, "Team size not initialized");
return teamSize;
}

public String retrieveTeamID(@Context SecurityContext securityContext, TeamParam request) {
List<Document> teamDocuments = new TeamInterface().getAllTeams(securityContext, request.getCourseID());
if (teamDocuments == null || teamDocuments.size() == 0) throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity("No teams found.").build());

if (teamDocuments == null || teamDocuments.size() == 0) throw new CPRException(Response.Status.NOT_FOUND, "No teams found.");
for (Document teamDocument : teamDocuments) {
String teamDocumentCourseID = teamDocument.getString("course_id");
if (request.getCourseID().equals(teamDocumentCourseID)) {
List<String> students = teamDocument.getList("team_members", String.class);
if (students.contains(request.getStudentID())) return teamDocument.getString("team_id");
}
}
throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).entity("Student not found.").build());
throw new CPRException(Response.Status.NOT_FOUND, "Student not found.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.oswego.cs.util;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

public class CPRException extends WebApplicationException {
public CPRException(Response.Status responseStatus, String errorMessage) {
super(Response.status(responseStatus).entity(errorMessage).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import lombok.NonNull;
import javax.json.bind.annotation.JsonbCreator;
import javax.json.bind.annotation.JsonbProperty;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.HashMap;

@Entity
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import com.mongodb.client.MongoDatabase;
import edu.oswego.cs.rest.daos.AssignmentDAO;
import edu.oswego.cs.rest.daos.FileDAO;
import edu.oswego.cs.rest.util.CPRException;
import org.apache.commons.io.FileUtils;
import org.bson.Document;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.mongodb.client.model.Filters.and;
import static com.mongodb.client.model.Filters.eq;
Expand All @@ -39,8 +42,8 @@ public AssignmentInterface() {
submissionCollection = assignmentDatabase.getCollection("submissions");
MongoDatabase courseDatabase = manager.getCourseDB();
courseCollection = courseDatabase.getCollection("courses");
} catch (WebApplicationException e) {
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Failed to retrieve collections.").build());
} catch (CPRException e) {
throw new CPRException(Response.Status.BAD_REQUEST,"Failed to retrieve collections.");
}
}

Expand All @@ -65,22 +68,14 @@ public static String getRelPath() {
return relativePathPrefix.toString();
}

public static String findAssignment(String courseID, int assignmentID) {
return getRelPath() + "assignments" + reg + courseID + reg + assignmentID + reg + "assignments";
}

public static String findPeerReview(String courseID, int assignmentID) {
return getRelPath() + "assignments" + reg + courseID + reg + assignmentID + reg + "peer-reviews";
}

public static String findFile(String courseID, int assignmentID, String fileName) {
return getRelPath() + "assignments" + reg + courseID + reg + assignmentID + reg + "assignments" + reg + fileName;
}

public static String findPeerReviewFile(String courseID, int assignmentID, String fileName) {
String filePath = getRelPath() + "assignments" + reg + courseID + reg + assignmentID + reg + "peer-reviews" + reg + fileName;
if (!new File(filePath).exists())
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity(filePath + " does not exist.").build());
throw new CPRException(Response.Status.BAD_REQUEST,filePath + "does not exist");
return filePath;
}

Expand Down Expand Up @@ -115,7 +110,7 @@ public void removeFile(String courseID, String fileName, int assignmentID) {
String fileLocation = findFile(courseID, assignmentID, fileName);
File file = new File(fileLocation);
if (!file.delete())
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Assignment does not exist or could not be deleted.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"Assignment does not exist or could not be deleted.");
assignmentsCollection.updateOne(and(eq("course_id", courseID),
eq("assignment_id", assignmentID)),
set("assignment_instructions", ""));
Expand All @@ -125,7 +120,7 @@ public void removePeerReviewTemplate(String courseID, String fileName, int assig
String fileLocation = findPeerReviewFile(courseID, assignmentID, fileName);
File file = new File(fileLocation);
if (!file.delete())
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Assignment does not exist or could not be deleted.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"Assignment does not exist or could not be deleted.");
assignmentsCollection.updateOne(and(
eq("course_id", courseID),
eq("assignment_id", assignmentID)),
Expand All @@ -136,7 +131,7 @@ public void removePeerReviewRubric(String courseID, String fileName, int assignm
String fileLocation = findPeerReviewFile(courseID, assignmentID, fileName);
File file = new File(fileLocation);
if (!file.delete())
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Assignment does not exist or could not be deleted.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"Assignment does not exist or could not be deleted.");
assignmentsCollection.updateOne(and(
eq("course_id", courseID),
eq("assignment_id", assignmentID)),
Expand All @@ -145,41 +140,37 @@ public void removePeerReviewRubric(String courseID, String fileName, int assignm

public Document createAssignment(AssignmentDAO assignmentDAO) throws IOException {
Document courseDocument = courseCollection.find(eq("course_id", assignmentDAO.courseID)).first();
if (courseDocument == null) throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Course not found.").build());

if (courseDocument == null) throw new CPRException(Response.Status.BAD_REQUEST,"Course not found.");
String FileStructure = getRelPath() + "assignments" + reg + assignmentDAO.courseID;

File dir = new File(FileStructure);
if (!dir.mkdirs() && !dir.exists())
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Failed to create directory at " + dir.getAbsolutePath()).build());
if (!dir.mkdirs() && !dir.exists()) throw new CPRException(Response.Status.BAD_REQUEST,"Failed to create directory at" + dir.getAbsolutePath());

String[] dirList = dir.list();
if (dirList == null)
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Directory must exist to make file structure.").build());
if (dirList == null) throw new CPRException(Response.Status.BAD_REQUEST,"Directory must exist to make file structure.");

int nextPos = generateAssignmentID();
assignmentDAO.assignmentID = nextPos;

FileStructure += reg + nextPos;
if (!new File(FileStructure + reg + "team-submissions").mkdirs())
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Failed to create team-submission directory.").build());
if (!new File(FileStructure + reg + "team-submissions").mkdirs()) throw new CPRException(Response.Status.BAD_REQUEST,"Failed to create team-submission directory.");

if (!new File(FileStructure + reg + "peer-reviews").mkdirs()) {
deleteFile(FileStructure + reg + "team-submissions");
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Failed to create peer-review directory.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"Failed to create peer-review directory.");
}

if (!new File(FileStructure + reg + "assignments").mkdirs()) {
deleteFile(FileStructure + reg + "team-submissions");
deleteFile(FileStructure + reg + "peer-reviews");
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Failed to create assignments directory.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"Failed to create assignments directory");
}

if (!new File(FileStructure + reg + "peer-review-submission").mkdirs()) {
deleteFile(FileStructure + reg + "team-submissions");
deleteFile(FileStructure + reg + "peer-reviews");
deleteFile(FileStructure + reg + "assignments");
throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("Failed to create peer-review-submission directory.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"Failed to create peer-review-submission directory");
}

Jsonb jsonb = JsonbBuilder.create();
Expand All @@ -198,7 +189,7 @@ public Document createAssignment(AssignmentDAO assignmentDAO) throws IOException
deleteFile(FileStructure + reg + "assignments");
deleteFile(FileStructure + reg + "peer-review-submission");

throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("This assignment already exists.").build());
throw new CPRException(Response.Status.BAD_REQUEST,"This assignment already exists.");
}

assignmentsCollection.insertOne(assignmentDocument);
Expand All @@ -217,7 +208,7 @@ public List<Document> getAllAssignments() {

public List<Document> getAssignmentsByCourse(String courseID) {
MongoCursor<Document> query = assignmentsCollection.find(eq("course_id", courseID)).iterator();
if (!query.hasNext()) throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("This course does not exist").build());
if (!query.hasNext()) throw new CPRException(Response.Status.BAD_REQUEST,"This course does not exist.");

List<Document> assignments = new ArrayList<>();
while (query.hasNext()) {
Expand All @@ -231,13 +222,13 @@ public Document getSpecifiedAssignment(String courseID, int AssignmentID) {
Document assignment = assignmentsCollection.find(and(
eq("course_id", courseID),
eq("assignment_id", AssignmentID))).first();
if (assignment == null) throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("No assignment by this name found.").build());
if (assignment == null) throw new CPRException(Response.Status.BAD_REQUEST,"No assignment by this name found");
return assignment;
}

public void updateAssignment(AssignmentDAO assignmentDAO, String courseID, int assignmentID) {
Document assignmentDocument = assignmentsCollection.find(and(eq("assignment_id", assignmentID),eq("course_id", courseID))).first();
if (assignmentDocument == null) throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("This assignment does not exist.").build());
if (assignmentDocument == null) throw new CPRException(Response.Status.BAD_REQUEST,"This assignment does not exist.");
assignmentDocument.replace("assignment_name", assignmentDAO.assignmentName);
assignmentDocument.replace("due_date", assignmentDAO.dueDate);
assignmentDocument.replace("instructions", assignmentDAO.instructions);
Expand All @@ -253,7 +244,7 @@ public void removeAssignment(int AssignmentID, String courseID) throws IOExcepti
MongoCursor<Document> results = assignmentsCollection.find(and(
eq("assignment_id", AssignmentID),
eq("course_id", courseID))).iterator();
if (!results.hasNext()) throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("No assignment by this name found.").build());
if (!results.hasNext()) throw new CPRException(Response.Status.BAD_REQUEST,"No assignment by this name found.");

while (results.hasNext()) {
Document assignment = results.next();
Expand All @@ -270,7 +261,7 @@ public void removeSubmissions(int AssignmentID, String courseID) throws IOExcept

public void removeCourse(String courseID) throws IOException {
MongoCursor<Document> results = assignmentsCollection.find(eq("course_id", courseID)).iterator();
if (!results.hasNext()) throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("No assignment by this name found.").build());
if (!results.hasNext()) throw new CPRException(Response.Status.BAD_REQUEST,"No assignment by this name found.");

while (results.hasNext()) {
Document assignmentDocument = results.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.oswego.cs.rest.util;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

public class CPRException extends WebApplicationException {
public CPRException(Response.Status responseStatus, String errorMessage) {
super(Response.status(responseStatus).entity(errorMessage).build());
}
}
9 changes: 7 additions & 2 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
target/
!target/*.war

src/test
.idea/
.mvn/
src/test
.vscode/
mvnw
mvnw.cmd
.DS_Store
6 changes: 0 additions & 6 deletions frontend/.gitignore

This file was deleted.

Loading

0 comments on commit ccbd421

Please sign in to comment.