using Abp.Auditing; using Abp.Configuration; using Abp.Domain.Repositories; using Abp.Runtime.Caching; using Abp.Runtime.Session; using Abp.UI; using Microsoft.AspNetCore.Hosting; using VberAdmin.Authorization.Accounts.Dto; using VberAdmin.Authorization.Users; using VberZero; using VberZero.AppService.Users.Dto; using VberZero.BaseSystem.Users; using VberZero.Folders; using VberZero.Session; using VberZero.Settings; using VberZero.Tools.FileHelpers; using VberZero.Tools.StringModel; namespace VberAdmin.Authorization.Accounts; public class AccountAppService : VberAdminAppServiceBase, IAccountAppService { // from: http://regexlib.com/REDetails.aspx?regexp_id=1923 public const string PasswordRegex = "(?=^.{8,}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\\s)[0-9a-zA-Z!@#$%^&*()]*$"; private readonly UserManager _userManager; private readonly IVzSession _abpSession; private readonly UserRegistrationManager _userRegistrationManager; private readonly IRepository _userRepository; private readonly IWebHostEnvironment _env; private readonly IAppFolders _appFolders; public AccountAppService( UserRegistrationManager userRegistrationManager, IRepository userRepository, IAppFolders appFolders, IWebHostEnvironment env, ICacheManager cacheManager, UserManager userManager, IVzSession abpSession) { _userRegistrationManager = userRegistrationManager; _userRepository = userRepository; _appFolders = appFolders; _env = env; _userManager = userManager; _abpSession = abpSession; CacheManager = cacheManager; } [DisableAuditing] public async Task GetUserProfile(long? userId) { var user = await CacheManager.GetCache(VzConsts.CacheSystemUser).GetAsync(userId + "", async () => await _userRepository.FirstOrDefaultAsync(a => a.Id == userId)); if (user == null) { CheckErrors("未查询到用户!"); return null; } var dto = new UserProfileDto() { Id = user.Id, Surname = user.Surname, Name = user.Name, UserName = user.UserName, EmailAddress = user.EmailAddress, //Birthday = DateTime.Today, IdCard = "", AvatarPath = user.AvatarPath.Replace("\\", "/"), PhoneNumber = user.PhoneNumber, }; return dto; } public async Task UpdateUserProfile(UserProfileDto input) { var user = await _userRepository.FirstOrDefaultAsync(a => a.Id == input.Id); if (user == null) { CheckErrors("未查询到用户!"); return; } user.Surname = input.Surname; user.Name = input.Name; user.EmailAddress = input.EmailAddress; user.PhoneNumber = input.PhoneNumber; user.Gender = input.Gender; if (input.AvatarPath.NotEmpty() && input.AvatarPath.StartsWith("data:image")) { UpdateAvatar(user, input.AvatarPath); } await _userRepository.UpdateAsync(user); await CurrentUnitOfWork.SaveChangesAsync(); await CacheManager.GetCache(VzConsts.CacheSystemUser) .SetAsync(user.Id + "", user); } private void UpdateAvatar(User user, string avatarPath) { var fileName = $"{AbpSession.UserId}@{AbpSession.GetUserName()}"; var filePath = _appFolders.ProfileImagesFolder; var start = ";base64,"; var file = avatarPath.Substring(avatarPath.IndexOf(start, StringComparison.Ordinal) + start.Length); var url = file.Base64ToPng(fileName, filePath, _env); if (url.StartsWith("error@")) { string error = url.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1]; throw new UserFriendlyException("修改头像", error); } user.AvatarPath = url; } public async Task ChangePassword(ChangePasswordDto input) { if (_abpSession.UserId == null) { throw new UserFriendlyException("请登录后再尝试修改密码。"); } await _userManager.InitializeOptionsAsync(AbpSession.TenantId); var user = await _userManager.FindByIdAsync(AbpSession.GetUserId().ToString()); if (user == null) { throw new UserFriendlyException("没有当前用户!"); } if (await _userManager.CheckPasswordAsync(user, input.CurrentPassword)) { CheckErrors(await _userManager.ChangePasswordAsync(user, input.NewPassword)); } else { throw new UserFriendlyException("密码不正确!"); } } public async Task IsTenantAvailable(IsTenantAvailableInput input) { var tenant = await TenantManager.FindByTenancyNameAsync(input.TenancyName); if (tenant == null) { return new IsTenantAvailableOutput(TenantAvailabilityState.NotFound); } if (!tenant.IsActive) { return new IsTenantAvailableOutput(TenantAvailabilityState.InActive); } return new IsTenantAvailableOutput(TenantAvailabilityState.Available, tenant.Id); } public async Task Register(RegisterInput input) { var user = await _userRegistrationManager.RegisterAsync( input.Name, input.Surname, input.EmailAddress, input.UserName, input.Password, true // Assumed email address is always confirmed. Change this if you want to implement email confirmation. ); var isEmailConfirmationRequiredForLogin = await SettingManager.GetSettingValueAsync(VzSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin); return new RegisterOutput { CanLogin = user.IsActive && (user.IsEmailConfirmed || !isEmailConfirmationRequiredForLogin) }; } }