Default roles on registration

resolves https://github.com/abpframework/abp/issues/2721
pull/2753/head
Yunus Emre Kalkan 6 years ago
parent 2d51fcd8d4
commit 61d8e68a0a

@ -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,18 @@ namespace Volo.Abp.Account
(await UserManager.CreateAsync(user, input.Password).ConfigureAwait(false)).CheckErrors();
await SetDefaultRolesAsync(user);
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(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))

@ -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 IIdentityRoleRepository _roleRepository;
[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(IIdentityRoleRepository roleRepository)
{
_roleRepository = roleRepository;
}
public virtual async Task OnGetAsync()
{
await CheckSelfRegistrationAsync().ConfigureAwait(false);
}
@ -35,15 +44,24 @@ namespace Volo.Abp.Account.Web.Pages.Account
var user = new IdentityUser(GuidGenerator.Create(), Input.UserName, Input.EmailAddress, CurrentTenant.Id);
(await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors();
(await UserManager.CreateAsync(user, Input.Password).ConfigureAwait(false)).CheckErrors();
await UserManager.SetEmailAsync(user, Input.EmailAddress).ConfigureAwait(false);
await SetDefaultRolesAsync(user);
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) ||

@ -21,5 +21,9 @@ namespace Volo.Abp.Identity
bool includeDetails = false,
CancellationToken cancellationToken = default
);
Task<List<IdentityRole>> GetDefaultOnesAsync(
CancellationToken cancellationToken = default
);
}
}

@ -42,6 +42,11 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public virtual async Task<List<IdentityRole>> GetDefaultOnesAsync(CancellationToken cancellationToken = default)
{
return await DbSet.Where(r => r.IsDefault).ToListAsync(cancellationToken: cancellationToken);
}
public override IQueryable<IdentityRole> WithDetails()
{
return GetQueryable().IncludeDetails();

@ -43,5 +43,10 @@ namespace Volo.Abp.Identity.MongoDB
.PageBy<IdentityRole, IMongoQueryable<IdentityRole>>(skipCount, maxResultCount)
.ToListAsync(GetCancellationToken(cancellationToken)).ConfigureAwait(false);
}
public virtual async Task<List<IdentityRole>> GetDefaultOnesAsync(CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().Where(r => r.IsDefault).ToListAsync(cancellationToken: cancellationToken);
}
}
}

@ -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()
{

Loading…
Cancel
Save