Skip to content

Conversation

AndyButland
Copy link
Contributor

@AndyButland AndyButland commented Aug 15, 2025

Prerequisites

  • I have added steps to test this contribution in the description below

Fixes #19917

Description

This PR resolves the issue highlighted and diagnosed to be due to the user group repository cache holding onto counts of the number of users in the group. This gets out of date as groups are added and removed from users. See: #19917 (comment)

To resolve I've added steps that invalidates the cache for each affected user group when added to or removed from a user, or if the user is deleted.

In passing I've also implemented a TODO around optimising the update of user groups for a user, so we don't just remove and add but make only the updates necessary.

Integration tests have been added to verify both the refactoring and the fixed behaviour.

Testing

See the linked issue.

Also verify that groups can be added to and removed from user as expected.

…han deleting and re-adding.

Ensure user groups affected by the update are invalidated in the repository cache.
@Copilot Copilot AI review requested due to automatic review settings August 15, 2025 12:57
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where checks for preventing removal of the final admin user were not working correctly due to cached user group counts becoming stale. The fix invalidates user group repository caches when users are added, removed, or deleted, and optimizes user group updates to only perform necessary changes rather than removing and re-adding all groups.

  • Invalidates user group caches when user-group associations change to ensure accurate user counts
  • Optimizes user group updates to only add/remove changed groups instead of rebuilding all associations
  • Adds comprehensive integration tests to verify the fix and refactored behavior

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/Umbraco.Infrastructure/Persistence/Repositories/Implement/UserRepository.cs Implements cache invalidation and optimized user group updates with selective add/remove operations
tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/UserServiceTests.cs Adds test to verify user group assignment and removal functionality
tests/Umbraco.Tests.Integration/Umbraco.Core/Services/UserServiceCrudTests.Update.cs Adds test to verify admin user group removal restrictions work correctly

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

v16 Unable to remove Admin user group from newly created user
1 participant