diff --git a/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll b/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll index 9b260304f6..629eb6cda8 100644 Binary files a/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll and b/src/AbpDesk/Web_PlugIns/AbpDesk.MongoBlog.dll differ diff --git a/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountControllerBase.cs b/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountControllerBase.cs index 9b720ab123..47ab906343 100644 --- a/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountControllerBase.cs +++ b/src/Volo.Abp.Account.Web/Areas/Account/Controllers/AccountControllerBase.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc; @@ -17,7 +19,7 @@ namespace Volo.Abp.Account.Web.Areas.Account.Controllers { if (!identityResult.Succeeded) { - throw new UserFriendlyException("Operation failed!"); + throw new UserFriendlyException("Operation failed: " + identityResult.Errors.Select(e => $"[{e.Code}] {e.Description}").JoinAsString(", ")); } //identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp diff --git a/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityAppServiceBase.cs b/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityAppServiceBase.cs new file mode 100644 index 0000000000..a6392dfb48 --- /dev/null +++ b/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityAppServiceBase.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.AspNetCore.Identity; +using Volo.Abp.Application.Services; +using Volo.Abp.Ui; + +namespace Volo.Abp.Identity +{ + public abstract class IdentityAppServiceBase : ApplicationService + { + protected void CheckIdentityErrors(IdentityResult identityResult) + { + if (!identityResult.Succeeded) + { + throw new UserFriendlyException("Operation failed: " + identityResult.Errors.Select(e => $"[{e.Code}] {e.Description}").JoinAsString(", ")); + } + + //identityResult.CheckErrors(LocalizationManager); //TODO: Get from old Abp + } + } +} diff --git a/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs b/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs index 97b578832b..7b9425018c 100644 --- a/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs +++ b/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs @@ -2,13 +2,12 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Dtos; -using Volo.Abp.Application.Services; namespace Volo.Abp.Identity { //TODO: Consider a way of passing cancellation token to all async application service methods! - public class IdentityUserAppService : ApplicationService, IIdentityUserAppService + public class IdentityUserAppService : IdentityAppServiceBase, IIdentityUserAppService { private readonly IdentityUserManager _userManager; private readonly IIdentityUserRepository _userRepository; @@ -41,8 +40,9 @@ namespace Volo.Abp.Identity { var user = new IdentityUser(GuidGenerator.Create(), input.UserName); + CheckIdentityErrors(await _userManager.CreateAsync(user, input.Password)); await UpdateUserByInput(user, input); - await _userManager.CreateAsync(user, input.Password); + await CurrentUnitOfWork.SaveChangesAsync(); return ObjectMapper.Map(user); @@ -52,9 +52,9 @@ namespace Volo.Abp.Identity { var user = await _userManager.GetByIdAsync(id); - await _userManager.SetUserNameAsync(user, input.UserName); + CheckIdentityErrors(await _userManager.SetUserNameAsync(user, input.UserName)); await UpdateUserByInput(user, input); - await _userManager.UpdateAsync(user); + CheckIdentityErrors(await _userManager.UpdateAsync(user)); await CurrentUnitOfWork.SaveChangesAsync(); return ObjectMapper.Map(user); @@ -63,7 +63,7 @@ namespace Volo.Abp.Identity public async Task DeleteAsync(Guid id) { var user = await _userManager.GetByIdAsync(id); - await _userManager.DeleteAsync(user); + CheckIdentityErrors(await _userManager.DeleteAsync(user)); } public async Task> GetRolesAsync(Guid id) @@ -77,19 +77,19 @@ namespace Volo.Abp.Identity public async Task UpdateRolesAsync(Guid id, IdentityUserUpdateRolesDto input) { var user = await _userManager.GetByIdAsync(id); - await _userManager.SetRolesAsync(user, input.RoleNames); + CheckIdentityErrors(await _userManager.SetRolesAsync(user, input.RoleNames)); } private async Task UpdateUserByInput(IdentityUser user, IdentityUserCreateOrUpdateDtoBase input) { - await _userManager.SetEmailAsync(user, input.Email); - await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber); - await _userManager.SetTwoFactorEnabledAsync(user, input.TwoFactorEnabled); - await _userManager.SetLockoutEnabledAsync(user, input.LockoutEnabled); + CheckIdentityErrors(await _userManager.SetEmailAsync(user, input.Email)); + CheckIdentityErrors(await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber)); + CheckIdentityErrors(await _userManager.SetTwoFactorEnabledAsync(user, input.TwoFactorEnabled)); + CheckIdentityErrors(await _userManager.SetLockoutEnabledAsync(user, input.LockoutEnabled)); if (input.RoleNames != null) { - await _userManager.SetRolesAsync(user, input.RoleNames); + CheckIdentityErrors(await _userManager.SetRolesAsync(user, input.RoleNames)); } } } diff --git a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs index 92cee54e1b..4bfb31154c 100644 --- a/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs +++ b/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs @@ -47,15 +47,26 @@ namespace Volo.Abp.Identity return user; } - public async Task SetRolesAsync([NotNull] IdentityUser user, [NotNull] IEnumerable roleNames) + public async Task SetRolesAsync([NotNull] IdentityUser user, [NotNull] IEnumerable roleNames) { Check.NotNull(user, nameof(user)); Check.NotNull(roleNames, nameof(roleNames)); var currentRoleNames = await GetRolesAsync(user); - await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); - await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); + var result = await RemoveFromRolesAsync(user, currentRoleNames.Except(roleNames).Distinct()); + if (!result.Succeeded) + { + return result; + } + + result = await AddToRolesAsync(user, roleNames.Except(currentRoleNames).Distinct()); + if (!result.Succeeded) + { + return result; + } + + return IdentityResult.Success; } } } diff --git a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs b/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs index 4025713ae1..7cb07c46b0 100644 --- a/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs +++ b/test/Volo.Abp.Identity.Application.Tests/Volo/Abp/Identity/IdentityUserAppService_Tests.cs @@ -63,7 +63,7 @@ namespace Volo.Abp.Identity Email = CreateRandomEmail(), LockoutEnabled = true, PhoneNumber = CreateRandomPhoneNumber(), - Password = "123qwe", + Password = "123qwE4r*", RoleNames = new[] { "moderator" } };