| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 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<User, long> _userRepository;
- private readonly IWebHostEnvironment _env;
- private readonly IAppFolders _appFolders;
- public AccountAppService(
- UserRegistrationManager userRegistrationManager, IRepository<User, long> 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<UserProfileDto> GetUserProfile(long? userId)
- {
- var user = await CacheManager.GetCache<string, User>(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<IsTenantAvailableOutput> 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<RegisterOutput> 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<bool>(VzSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin);
- return new RegisterOutput
- {
- CanLogin = user.IsActive && (user.IsEmailConfirmed || !isEmailConfirmationRequiredForLogin)
- };
- }
- }
|