Files
jinko.yikatong/Src/Dlhis.ScheduledTask/CommonJobs/UpdateUserPasswordJob.cs
2024-03-25 11:18:48 +08:00

62 lines
1.9 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Sys.Entities;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.DataAccess.Entity;
using Kurisu.SqlSugar.Services;
using Microsoft.AspNetCore.Identity;
namespace Dlhis.ScheduledTask.CommonJobs;
public class UpdateUserPasswordJob
{
private readonly IDbContext _dbContext;
private readonly ILogger<UpdateUserPasswordJob> _logger;
public UpdateUserPasswordJob(
IDbContext dbContext,
ILogger<UpdateUserPasswordJob> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
try
{
var identityDbContext = _dbContext.ChangeDb("identity");
var whitelist = new List<string>() { "admin", "WB-0741", "WB-1091" };
var users = await identityDbContext.Queryable<UserEntity>()
.Where(x => !whitelist.Contains(x.UserName))
.ToListAsync();
var gainfos = await _dbContext.Queryable<GainfoEntity>().ToListAsync();
var hasher = new PasswordHasher<UserEntity>();
foreach (var user in users)
{
var gainfo = gainfos.FirstOrDefault(x => x.Code == user.UserName);
if (gainfo != null && gainfo.IdNo.IsPresent())
{
var password = gainfo.IdNo.TakeLast(6).Join("");
password = hasher.HashPassword(user, password);
user.PasswordHash = password;
await identityDbContext.UpdateAsync(user);
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "密码修改失败:{message}", ex.Message);
}
finally
{
_dbContext.Client.QueryFilter.Restore();
}
}
}