AccountAppService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using Abp.Auditing;
  2. using Abp.Configuration;
  3. using Abp.Domain.Repositories;
  4. using Abp.Runtime.Caching;
  5. using Abp.Runtime.Session;
  6. using Abp.UI;
  7. using Microsoft.AspNetCore.Hosting;
  8. using VberAdmin.Authorization.Accounts.Dto;
  9. using VberAdmin.Authorization.Users;
  10. using VberZero;
  11. using VberZero.AppService.Users.Dto;
  12. using VberZero.BaseSystem.Users;
  13. using VberZero.Folders;
  14. using VberZero.Session;
  15. using VberZero.Settings;
  16. using VberZero.Tools.FileHelpers;
  17. using VberZero.Tools.StringModel;
  18. namespace VberAdmin.Authorization.Accounts;
  19. public class AccountAppService : VberAdminAppServiceBase, IAccountAppService
  20. {
  21. // from: http://regexlib.com/REDetails.aspx?regexp_id=1923
  22. public const string PasswordRegex = "(?=^.{8,}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\\s)[0-9a-zA-Z!@#$%^&*()]*$";
  23. private readonly UserManager _userManager;
  24. private readonly IVzSession _abpSession;
  25. private readonly UserRegistrationManager _userRegistrationManager;
  26. private readonly IRepository<User, long> _userRepository;
  27. private readonly IWebHostEnvironment _env;
  28. private readonly IAppFolders _appFolders;
  29. public AccountAppService(
  30. UserRegistrationManager userRegistrationManager, IRepository<User, long> userRepository, IAppFolders appFolders, IWebHostEnvironment env, ICacheManager cacheManager, UserManager userManager, IVzSession abpSession)
  31. {
  32. _userRegistrationManager = userRegistrationManager;
  33. _userRepository = userRepository;
  34. _appFolders = appFolders;
  35. _env = env;
  36. _userManager = userManager;
  37. _abpSession = abpSession;
  38. CacheManager = cacheManager;
  39. }
  40. [DisableAuditing]
  41. public async Task<UserProfileDto> GetUserProfile(long? userId)
  42. {
  43. var user = await CacheManager.GetCache<string, User>(VzConsts.CacheSystemUser).GetAsync(userId + "", async () => await _userRepository.FirstOrDefaultAsync(a => a.Id == userId));
  44. if (user == null)
  45. {
  46. CheckErrors("未查询到用户!");
  47. return null;
  48. }
  49. var dto = new UserProfileDto()
  50. {
  51. Id = user.Id,
  52. Surname = user.Surname,
  53. Name = user.Name,
  54. UserName = user.UserName,
  55. EmailAddress = user.EmailAddress,
  56. //Birthday = DateTime.Today,
  57. IdCard = "",
  58. AvatarPath = user.AvatarPath.Replace("\\", "/"),
  59. PhoneNumber = user.PhoneNumber,
  60. };
  61. return dto;
  62. }
  63. public async Task UpdateUserProfile(UserProfileDto input)
  64. {
  65. var user = await _userRepository.FirstOrDefaultAsync(a => a.Id == input.Id);
  66. if (user == null)
  67. {
  68. CheckErrors("未查询到用户!");
  69. return;
  70. }
  71. user.Surname = input.Surname;
  72. user.Name = input.Name;
  73. user.EmailAddress = input.EmailAddress;
  74. user.PhoneNumber = input.PhoneNumber;
  75. user.Gender = input.Gender;
  76. if (input.AvatarPath.NotEmpty() && input.AvatarPath.StartsWith("data:image"))
  77. {
  78. UpdateAvatar(user, input.AvatarPath);
  79. }
  80. await _userRepository.UpdateAsync(user);
  81. await CurrentUnitOfWork.SaveChangesAsync();
  82. await CacheManager.GetCache(VzConsts.CacheSystemUser)
  83. .SetAsync(user.Id + "", user);
  84. }
  85. private void UpdateAvatar(User user, string avatarPath)
  86. {
  87. var fileName = $"{AbpSession.UserId}@{AbpSession.GetUserName()}";
  88. var filePath = _appFolders.ProfileImagesFolder;
  89. var start = ";base64,";
  90. var file = avatarPath.Substring(avatarPath.IndexOf(start, StringComparison.Ordinal) + start.Length);
  91. var url = file.Base64ToPng(fileName, filePath, _env);
  92. if (url.StartsWith("error@"))
  93. {
  94. string error = url.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries)[1];
  95. throw new UserFriendlyException("修改头像", error);
  96. }
  97. user.AvatarPath = url;
  98. }
  99. public async Task ChangePassword(ChangePasswordDto input)
  100. {
  101. if (_abpSession.UserId == null)
  102. {
  103. throw new UserFriendlyException("请登录后再尝试修改密码。");
  104. }
  105. await _userManager.InitializeOptionsAsync(AbpSession.TenantId);
  106. var user = await _userManager.FindByIdAsync(AbpSession.GetUserId().ToString());
  107. if (user == null)
  108. {
  109. throw new UserFriendlyException("没有当前用户!");
  110. }
  111. if (await _userManager.CheckPasswordAsync(user, input.CurrentPassword))
  112. {
  113. CheckErrors(await _userManager.ChangePasswordAsync(user, input.NewPassword));
  114. }
  115. else
  116. {
  117. throw new UserFriendlyException("密码不正确!");
  118. }
  119. }
  120. public async Task<IsTenantAvailableOutput> IsTenantAvailable(IsTenantAvailableInput input)
  121. {
  122. var tenant = await TenantManager.FindByTenancyNameAsync(input.TenancyName);
  123. if (tenant == null)
  124. {
  125. return new IsTenantAvailableOutput(TenantAvailabilityState.NotFound);
  126. }
  127. if (!tenant.IsActive)
  128. {
  129. return new IsTenantAvailableOutput(TenantAvailabilityState.InActive);
  130. }
  131. return new IsTenantAvailableOutput(TenantAvailabilityState.Available, tenant.Id);
  132. }
  133. public async Task<RegisterOutput> Register(RegisterInput input)
  134. {
  135. var user = await _userRegistrationManager.RegisterAsync(
  136. input.Name,
  137. input.Surname,
  138. input.EmailAddress,
  139. input.UserName,
  140. input.Password,
  141. true // Assumed email address is always confirmed. Change this if you want to implement email confirmation.
  142. );
  143. var isEmailConfirmationRequiredForLogin = await SettingManager.GetSettingValueAsync<bool>(VzSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin);
  144. return new RegisterOutput
  145. {
  146. CanLogin = user.IsActive && (user.IsEmailConfirmed || !isEmailConfirmationRequiredForLogin)
  147. };
  148. }
  149. }