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 9935078fe1..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 @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Volo.Abp.Account.Settings; using Volo.Abp.Application.Services; @@ -9,11 +10,14 @@ namespace Volo.Abp.Account { public class AccountAppService : ApplicationService, IAccountAppService { + private readonly IIdentityRoleRepository _roleRepository; protected IdentityUserManager UserManager { get; } public AccountAppService( - IdentityUserManager userManager) + IdentityUserManager userManager, + IIdentityRoleRepository roleRepository) { + _roleRepository = roleRepository; UserManager = userManager; } @@ -25,9 +29,20 @@ 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); } + protected virtual 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)) 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 40b819e0e1..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 @@ -1,8 +1,10 @@ using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Account.Settings; +using Volo.Abp.Application.Dtos; using Volo.Abp.Identity; using Volo.Abp.Settings; using Volo.Abp.Uow; @@ -12,6 +14,8 @@ namespace Volo.Abp.Account.Web.Pages.Account { public class RegisterModel : AccountPageModel { + private readonly IAccountAppService _accountAppService; + [BindProperty(SupportsGet = true)] public string ReturnUrl { get; set; } @@ -21,7 +25,12 @@ namespace Volo.Abp.Account.Web.Pages.Account [BindProperty] public PostInput Input { get; set; } - public virtual async Task OnGet() + public RegisterModel(IAccountAppService accountAppService) + { + _accountAppService = accountAppService; + } + + public virtual async Task OnGetAsync() { await CheckSelfRegistrationAsync().ConfigureAwait(false); } @@ -33,11 +42,16 @@ namespace Volo.Abp.Account.Web.Pages.Account await CheckSelfRegistrationAsync().ConfigureAwait(false); - var user = new IdentityUser(GuidGenerator.Create(), Input.UserName, Input.EmailAddress, CurrentTenant.Id); + var registerDto = new RegisterDto + { + AppName = "MVC", + EmailAddress = Input.EmailAddress, + Password = Input.Password, + UserName = Input.UserName + }; - (await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors(); - - await UserManager.SetEmailAsync(user, Input.EmailAddress).ConfigureAwait(false); + var userDto = await _accountAppService.RegisterAsync(registerDto); + var user = await UserManager.GetByIdAsync(userDto.Id); await SignInManager.SignInAsync(user, isPersistent: false).ConfigureAwait(false); diff --git a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs index 6ce3ce3c0c..3a344b3140 100644 --- a/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IIdentityRoleRepository.cs @@ -21,5 +21,10 @@ namespace Volo.Abp.Identity bool includeDetails = false, CancellationToken cancellationToken = default ); + + Task> GetDefaultOnesAsync( + bool includeDetails = false, + CancellationToken cancellationToken = default + ); } } \ No newline at end of file diff --git a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs index 731cc6df23..60fec622f2 100644 --- a/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs @@ -42,6 +42,12 @@ namespace Volo.Abp.Identity.EntityFrameworkCore .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); } + public virtual async Task> GetDefaultOnesAsync( + bool includeDetails = false, CancellationToken cancellationToken = default) + { + return await DbSet.IncludeDetails(includeDetails).Where(r => r.IsDefault).ToListAsync(GetCancellationToken(cancellationToken)); + } + public override IQueryable WithDetails() { return GetQueryable().IncludeDetails(); diff --git a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs index 4bae372c8e..af5d288fa1 100644 --- a/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs +++ b/modules/identity/src/Volo.Abp.Identity.MongoDB/Volo/Abp/Identity/MongoDB/MongoIdentityRoleRepository.cs @@ -43,5 +43,11 @@ namespace Volo.Abp.Identity.MongoDB .PageBy>(skipCount, maxResultCount) .ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false); } + + public virtual async Task> GetDefaultOnesAsync( + bool includeDetails = false, CancellationToken cancellationToken = default) + { + return await GetMongoQueryable().Where(r => r.IsDefault).ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); + } } } \ No newline at end of file diff --git a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs index cd7ee1eb32..9f427d3f04 100644 --- a/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs +++ b/modules/identity/test/Volo.Abp.Identity.TestBase/Volo/Abp/Identity/IdentityRoleRepository_Tests.cs @@ -36,6 +36,17 @@ namespace Volo.Abp.Identity roles.ShouldContain(r => r.Name == "supporter"); } + [Fact] + public async Task GetDefaultOnesAsync() + { + var roles = await RoleRepository.GetDefaultOnesAsync().ConfigureAwait(false); + + foreach (var role in roles) + { + role.IsDefault.ShouldBe(true); + } + } + [Fact] public async Task GetCountAsync() {