mirror of https://github.com/abpframework/abp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.8 KiB
53 lines
1.8 KiB
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Volo.Abp.Account.Settings;
|
|
using Volo.Abp.Application.Services;
|
|
using Volo.Abp.Identity;
|
|
using Volo.Abp.Settings;
|
|
|
|
namespace Volo.Abp.Account
|
|
{
|
|
public class AccountAppService : ApplicationService, IAccountAppService
|
|
{
|
|
private readonly IIdentityRoleRepository _roleRepository;
|
|
protected IdentityUserManager UserManager { get; }
|
|
|
|
public AccountAppService(
|
|
IdentityUserManager userManager,
|
|
IIdentityRoleRepository roleRepository)
|
|
{
|
|
_roleRepository = roleRepository;
|
|
UserManager = userManager;
|
|
}
|
|
|
|
public virtual async Task<IdentityUserDto> RegisterAsync(RegisterDto input)
|
|
{
|
|
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 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))
|
|
{
|
|
throw new UserFriendlyException(L["SelfRegistrationDisabledMessage"]);
|
|
}
|
|
}
|
|
}
|
|
}
|