mirror of https://github.com/abpframework/abp.git
committed by
GitHub
32 changed files with 440 additions and 12 deletions
@ -0,0 +1,16 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Volo.Abp.Application.Services; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public interface IProfileAppService : IApplicationService |
|||
{ |
|||
Task<ProfileDto> GetAsync(); |
|||
|
|||
Task<ProfileDto> UpdateAsync(UpdateProfileDto input); |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class ProfileDto |
|||
{ |
|||
public string UserName { get; set; } |
|||
|
|||
public string Email { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public string Surname { get; set; } |
|||
|
|||
public string PhoneNumber { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class UpdateProfileDto |
|||
{ |
|||
[StringLength(IdentityUserConsts.MaxUserNameLength)] |
|||
public string UserName { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxEmailLength)] |
|||
public string Email { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxNameLength)] |
|||
public string Name { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxSurnameLength)] |
|||
public string Surname { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxPhoneNumberLength)] |
|||
public string PhoneNumber { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,44 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Authorization; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
[Authorize] |
|||
public class ProfileAppService : IdentityAppServiceBase, IProfileAppService |
|||
{ |
|||
private readonly IdentityUserManager _userManager; |
|||
|
|||
public ProfileAppService(IdentityUserManager userManager) |
|||
{ |
|||
_userManager = userManager; |
|||
} |
|||
|
|||
public async Task<ProfileDto> GetAsync() |
|||
{ |
|||
return ObjectMapper.Map<IdentityUser, ProfileDto>( |
|||
await _userManager.GetByIdAsync(CurrentUser.GetId()) |
|||
); |
|||
} |
|||
|
|||
public async Task<ProfileDto> UpdateAsync(UpdateProfileDto input) |
|||
{ |
|||
var user = await _userManager.GetByIdAsync(CurrentUser.GetId()); |
|||
|
|||
(await _userManager.SetUserNameAsync(user, input.UserName)).CheckErrors(); |
|||
(await _userManager.SetEmailAsync(user, input.Email)).CheckErrors(); |
|||
(await _userManager.SetPhoneNumberAsync(user, input.PhoneNumber)).CheckErrors(); |
|||
user.Name = input.Name; |
|||
user.Surname = input.Surname; |
|||
|
|||
(await _userManager.UpdateAsync(user)).CheckErrors(); |
|||
await CurrentUnitOfWork.SaveChangesAsync(); |
|||
|
|||
return ObjectMapper.Map<IdentityUser, ProfileDto>(user); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
[RemoteService] |
|||
[Area("identity")] |
|||
[ControllerName("Profile")] |
|||
public class ProfileController : IProfileAppService |
|||
{ |
|||
private readonly IProfileAppService _profileAppService; |
|||
|
|||
public ProfileController(IProfileAppService profileAppService) |
|||
{ |
|||
_profileAppService = profileAppService; |
|||
} |
|||
public Task<ProfileDto> GetAsync() |
|||
{ |
|||
return _profileAppService.GetAsync(); |
|||
} |
|||
|
|||
public Task<ProfileDto> UpdateAsync(UpdateProfileDto input) |
|||
{ |
|||
return _profileAppService.UpdateAsync(input); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
@page |
|||
@using Microsoft.AspNetCore.Mvc.Localization |
|||
@using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Modal |
|||
@using Volo.Abp.Identity.Localization |
|||
@using Volo.Abp.Identity.Web.Pages.Identity.Shared |
|||
@model PersonalSettingsModal |
|||
@inject IHtmlLocalizer<IdentityResource> L |
|||
@{ |
|||
Layout = null; |
|||
} |
|||
|
|||
<abp-dynamic-form abp-model="@Model.PersonalSettingsInfoModel" asp-page="/Identity/Shared/PersonalSettingsModal"> |
|||
<abp-modal> |
|||
<abp-modal-header title="@L["PersonalSettings"].Value"></abp-modal-header> |
|||
<abp-modal-body> |
|||
<abp-form-content /> |
|||
</abp-modal-body> |
|||
<abp-modal-footer buttons="@(AbpModalButtons.Cancel|AbpModalButtons.Save)"></abp-modal-footer> |
|||
</abp-modal> |
|||
</abp-dynamic-form> |
|||
@ -0,0 +1,66 @@ |
|||
using System.ComponentModel.DataAnnotations; |
|||
using System.Security.Authentication; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Microsoft.Extensions.Localization; |
|||
using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; |
|||
using Volo.Abp.Identity.Localization; |
|||
|
|||
namespace Volo.Abp.Identity.Web.Pages.Identity.Shared |
|||
{ |
|||
public class PersonalSettingsModal : AbpPageModel |
|||
{ |
|||
[BindProperty] |
|||
public PersonalSettingsInfoModel PersonalSettingsInfoModel { get; set; } |
|||
|
|||
private readonly IProfileAppService _profileAppService; |
|||
|
|||
public PersonalSettingsModal(IProfileAppService profileAppService) |
|||
{ |
|||
_profileAppService = profileAppService; |
|||
} |
|||
|
|||
public async Task OnGetAsync() |
|||
{ |
|||
var user = await _profileAppService.GetAsync(); |
|||
|
|||
PersonalSettingsInfoModel = ObjectMapper.Map<ProfileDto, PersonalSettingsInfoModel>(user); |
|||
} |
|||
|
|||
public async Task<IActionResult> OnPostAsync() |
|||
{ |
|||
ValidateModel(); |
|||
|
|||
var updateDto = ObjectMapper.Map<PersonalSettingsInfoModel, UpdateProfileDto>(PersonalSettingsInfoModel); |
|||
|
|||
await _profileAppService.UpdateAsync(updateDto); |
|||
|
|||
return NoContent(); |
|||
} |
|||
} |
|||
|
|||
public class PersonalSettingsInfoModel |
|||
{ |
|||
[Required] |
|||
[StringLength(IdentityUserConsts.MaxUserNameLength)] |
|||
[Display(Name = "DisplayName:UserName")] |
|||
public string UserName { get; set; } |
|||
|
|||
[Required] |
|||
[StringLength(IdentityUserConsts.MaxEmailLength)] |
|||
[Display(Name = "DisplayName:Email")] |
|||
public string Email { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxNameLength)] |
|||
[Display(Name = "DisplayName:Name")] |
|||
public string Name { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxSurnameLength)] |
|||
[Display(Name = "DisplayName:Surname")] |
|||
public string Surname { get; set; } |
|||
|
|||
[StringLength(IdentityUserConsts.MaxPhoneNumberLength)] |
|||
[Display(Name = "DisplayName:PhoneNumber")] |
|||
public string PhoneNumber { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
(function ($) { |
|||
|
|||
var l = abp.localization.getResource('AbpIdentity'); |
|||
var _personalSettingsModal = new abp.ModalManager(abp.appPath + 'Identity/Shared/PersonalSettingsModal'); |
|||
|
|||
$(function () { |
|||
|
|||
$("#abp-account-personal-settings").click(function (e) { |
|||
e.preventDefault(); |
|||
_personalSettingsModal.open(); |
|||
}); |
|||
|
|||
_personalSettingsModal.onResult(function () { |
|||
//abp.message.success(l("PersonalSettingsSavedMessage"));
|
|||
}); |
|||
}); |
|||
|
|||
})(jQuery); |
|||
@ -0,0 +1,97 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Identity; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using NSubstitute; |
|||
using Shouldly; |
|||
using Volo.Abp.Users; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Identity |
|||
{ |
|||
public class ProfileAppService_Tests : AbpIdentityApplicationTestBase |
|||
{ |
|||
private readonly IProfileAppService _profileAppService; |
|||
private readonly IIdentityUserRepository _userRepository; |
|||
private readonly SignInManager<IdentityUser> _signInManager; |
|||
private readonly IdentityTestData _testData; |
|||
private ICurrentUser _currentUser; |
|||
|
|||
public ProfileAppService_Tests() |
|||
{ |
|||
_profileAppService = GetRequiredService<IProfileAppService>(); |
|||
_userRepository = GetRequiredService<IIdentityUserRepository>(); |
|||
_signInManager = GetRequiredService<SignInManager<IdentityUser>>(); |
|||
_testData = GetRequiredService<IdentityTestData>(); |
|||
} |
|||
|
|||
protected override void AfterAddApplication(IServiceCollection services) |
|||
{ |
|||
_currentUser = Substitute.For<ICurrentUser>(); |
|||
services.AddSingleton(_currentUser); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAsync() |
|||
{ |
|||
//Arrange
|
|||
_currentUser.Id.Returns(_testData.UserJohnId); |
|||
_currentUser.IsAuthenticated.Returns(true); |
|||
|
|||
//Act
|
|||
var result = await _profileAppService.GetAsync(); |
|||
|
|||
//Assert
|
|||
var johnNash = GetUser("john.nash"); |
|||
|
|||
result.UserName.ShouldBe(johnNash.UserName); |
|||
result.Email.ShouldBe(johnNash.Email); |
|||
result.PhoneNumber.ShouldBe(johnNash.PhoneNumber); |
|||
} |
|||
|
|||
|
|||
[Fact] |
|||
public async Task UpdateAsync() |
|||
{ |
|||
//Arrange
|
|||
|
|||
//Arrange
|
|||
_currentUser.Id.Returns(_testData.UserJohnId); |
|||
_currentUser.IsAuthenticated.Returns(true); |
|||
|
|||
var input = new UpdateProfileDto |
|||
{ |
|||
UserName = CreateRandomString(), |
|||
PhoneNumber = CreateRandomPhoneNumber(), |
|||
Email = CreateRandomEmail(), |
|||
Name = CreateRandomString(), |
|||
Surname = CreateRandomString() |
|||
}; |
|||
|
|||
//Act
|
|||
var result = await _profileAppService.UpdateAsync(input); |
|||
|
|||
//Assert
|
|||
result.UserName.ShouldBe(input.UserName); |
|||
result.Email.ShouldBe(input.Email); |
|||
result.PhoneNumber.ShouldBe(input.PhoneNumber); |
|||
result.Surname.ShouldBe(input.Surname); |
|||
result.Name.ShouldBe(input.Name); |
|||
} |
|||
|
|||
private static string CreateRandomEmail() |
|||
{ |
|||
return CreateRandomString() + "@abp.io"; |
|||
} |
|||
|
|||
private static string CreateRandomString() |
|||
{ |
|||
return Guid.NewGuid().ToString("N").Left(16); |
|||
} |
|||
|
|||
private static string CreateRandomPhoneNumber() |
|||
{ |
|||
return RandomHelper.GetRandom(10000000, 100000000).ToString(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue