Skip to content

Clear member cache by older user name when member user name is updated (13)#19672

Merged
nikolajlauridsen merged 2 commits intov13/devfrom
v13/bugfix/clear-member-cache-for-previous-username
Jul 7, 2025
Merged

Clear member cache by older user name when member user name is updated (13)#19672
nikolajlauridsen merged 2 commits intov13/devfrom
v13/bugfix/clear-member-cache-for-previous-username

Conversation

@AndyButland
Copy link
Contributor

@AndyButland AndyButland commented Jul 4, 2025

Prerequisites

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

Resolves #19618 for Umbraco 13

Description

The linked issue discusses the problem where updating a user in the backoffice to change the user name still leaves the member cached and retrievable by the old user name.

This PR uses the AdditionalData collection available on entities to provide the previous user name to the cache refresher, so it can clear by the old user name as well as the new.

I've also added a constant for the AdditionalData key and used it for one other instance.

Testing

With code like the following, retrieve a member by the user name:

@inject IMemberService MemberService;
@{
    var fred = MemberService.GetByUsername("fred@test.com");
    @if (fred is null)
    {
        <div>Fred not found</div>
    }
    else
    {
        <div>Found Fred: @fred.Email</div>
    }
}

Update the member's user name in the backoffice. Before the PR you will see the member is still found, but after these changes are applied, it should not be.

Copilot AI review requested due to automatic review settings July 4, 2025 10:56
Copy link
Contributor

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 ensures that when a member’s username is updated, any cached entries under the old username are also purged by capturing and propagating the previous username.

  • Capture the old username in AdditionalData on member save
  • Extend the cache refresher payload to include PreviousUsername and clear both old and new keys
  • Introduce constants for the additional-data keys and update related handlers and extensions

Reviewed Changes

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

Show a summary per file
File Description
src/Umbraco.Core/Services/MemberService.cs Capture previous username before save and populate AdditionalData
src/Umbraco.Core/Models/MemberGroup.cs Replace literal "previousName" with a constant key
src/Umbraco.Core/Handlers/PublicAccessHandler.cs Use constant key to detect and rename by previous member-group name
src/Umbraco.Core/Constants-Entities.cs Define MemberPreviousUserName and MemberGroupPreviousName constants
src/Umbraco.Core/Cache/Refreshers/Implement/MemberCacheRefresher.cs Add PreviousUsername to payload and clear cache by old name
src/Umbraco.Core/Cache/DistributedCacheExtensions.cs Refactor to build payloads with PreviousUsername
Comments suppressed due to low confidence (1)

src/Umbraco.Core/Cache/DistributedCacheExtensions.cs:175

  • Add unit tests for GetPayloads and the cache refresher to verify that PreviousUsername is correctly populated and that cache entries are cleared for both the old and new usernames.
    private static IEnumerable<MemberCacheRefresher.JsonPayload> GetPayloads(IEnumerable<IMember> members, bool removed)

Copy link
Contributor

@nikolajlauridsen nikolajlauridsen left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@nikolajlauridsen nikolajlauridsen merged commit 13a2cd7 into v13/dev Jul 7, 2025
17 of 19 checks passed
@nikolajlauridsen nikolajlauridsen deleted the v13/bugfix/clear-member-cache-for-previous-username branch July 7, 2025 12:01
@AndyButland AndyButland changed the title Clear member cache by older user name when member user name is updated. Clear member cache by older user name when member user name is updated Jul 7, 2025
@AndyButland AndyButland changed the title Clear member cache by older user name when member user name is updated Clear member cache by older user name when member user name is updated (13) Jul 8, 2025
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.

2 participants