Skip to content

Commit f12d77c

Browse files
Kasinhoukosarkokuchtiak-ufal
authored
UFAL/Issue 1349: admin user is not allowed to delete himself/herself (ufal#1350) (#1306) (#1396)
* Issue 1349: admin user is not allowed to delete himself/herself * improve the fix: test context.getCurrentUser() for null * throw IllegalStateException rather than AuthorizeException, and allow client to see the error message (cherry picked from commit b913627) (cherry picked from commit 68463d4) Co-authored-by: Ondřej Košarko <ko_ok@centrum.cz> Co-authored-by: Milan Kuchtiak <kuchtiak@ufal.mff.cuni.cz>
1 parent 7035a4c commit f12d77c

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ public void delete(Context context, EPerson ePerson, boolean cascade)
278278
throw new AuthorizeException(
279279
"You must be an admin to delete an EPerson");
280280
}
281+
// Admin cannot delete himself/herself
282+
if (!context.ignoreAuthorization()) {
283+
EPerson currentUser = context.getCurrentUser();
284+
if (currentUser != null && ePerson.getID().equals(currentUser.getID())) {
285+
throw new IllegalStateException(
286+
"You, as admin user, cannot delete yourself");
287+
}
288+
}
281289
Set<Group> workFlowGroups = getAllWorkFlowGroups(context, ePerson);
282290
for (Group group: workFlowGroups) {
283291
List<EPerson> ePeople = groupService.allMembers(context, group);

dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ protected void delete(Context context, UUID id) throws AuthorizeException {
326326
} catch (EmptyWorkflowGroupException e) {
327327
throw new RESTEmptyWorkflowGroupException(e);
328328
} catch (IllegalStateException e) {
329-
throw new UnprocessableEntityException(e.getMessage(), e);
329+
throw new DSpaceBadRequestException(e.getMessage(), e);
330330
}
331331
}
332332

dspace-server-webapp/src/test/java/org/dspace/app/rest/EPersonRestRepositoryIT.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,20 @@ public void deleteForbidden() throws Exception {
842842
.andExpect(status().isOk());
843843
}
844844

845+
@Test
846+
public void deleteYourselfForbidden() throws Exception {
847+
// login as admin
848+
String adminToken = getAuthToken(admin.getEmail(), password);
849+
850+
// Deleting yourself is forbidden
851+
getClient(adminToken).perform(delete("/api/eperson/epersons/" + admin.getID()))
852+
.andExpect(status().isBadRequest());
853+
854+
// Verify the admin is still here
855+
getClient(adminToken).perform(get("/api/eperson/epersons/" + admin.getID()))
856+
.andExpect(status().isOk());
857+
}
858+
845859
@Test
846860
public void deleteViolatingWorkFlowConstraints() throws Exception {
847861
// We turn off the authorization system in order to create the structure as defined below

0 commit comments

Comments
 (0)