sozsoft-platform/api/src/Sozsoft.Platform.Application/Identity/PlatformAccountAppService.cs

209 lines
7.5 KiB
C#
Raw Normal View History

2026-02-24 20:44:16 +00:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Sozsoft.Sender.Mail;
using Sozsoft.Platform.Data.Seeds;
using Sozsoft.Platform.Extensions;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Volo.Abp.Account;
using Volo.Abp.Account.Emailing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Identity;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Settings;
using Volo.Abp.UI.Navigation.Urls;
using IdentityUser = Volo.Abp.Identity.IdentityUser;
namespace Sozsoft.Platform.Identity;
[Dependency(ReplaceServices = true)]
public class PlatformAccountAppService : AccountAppService, IAccountAppService
{
private readonly ISettingProvider settingProvider;
private readonly ISozsoftEmailSender emailSender;
private readonly IAppUrlProvider appUrlProvider;
public PlatformAccountAppService(
IdentityUserManager userManager,
IIdentityRoleRepository roleRepository,
IAccountEmailer accountEmailer,
IdentitySecurityLogManager identitySecurityLogManager,
IOptions<IdentityOptions> identityOptions,
ISettingProvider settingProvider,
ISozsoftEmailSender emailSender,
IAppUrlProvider appUrlProvider
) : base(userManager, roleRepository, accountEmailer, identitySecurityLogManager, identityOptions)
{
this.settingProvider = settingProvider;
this.emailSender = emailSender;
this.appUrlProvider = appUrlProvider;
}
[Captcha]
public override async Task<IdentityUserDto> RegisterAsync(RegisterDto input)
{
await CheckSelfRegistrationAsync();
await IdentityOptions.SetAsync();
var user = new IdentityUser(GuidGenerator.Create(), input.UserName, input.EmailAddress, CurrentTenant.Id)
{
Name = input.ExtraProperties.GetValueOrDefault(PlatformConsts.AbpIdentity.User.Name)?.ToString(),
Surname = input.ExtraProperties.GetValueOrDefault(PlatformConsts.AbpIdentity.User.Surname)?.ToString(),
};
user.SetIsVerified(false);
input.MapExtraPropertiesTo(user);
(await UserManager.CreateAsync(user, input.Password)).CheckErrors();
await UserManager.SetEmailAsync(user, input.EmailAddress);
await UserManager.AddDefaultRolesAsync(user);
var TwoFactorEnabled = await SettingProvider.GetAsync<bool>(PlatformConsts.AbpAccount.TwoFactor.Enabled);
await UserManager.SetTwoFactorEnabledAsync(user, TwoFactorEnabled);
// Send Confirmation Code
await SendConfirmationCodeAsync(user);
//Send mail to admins
try
{
var SenderName = await settingProvider.GetOrNullAsync(SeedConsts.AbpSettings.Mailing.Default.DefaultFromDisplayName);
var SenderEmailAddress = await settingProvider.GetOrNullAsync(SeedConsts.AbpSettings.Mailing.Default.DefaultFromAddress);
var url = await appUrlProvider.GetUrlAsync("MVC", PlatformConsts.Urls.UserDetail);
var userDetailUrl = $"{url}/{user.Id}";
var content = $@"My name is: {user.GetFullName()}.
Email Address: {user.Email}
User Detail: {userDetailUrl}";
var recipients = await SettingProvider.GetOrNullAsync(PlatformConsts.AbpSettings.SiteManagement.General.NewMemberNotificationEmails);
if (!recipients.IsNullOrWhiteSpace())
{
await emailSender.QueueEmailAsync(
SenderEmailAddress,
new KeyValuePair<string, string>(SenderName, SenderEmailAddress),
new
{
user.Email,
Name = user.GetFullName()
},
content,
subject: PlatformConsts.AppName + " : New User");
}
}
catch
{ }
return ObjectMapper.Map<IdentityUser, IdentityUserDto>(user);
}
[Captcha]
public override Task SendPasswordResetCodeAsync(SendPasswordResetCodeDto input)
{
return base.SendPasswordResetCodeAsync(input);
}
[Captcha]
public async Task SendExtendLoginRequestAsync(SendExtendLoginRequestInputDto input)
{
var user = await UserManager.FindByEmailAsync(input.EmailAddress);
if (user == null)
{
return;
}
var userDetailUrl = await appUrlProvider.GetUrlAsync(PlatformConsts.React, PlatformConsts.Urls.UserDetail);
var content = $@"My name is: {user.GetFullName()}.
User Detail: {string.Format(userDetailUrl, user.Id)}";
var recipient = await settingProvider.GetOrNullAsync(PlatformConsts.AbpSettings.SiteManagement.General.TimedLoginEmails);
if (!recipient.IsNullOrWhiteSpace())
{
await emailSender.QueueEmailAsync(
recipient,
null,
null,
content,
subject: PlatformConsts.AppName + " : Extend Login Request");
}
return;
}
[Captcha]
public async Task SendAccountConfirmationCodeAsync(SendAccountConfirmationCodeInputDto input)
{
var user = await UserManager.FindByEmailAsync(input.EmailAddress);
if (user != null)
await SendConfirmationCodeAsync(user);
}
public async Task<bool> VerifyAccountConfirmationCodeAsync(VerifyAccountConfirmationCodeInputDto input)
{
await IdentityOptions.SetAsync();
var user = await UserManager.FindByIdAsync(input.UserId.ToString());
if (user == null)
{
return false;
}
try
{
input.Token = Encoding.UTF8.GetString(Base64UrlEncoder.DecodeBytes(input.Token));
}
catch
{
return false;
}
var result = await UserManager.ConfirmEmailAsync(user, input.Token);
return result.Succeeded;
}
private async Task<bool> SendConfirmationCodeAsync(IdentityUser user = null)
{
if (user == null && CurrentUser.IsAuthenticated && !CurrentUser.EmailVerified && CurrentUser.Id.HasValue)
user = await UserManager.GetByIdAsync(CurrentUser.Id.Value);
if (user == null || user.EmailConfirmed)
return false;
var confirmationToken = await UserManager.GenerateEmailConfirmationTokenAsync(user);
confirmationToken = Base64UrlEncoder.Encode(Encoding.UTF8.GetBytes(confirmationToken));
//confirmationToken = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(confirmationToken));
var url = await appUrlProvider.GetUrlAsync(PlatformConsts.React, PlatformConsts.Urls.EmailConfirmation);
var callbackUrl = $"{url}/{user.Id}/{confirmationToken}";
var content = $@"Dear {user.GetFullName()}. Your email address has been registered.
To validate your account, please complete your profile by clicking (or copy-pasting) the link below
{callbackUrl}";
var SenderName = await settingProvider.GetOrNullAsync(SeedConsts.AbpSettings.Mailing.Default.DefaultFromDisplayName);
var SenderEmailAddress = await settingProvider.GetOrNullAsync(SeedConsts.AbpSettings.Mailing.Default.DefaultFromAddress);
await emailSender.QueueEmailAsync(
user.Email,
new KeyValuePair<string, string>(SenderName, SenderEmailAddress),
new
{
user.Email,
Name = user.GetFullName(),
Link = callbackUrl
},
content,
subject: PlatformConsts.AppName + " : User Email Verification");
return true;
}
}