Skip to content

Commit 63db37e

Browse files
committed
Add code to detect a missing user id where it is required.
1 parent 8ae7a53 commit 63db37e

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/main/java/com/parallax/server/blocklyprop/db/dao/impl/ProjectDaoImpl.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,10 @@ public List<ProjectRecord> getUserProjects(
412412
* @param order
413413
* @param limit
414414
* @param offset
415-
* @param idUser
415+
416416
* @return
417+
* Returns a list of ProjectRecord objects corresponding to the projects
418+
* matching the selection creiteria
417419
*/
418420
@Override
419421
public List<ProjectRecord> getSharedProjects(
@@ -563,39 +565,61 @@ public boolean deleteProject(Long idProject) {
563565
}
564566

565567
/**
566-
* TODO: add details.
568+
* Update the code block in the specified project
567569
*
568570
* @param idProject
569571
* @param code
572+
*
570573
* @return
571574
*/
572575
@Override
573576
public ProjectRecord updateProjectCode(Long idProject, String code) {
574577
LOG.info("Update code for project {}.", idProject);
578+
579+
// Retrieve the specified project
575580
ProjectRecord record = create.selectFrom(Tables.PROJECT)
576581
.where(Tables.PROJECT.ID.equal(idProject))
577582
.fetchOne();
578583

584+
// Get a timestamp used to update the modified field of the project record
579585
GregorianCalendar cal = new GregorianCalendar();
580586
cal.setTime(new java.util.Date());
581587

582588
if (record != null) {
589+
// Found the project. Verify that the current user owns it
583590
Long idUser = BlocklyPropSecurityUtils.getCurrentUserId();
591+
592+
// TODO: Detecting a zero user id
593+
if (idUser == 0) {
594+
LOG.error("Detected current user ID is zero for project {}", idProject);
595+
return null;
596+
}
597+
598+
if (record.getIdUser() == 0) {
599+
LOG.error("Detected project user ID is zero for project {}", idProject);
600+
return null;
601+
}
602+
603+
// Update the project if the current user owns it
584604
if (record.getIdUser().equals(idUser)) {
585605
record.setCode(code);
586606
record.setModified(cal);
587607
record.setCodeBlockVersion(BLOCKLY_LIBRARY_VERSION);
588608
record.update();
589609
return record;
590610
} else {
611+
// If the project is a shared project, allow the current user
612+
// to clone the project into their library
591613
if (record.getShared()) {
592614
ProjectRecord cloned = doProjectClone(record);
593615
cloned.setCode(code);
594616
cloned.setModified(cal);
595617
cloned.setCodeBlockVersion(BLOCKLY_LIBRARY_VERSION);
618+
cloned.setIdUser(idUser); // The logged in user owns this copy of the project
596619
cloned.update();
597620
return cloned;
598621
}
622+
599623
LOG.error("User {} tried and failed to update project {}.", idUser, idProject);
600624
throw new UnauthorizedException();
601625
}
@@ -605,6 +629,8 @@ public ProjectRecord updateProjectCode(Long idProject, String code) {
605629
}
606630
}
607631

632+
633+
608634
/**
609635
* Save the current project as a new project
610636
*
@@ -700,7 +726,7 @@ private ProjectRecord doProjectClone(ProjectRecord original) {
700726
original.getBoard(),
701727
original.getPrivate(),
702728
original.getShared(),
703-
original.getId()
729+
original.getId() // set the parent project id
704730
);
705731

706732
// cloned.setBasedOn(original.getId());

src/main/java/com/parallax/server/blocklyprop/rest/RestProject.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class RestProject {
5454
// Connector to project converter object
5555
private ProjectConverter projectConverter;
5656

57+
5758
/**
5859
* Connect to the project service object
5960
* @param projectService
@@ -63,6 +64,7 @@ public void setProjectService(ProjectService projectService) {
6364
this.projectService = projectService;
6465
}
6566

67+
6668
/**
6769
* Connect to the project converter object
6870
* @param projectConverter
@@ -72,6 +74,7 @@ public void setProjectConverter(ProjectConverter projectConverter) {
7274
this.projectConverter = projectConverter;
7375
}
7476

77+
7578
/**
7679
* Return a list of projects owned by the currently authenticated user.
7780
*
@@ -124,10 +127,13 @@ public Response get(
124127

125128
JsonObject result = new JsonObject();
126129
JsonArray jsonProjects = new JsonArray();
130+
131+
// Loop through user projects and build a Json array
127132
for (ProjectRecord project : userProjects) {
128133
jsonProjects.add(projectConverter.toListJson(project));
129134
}
130135

136+
// Add payload details
131137
result.add("rows", jsonProjects);
132138
result.addProperty("total", projectCount);
133139

@@ -139,9 +145,15 @@ public Response get(
139145
LOG.warn("Error is {}", ex.getMessage());
140146
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
141147
}
142-
143148
}
144149

150+
151+
/**
152+
* Retreive a project based on the supplied project ID
153+
*
154+
* @param idProject
155+
* @return
156+
*/
145157
@GET
146158
@Path("/get/{id}")
147159
@Detail("Get project by id")
@@ -177,6 +189,7 @@ public Response get(@PathParam("id") @ParameterDetail("Project identifier") Long
177189
}
178190
}
179191

192+
180193
/**
181194
* Update the code in an existing project.
182195
*
@@ -202,11 +215,13 @@ public Response saveProjectCode(
202215
LOG.debug("Code for project {} has been saved", idProject);
203216

204217
JsonObject result = projectConverter.toJson(savedProject,false);
218+
205219
LOG.debug("Returning JSON: {}", result);
206220

207221
result.addProperty("success", true);
208222

209223
return Response.ok(result.toString()).build();
224+
210225
} catch (AuthorizationException ae) {
211226
LOG.warn("Project code not saved. Not Authorized");
212227
return Response.status(Response.Status.UNAUTHORIZED).build();
@@ -217,6 +232,15 @@ public Response saveProjectCode(
217232
}
218233
}
219234

235+
236+
/**
237+
*
238+
* @param idProject
239+
* @param code
240+
* @param newName
241+
* @param newBoard
242+
* @return
243+
*/
220244
@POST
221245
@Path("/code-as")
222246
@Detail("Save project code")

src/main/java/com/parallax/server/blocklyprop/services/impl/ProjectServiceImpl.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,28 @@ public boolean deleteProject(Long idProject) {
252252
return projectDao.deleteProject(idProject);
253253
}
254254

255+
256+
/**
257+
* Update the code block in the specified project
258+
*
259+
* @param idProject
260+
* @param code
261+
* @return
262+
*/
255263
@Override
256264
public ProjectRecord saveProjectCode(Long idProject, String code) {
257265
return projectDao.updateProjectCode(idProject, code);
258266
}
259267

268+
269+
/**
270+
*
271+
* @param idProject
272+
* @param code
273+
* @param newName
274+
* @param newBoard
275+
* @return
276+
*/
260277
@Override
261278
public ProjectRecord saveProjectCodeAs(Long idProject, String code, String newName, String newBoard) {
262279
return projectDao.saveProjectCodeAs(idProject, code, newName, newBoard);

0 commit comments

Comments
 (0)