V13: Introduce publishNotifications method on IMembershipMemberService (#18207)

* Introduce publishNotifications method on IMembershipMemberService.cs

* Fix test

* Add PublishNotificationSaveOptions

* Fix up according to comments

* Use numeric values for flag enum

* Update src/Umbraco.Core/Services/MemberService.cs

Co-authored-by: Andy Butland <abutland73@gmail.com>

* Update src/Umbraco.Core/Services/MemberService.cs

Co-authored-by: Andy Butland <abutland73@gmail.com>

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
Nikolaj Geisle
2025-02-12 13:30:41 +01:00
committed by GitHub
parent 9227517a50
commit 8c2b1ebdc5
5 changed files with 57 additions and 9 deletions

View File

@@ -0,0 +1,28 @@
namespace Umbraco.Cms.Core.Models;
/// <summary>
/// Specifies options for publishing notifcations when saving.
/// </summary>
[Flags]
public enum PublishNotificationSaveOptions
{
/// <summary>
/// Do not publish any notifications.
/// </summary>
None = 0,
/// <summary>
/// Only publish the saving notification.
/// </summary>
Saving = 1,
/// <summary>
/// Only publish the saved notification.
/// </summary>
Saved = 2,
/// <summary>
/// Publish all the notifications.
/// </summary>
All = Saving | Saved,
}

View File

@@ -135,6 +135,14 @@ public interface IMembershipMemberService<T> : IService
/// <param name="entity"><see cref="IMember" /> or <see cref="IUser" /> to Save</param>
void Save(T entity);
/// <summary>
/// Saves an <see cref="IMembershipUser" />
/// </summary>
/// <remarks>An <see cref="IMembershipUser" /> can be of type <see cref="IMember" /> or <see cref="IUser" /></remarks>
/// <param name="entity"><see cref="IMember" /> or <see cref="IUser" /> to Save</param>
/// <param name="publishNotificationSaveOptions"> Enum for deciding which notifications to publish.</param>
void Save(T entity, PublishNotificationSaveOptions publishNotificationSaveOptions) => Save(entity);
/// <summary>
/// Saves a list of <see cref="IMembershipUser" /> objects
/// </summary>

View File

@@ -743,7 +743,9 @@ namespace Umbraco.Cms.Core.Services
public void SetLastLogin(string username, DateTime date) => throw new NotImplementedException();
/// <inheritdoc />
public void Save(IMember member)
public void Save(IMember member) => Save(member, PublishNotificationSaveOptions.All);
public void Save(IMember member, PublishNotificationSaveOptions publishNotificationSaveOptions)
{
// trimming username and email to make sure we have no trailing space
member.Username = member.Username.Trim();
@@ -752,11 +754,15 @@ namespace Umbraco.Cms.Core.Services
EventMessages evtMsgs = EventMessagesFactory.Get();
using ICoreScope scope = ScopeProvider.CreateCoreScope();
var savingNotification = new MemberSavingNotification(member, evtMsgs);
if (scope.Notifications.PublishCancelable(savingNotification))
MemberSavingNotification? savingNotification = null;
if (publishNotificationSaveOptions.HasFlag(PublishNotificationSaveOptions.Saving))
{
scope.Complete();
return;
savingNotification = new MemberSavingNotification(member, evtMsgs);
if (scope.Notifications.PublishCancelable(savingNotification))
{
scope.Complete();
return;
}
}
if (string.IsNullOrWhiteSpace(member.Name))
@@ -768,7 +774,13 @@ namespace Umbraco.Cms.Core.Services
_memberRepository.Save(member);
scope.Notifications.Publish(new MemberSavedNotification(member, evtMsgs).WithStateFrom(savingNotification));
if (publishNotificationSaveOptions.HasFlag(PublishNotificationSaveOptions.Saved))
{
scope.Notifications.Publish(
savingNotification is null
? new MemberSavedNotification(member, evtMsgs)
: new MemberSavedNotification(member, evtMsgs).WithStateFrom(savingNotification));
}
Audit(AuditType.Save, 0, member.Id);

View File

@@ -110,7 +110,7 @@ public class MemberUserStore : UmbracoUserStore<MemberIdentityUser, UmbracoIdent
UpdateMemberProperties(memberEntity, user, out bool _);
// create the member
_memberService.Save(memberEntity);
_memberService.Save(memberEntity, PublishNotificationSaveOptions.Saving);
// We need to add roles now that the member has an Id. It do not work implicit in UpdateMemberProperties
_memberService.AssignRoles(

View File

@@ -122,7 +122,7 @@ public class MemberUserStoreTests
_mockMemberService
.Setup(x => x.CreateMember(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(mockMember);
_mockMemberService.Setup(x => x.Save(mockMember));
_mockMemberService.Setup(x => x.Save(mockMember, PublishNotificationSaveOptions.Saving));
// act
var identityResult = await sut.CreateAsync(fakeUser, CancellationToken.None);
@@ -132,7 +132,7 @@ public class MemberUserStoreTests
Assert.IsTrue(!identityResult.Errors.Any());
_mockMemberService.Verify(x =>
x.CreateMember(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()));
_mockMemberService.Verify(x => x.Save(mockMember));
_mockMemberService.Verify(x => x.Save(mockMember, PublishNotificationSaveOptions.Saving));
}
[Test]