Skip to content

Commit

Permalink
Support member saving Cancel (#16147)
Browse files Browse the repository at this point in the history
* Fixes #15152

Handle when a notification cancels the member saving operation.

* Fixed unit tests
  • Loading branch information
bergmania authored Apr 25, 2024
1 parent a8021c4 commit 8975515
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ protected IActionResult MemberEditingOperationStatusResult(MemberEditingOperatio
.WithTitle("Duplicate email detected")
.WithDetail("The supplied email is already in use by another member.")
.Build()),
MemberEditingOperationStatus.CancelledByNotificationHandler => BadRequest(problemDetailsBuilder
.WithTitle("Cancelled")
.Build()),
MemberEditingOperationStatus.Unknown => StatusCode(
StatusCodes.Status500InternalServerError,
problemDetailsBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public enum MemberEditingOperationStatus
InvalidEmail,
DuplicateUsername,
DuplicateEmail,
CancelledByNotificationHandler,
Unknown,
}
13 changes: 12 additions & 1 deletion src/Umbraco.Infrastructure/Security/MemberUserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Umbraco.Cms.Core.Security;
public class MemberUserStore : UmbracoUserStore<MemberIdentityUser, UmbracoIdentityRole>, IMemberUserStore
{
private const string GenericIdentityErrorCode = "IdentityErrorUserStore";
public const string CancelledIdentityErrorCode = "CancelledIdentityErrorUserStore";
private readonly IExternalLoginWithKeyService _externalLoginService;
private readonly IUmbracoMapper _mapper;
private readonly IMemberService _memberService;
Expand Down Expand Up @@ -127,7 +128,17 @@ public override Task<IdentityResult> CreateAsync(
UpdateMemberProperties(memberEntity, user, out bool _);

// create the member
_memberService.Save(memberEntity);
Attempt<OperationResult?> saveAttempt = _memberService.Save(memberEntity);
if (saveAttempt.Success is false)
{
scope.Complete();
return Task.FromResult(IdentityResult.Failed(
new IdentityError {
Code = CancelledIdentityErrorCode,
Description = string.Empty

}));
}

// We need to add roles now that the member has an Id. It do not work implicit in UpdateMemberProperties
_memberService.AssignRoles(
Expand Down
3 changes: 3 additions & 0 deletions src/Umbraco.Infrastructure/Services/MemberEditingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ private static Attempt<MemberCreateResult, MemberEditingStatus> IdentityMemberCr
case nameof(IdentityErrorDescriber.DuplicateEmail):
createStatus = MemberEditingOperationStatus.DuplicateEmail;
break;
case MemberUserStore.CancelledIdentityErrorCode:
createStatus = MemberEditingOperationStatus.CancelledByNotificationHandler;
break;
}

if (createStatus is not MemberEditingOperationStatus.Unknown)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Net;
Expand Down Expand Up @@ -141,7 +142,7 @@ public async Task GivenICreateANewUser_AndTheUserIsPopulatedCorrectly_ThenIShoul

// assert
Assert.IsTrue(identityResult.Succeeded);
Assert.IsTrue(!identityResult.Errors.Any());
Assert.IsFalse(identityResult.Errors.Any());
}

[Test]
Expand Down Expand Up @@ -266,6 +267,9 @@ private void MockMemberServiceForCreateMember(IMember fakeMember)
_mockMemberService
.Setup(x => x.CreateMember(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(fakeMember);
_mockMemberService.Setup(x => x.Save(fakeMember, Constants.Security.SuperUserId));
_mockMemberService
.Setup(x => x.Save(fakeMember, Constants.Security.SuperUserId))
.Returns(Attempt.Succeed<OperationResult?>(null));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ public async Task GivenICreateANewUser_AndTheUserIsPopulatedCorrectly_ThenIShoul
_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, Constants.Security.SuperUserId));

_mockMemberService
.Setup(x => x.Save(mockMember, Constants.Security.SuperUserId))
.Returns(Attempt.Succeed<OperationResult?>(null));
// act
var identityResult = await sut.CreateAsync(fakeUser, CancellationToken.None);

Expand Down

0 comments on commit 8975515

Please sign in to comment.