using Furion.DatabaseAccessor.Extensions;
using Microsoft.Extensions.Options;
using YBEE.EQM.Core;
namespace YBEE.EQM.Application;
///
/// 系统用户服务
///
public class SysUserService : ISysUserService, ITransient
{
private readonly IRepository _userRep;
private readonly AuthOptions _authOptions;
public SysUserService(IRepository userRep, IOptions options)
{
_userRep = userRep;
_authOptions = options.Value;
}
///
/// 获取所有用户列表
///
///
public async Task> GetAllList()
{
var items = await _userRep.DetachedEntities.Where(u => u.Status == CommonStatus.ENABLE)
.ProjectToType()
.ToListAsync();
return items;
}
///
/// 修改密码
///
///
///
public async Task ChangePassword(ChangeSysUserPasswordInput input)
{
string oldPwd = RSAEncryption.Decrypt(input.OldPassword, _authOptions.RsaPrivateKey);
string newPwd = RSAEncryption.Decrypt(input.NewPassword, _authOptions.RsaPrivateKey);
if (newPwd.Length < 6 || newPwd.Length > 32)
{
throw Oops.Oh(ErrorCode.E1006);
}
if ((await _userRep.DetachedEntities.AsQueryable().ToListAsync()).Any(t => t.Id == CurrentSysUserInfo.SysUserId && AESEncryption.Decrypt(t.Password, _authOptions.AesPassword).Equals(oldPwd)))
{
var user = await _userRep.FirstOrDefaultAsync(t => t.Id == CurrentSysUserInfo.SysUserId);
user.Password = AESEncryption.Encrypt(newPwd, _authOptions.AesPassword);
await user.UpdateIncludeNowAsync(new[] { "Password" });
}
else
{
throw Oops.Oh(ErrorCode.E1005);
}
}
}