Skip to content

Commit 5e51db2

Browse files
Kasinhoukosarkokuchtiak-ufal
authored
UFAL/Issue 1349: admin user is not allowed to delete himself/herself (ufal#1350) (#1306) (#1397)
* 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 3d8fd67 commit 5e51db2

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
@@ -371,6 +371,14 @@ public void delete(Context context, EPerson ePerson, boolean cascade)
371371
throw new AuthorizeException(
372372
"You must be an admin to delete an EPerson");
373373
}
374+
// Admin cannot delete himself/herself
375+
if (!context.ignoreAuthorization()) {
376+
EPerson currentUser = context.getCurrentUser();
377+
if (currentUser != null && ePerson.getID().equals(currentUser.getID())) {
378+
throw new IllegalStateException(
379+
"You, as admin user, cannot delete yourself");
380+
}
381+
}
374382
// Get all workflow-related groups that the current EPerson belongs to
375383
Set<Group> workFlowGroups = getAllWorkFlowGroups(context, ePerson);
376384
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
@@ -375,7 +375,7 @@ protected void delete(Context context, UUID id) throws AuthorizeException {
375375
} catch (EmptyWorkflowGroupException e) {
376376
throw new RESTEmptyWorkflowGroupException(e);
377377
} catch (IllegalStateException e) {
378-
throw new UnprocessableEntityException(e.getMessage(), e);
378+
throw new DSpaceBadRequestException(e.getMessage(), e);
379379
}
380380
}
381381

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
@@ -1126,6 +1126,20 @@ public void deleteForbidden() throws Exception {
11261126
.andExpect(status().isOk());
11271127
}
11281128

1129+
@Test
1130+
public void deleteYourselfForbidden() throws Exception {
1131+
// login as admin
1132+
String adminToken = getAuthToken(admin.getEmail(), password);
1133+
1134+
// Deleting yourself is forbidden
1135+
getClient(adminToken).perform(delete("/api/eperson/epersons/" + admin.getID()))
1136+
.andExpect(status().isBadRequest());
1137+
1138+
// Verify the admin is still here
1139+
getClient(adminToken).perform(get("/api/eperson/epersons/" + admin.getID()))
1140+
.andExpect(status().isOk());
1141+
}
1142+
11291143
@Test
11301144
public void deleteViolatingWorkFlowConstraints() throws Exception {
11311145
// We turn off the authorization system in order to create the structure as defined below

0 commit comments

Comments
 (0)