From f727696e21ca31d8c60d5c53be53a4a803ab2b56 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 4 Feb 2020 12:53:30 +0300 Subject: [PATCH] registration refactor --- .../Volo/Abp/Account/AccountAppService.cs | 2 ++ .../Pages/Account/Register.cshtml.cs | 28 ++++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs index ab703dd223..639e26b7ef 100644 --- a/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs +++ b/modules/account/src/Volo.Abp.Account.Application/Volo/Abp/Account/AccountAppService.cs @@ -29,6 +29,8 @@ namespace Volo.Abp.Account (await UserManager.CreateAsync(user, input.Password).ConfigureAwait(false)).CheckErrors(); + await UserManager.SetEmailAsync(user,input.EmailAddress).ConfigureAwait(false); + await SetDefaultRolesAsync(user); return ObjectMapper.Map(user); diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs index e521db5f9d..a690e9dc35 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs @@ -14,7 +14,7 @@ namespace Volo.Abp.Account.Web.Pages.Account { public class RegisterModel : AccountPageModel { - private readonly IIdentityRoleRepository _roleRepository; + private readonly IAccountAppService _accountAppService; [BindProperty(SupportsGet = true)] public string ReturnUrl { get; set; } @@ -25,9 +25,9 @@ namespace Volo.Abp.Account.Web.Pages.Account [BindProperty] public PostInput Input { get; set; } - public RegisterModel(IIdentityRoleRepository roleRepository) + public RegisterModel(IAccountAppService accountAppService) { - _roleRepository = roleRepository; + _accountAppService = accountAppService; } public virtual async Task OnGetAsync() @@ -42,26 +42,22 @@ namespace Volo.Abp.Account.Web.Pages.Account await CheckSelfRegistrationAsync().ConfigureAwait(false); - var user = new IdentityUser(GuidGenerator.Create(), Input.UserName, Input.EmailAddress, CurrentTenant.Id); - - (await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors(); - - await UserManager.SetEmailAsync(user, Input.EmailAddress).ConfigureAwait(false); + var registerDto = new RegisterDto + { + AppName = "MVC", + EmailAddress = Input.EmailAddress, + Password = Input.Password, + UserName = Input.UserName + }; - await SetDefaultRolesAsync(user); + var userDto = await _accountAppService.RegisterAsync(registerDto); + var user = await UserManager.GetByIdAsync(userDto.Id); await SignInManager.SignInAsync(user, isPersistent: false).ConfigureAwait(false); return Redirect(ReturnUrl ?? "/"); //TODO: How to ensure safety? IdentityServer requires it however it should be checked somehow! } - protected async Task SetDefaultRolesAsync(IdentityUser user) - { - var defaultRoles = await _roleRepository.GetDefaultOnesAsync().ConfigureAwait(false); - - await UserManager.SetRolesAsync(user, defaultRoles.Select(r => r.Name)).ConfigureAwait(false); - } - protected virtual async Task CheckSelfRegistrationAsync() { if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled).ConfigureAwait(false) ||