JCU/Prevent an admin from deleting their own account - #1402
Merged
Conversation
…fal#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)
There was a problem hiding this comment.
Pull request overview
Prevents an administrator from deleting the currently-authenticated EPerson via the REST API, protecting the platform from accidental “self-delete” lockouts while still allowing deletion when authorization is intentionally bypassed (e.g., CLI/consumers).
Changes:
- Add a guard in
EPersonServiceImpl.delete()to reject deleting the current context user (when authorization is enforced). - Map that refusal to an HTTP 400 in
EPersonRestRepository(instead of 422). - Add an integration test covering “admin tries to delete self” and ensuring the account remains accessible afterward.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| dspace-api/src/main/java/org/dspace/eperson/EPersonServiceImpl.java | Adds server-side protection against deleting the current context user. |
| dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java | Translates the service-layer refusal into an HTTP 400 response. |
| dspace-server-webapp/src/test/java/org/dspace/app/rest/EPersonRestRepositoryIT.java | Adds an integration test asserting self-delete is rejected and the account still exists. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #1306 (
68463d490f) fromdtq-devontocustomer/jcu.EPersonServiceImpl.delete()now refuses to delete the EPerson that is the current context user (unless authorization is being ignored, e.g. CLI/consumers), andEPersonRestRepositorymaps thatIllegalStateExceptionto a 400 Bad Request carrying the message, so the UI can show a friendly notification instead of a generic failure.Verified locally:
mvn -o checkstyle:check -pl dspace-api,dspace-server-webappclean;mvn -o compile -pl dspace-apiclean.Refs dataquest-dev/dspace-customers#855
Frontend counterpart: dataquest-dev/dspace-angular#1447 — the two must ship together.
How this was verified
Automated — runs on this PR
The change ships with an integration test that drives the real REST API:
EPersonRestRepositoryIT#deleteYourselfForbiddenlogs in as an administrator, callsDELETE /api/eperson/epersons/{own-id}, asserts 400, then asserts the account still exists.The Run Integration Tests CI job is green on this PR, and its log shows the class executing:
The base branch has 92
@Testmethods in that class — the 93rd is this one.Locally:
mvn -o checkstyle:check -pl dspace-api,dspace-server-webapp→ 0 violations.Manual — on a live instance
Throwaway DSpace 9.3 stack (own DB / Solr / assetstore, REST on :8091), logged in as an administrator:
DELETE /server/api/eperson/epersons/<own uuid>204, and a follow-upGET→404. The admin deleted their own account.400 Bad Request, follow-upGET→200. The account survives.This branch is the one exercised live above.
Reviewer note — the message does not reach the client
The source commit intended to "allow client to see the error message", but the 400 body is DSpace's generic
{"status":400,"error":"Bad Request","message":"An exception has occurred"}— the text"You, as admin user, cannot delete yourself" never reaches the browser, because
DSpaceBadRequestExceptionis annotated@ResponseStatus(reason = "Bad Request").This does not weaken the fix: the deletion is refused server-side either way, and the frontend does not
depend on that string (it decides from its own identity check). Flagging it so nobody later builds on the
assumption that the message is visible.