Skip to content

Commit ef8b143

Browse files
kosarkokuchtiak-ufal
authored andcommitted
UFAL/Issue 1349: admin user is not allowed to delete himself/herself (ufal#1350) (#1306)
* 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) Co-authored-by: Milan Kuchtiak <kuchtiak@ufal.mff.cuni.cz> (cherry picked from commit 68463d4)
1 parent 8e8782a commit ef8b143

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
@@ -380,6 +380,14 @@ public void delete(Context context, EPerson ePerson, boolean cascade)
380380
throw new AuthorizeException(
381381
"You must be an admin to delete an EPerson");
382382
}
383+
// Admin cannot delete himself/herself
384+
if (!context.ignoreAuthorization()) {
385+
EPerson currentUser = context.getCurrentUser();
386+
if (currentUser != null && ePerson.getID().equals(currentUser.getID())) {
387+
throw new IllegalStateException(
388+
"You, as admin user, cannot delete yourself");
389+
}
390+
}
383391
// Get all workflow-related groups that the current EPerson belongs to
384392
Set<Group> workFlowGroups = getAllWorkFlowGroups(context, ePerson);
385393
for (Group group: workFlowGroups) {

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
@@ -376,7 +376,7 @@ protected void delete(Context context, UUID id) throws AuthorizeException {
376376
} catch (EmptyWorkflowGroupException e) {
377377
throw new RESTEmptyWorkflowGroupException(e);
378378
} catch (IllegalStateException e) {
379-
throw new UnprocessableEntityException(e.getMessage(), e);
379+
throw new DSpaceBadRequestException(e.getMessage(), e);
380380
}
381381
}
382382

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
@@ -1099,6 +1099,20 @@ public void deleteForbidden() throws Exception {
10991099
.andExpect(status().isOk());
11001100
}
11011101

1102+
@Test
1103+
public void deleteYourselfForbidden() throws Exception {
1104+
// login as admin
1105+
String adminToken = getAuthToken(admin.getEmail(), password);
1106+
1107+
// Deleting yourself is forbidden
1108+
getClient(adminToken).perform(delete("/api/eperson/epersons/" + admin.getID()))
1109+
.andExpect(status().isBadRequest());
1110+
1111+
// Verify the admin is still here
1112+
getClient(adminToken).perform(get("/api/eperson/epersons/" + admin.getID()))
1113+
.andExpect(status().isOk());
1114+
}
1115+
11021116
@Test
11031117
public void deleteViolatingWorkFlowConstraints() throws Exception {
11041118
// We turn off the authorization system in order to create the structure as defined below

0 commit comments

Comments
 (0)