Skip to content

Delete account *after* delete-account auditlog entry is made, not *before*#3738

Merged
hmpf merged 1 commit intomasterfrom
fix-making-auditlog-for-deletion-of-account
Feb 2, 2026
Merged

Delete account *after* delete-account auditlog entry is made, not *before*#3738
hmpf merged 1 commit intomasterfrom
fix-making-auditlog-for-deletion-of-account

Conversation

@hmpf
Copy link
Copy Markdown
Contributor

@hmpf hmpf commented Jan 27, 2026

Scope and purpose

Fixes #3709

Related: #3729

Contributor Checklist

Every pull request should have this checklist filled out, no matter how small it is.
More information about contributing to NAV can be found in the
Hacker's guide to NAV.

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation
  • Linted/formatted the code with ruff, easiest by using pre-commit
  • Wrote the commit message so that the first line continues the sentence "If applied, this commit will ...", starts with a capital letter, does not end with punctuation and is 50 characters or less long. See https://cbea.ms/git-commit/
  • Based this pull request on the correct upstream branch: For a patch/bugfix affecting the latest stable version, it should be based on that version's branch (<major>.<minor>.x). For a new feature or other additions, it should be based on master.
  • If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done
  • If it's not obvious from a linked issue, described how to interact with NAV in order for a reviewer to observe the effects of this change first-hand (commands, URLs, UI interactions)
  • If this results in changes in the UI: Added screenshots of the before and after
  • If this adds a new Python source code file: Added the boilerplate header to that file

@hmpf hmpf requested a review from a team January 27, 2026 11:31
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jan 27, 2026

Test results

    27 files      27 suites   45m 1s ⏱️
 2 778 tests  2 778 ✅ 0 💤 0 ❌
20 614 runs  20 614 ✅ 0 💤 0 ❌

Results for commit a0d5e76.

♻️ This comment has been updated with latest results.

@codecov
Copy link
Copy Markdown

codecov bot commented Jan 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 63.04%. Comparing base (9b93ea7) to head (a0d5e76).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3738      +/-   ##
==========================================
- Coverage   63.05%   63.04%   -0.02%     
==========================================
  Files         614      614              
  Lines       45450    45452       +2     
  Branches       43       43              
==========================================
- Hits        28660    28655       -5     
- Misses      16780    16787       +7     
  Partials       10       10              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hmpf hmpf self-assigned this Jan 27, 2026
@hmpf hmpf added the auditlog label Jan 27, 2026
# Delete cache entry of how many accounts have password issues
cache.delete(PASSWORD_ISSUES_CACHE_KEY)
return HttpResponseRedirect(reverse('useradmin-account_list'))
with transaction.atomic():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the transaction fails?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's now a guarantee that the log entry is only made iff the user is deleted. Before, you could have it half way: deleting the user and not logging it. With the new order of operations, without the atomic you can log the deletion then not delete the user.

The transaction can fail if the user was already deleted by a different process/logged in users/API call while you attempted to delete via the request.

If we had used a generic view we would have gotten a 404 exception on the attempt if the user was gone as we entered the request, but if it happened during the request I guess we'd get an (uncaught) database error.

Now, we fall off the with and re-render the delete widget/page. It might be that a redirect to the same page after the with would be better, triggering an error message that the account is gone if it is and redirecting to the list page.

@Simrayz Simrayz requested a review from a team January 28, 2026 09:06
Copy link
Copy Markdown
Contributor

@Simrayz Simrayz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution 😄

(Approved with comment, because changing the indent of the line should not require a re-review)

messages.success(request, 'Account %s has been deleted.' % (account.name))
# Delete cache entry of how many accounts have password issues
cache.delete(PASSWORD_ISSUES_CACHE_KEY)
return HttpResponseRedirect(reverse('useradmin-account_list'))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return should probably be outside the transaction block?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's there on purpose. I'll merge this as is, there are ways to make it even more robust but that can wait.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine by me 🫡

@hmpf hmpf force-pushed the fix-making-auditlog-for-deletion-of-account branch from 9700d56 to a0d5e76 Compare January 30, 2026 11:17
@sonarqubecloud
Copy link
Copy Markdown

@hmpf hmpf merged commit cdbe56c into master Feb 2, 2026
20 checks passed
@hmpf hmpf deleted the fix-making-auditlog-for-deletion-of-account branch February 2, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should we fix account_delete so that deleted accounts behave properly in the auditlog?

2 participants