From 83a0f4959492685a27bfbb680f6a517d5943fe75 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Mon, 25 Mar 2024 11:18:48 +0800 Subject: [PATCH] fix job --- .../CommonJobs/TempCreateUserJob.cs | 121 ------------------ .../CommonJobs/UpdateUserPasswordJob.cs | 61 +++++++++ Src/Dlhis.ScheduledTask/Packs/JobPack.cs | 10 +- 3 files changed, 66 insertions(+), 126 deletions(-) delete mode 100644 Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs create mode 100644 Src/Dlhis.ScheduledTask/CommonJobs/UpdateUserPasswordJob.cs diff --git a/Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs b/Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs deleted file mode 100644 index 8ff12d6c..00000000 --- a/Src/Dlhis.ScheduledTask/CommonJobs/TempCreateUserJob.cs +++ /dev/null @@ -1,121 +0,0 @@ -//using Dlhis.Entity.Basic.Entity; -//using Dlhis.Entity.Sys.Entities; -//using Kurisu.AspNetCore.Authentication.Options; -//using Kurisu.AspNetCore.Utils.Extensions; -//using Kurisu.Core.DataAccess.Entity; -//using Kurisu.SqlSugar.Services; -//using Microsoft.AspNetCore.Identity; -//using Microsoft.Extensions.Options; - -//namespace Dlhis.ScheduledTask.CommonJobs -//{ -// public class TempCreateUserJob -// { -// private readonly IDbContext _dbContext; -// private readonly ILogger _logger; -// private readonly IdentityServerOptions _identityServerOptions; -// private readonly IHttpClientFactory _httpClientFactory; - -// public TempCreateUserJob( -// IDbContext dbContext, -// ILogger logger, -// IOptions options, -// IHttpClientFactory httpClientFactory) -// { -// _dbContext = dbContext; -// _logger = logger; -// _httpClientFactory = httpClientFactory; -// _identityServerOptions = options.Value; -// } - -// public async Task RunAsync() -// { -// _dbContext.Client.QueryFilter.ClearAndBackup(); -// try -// { - -// //var identityDbContext = _dbContext.ChangeDb("identity"); - -// ////HST0001 -// //var users = await identityDbContext.Queryable().Where(x => x.UserName != "HST0001") -// // .Where(x => x.Id > 21) -// // .ToListAsync(); - -// //var gainfos = await _dbContext.Queryable().ToListAsync(); - -// //var hasher = new PasswordHasher(); -// //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); -// // } -// //} - -// //var userNames = await identityDbContext.Queryable().Select(x => x.UserName).ToListAsync(); -// //var newUser = await _dbContext.Queryable() -// // .Where(x => !string.IsNullOrEmpty(x.Code)) -// // .Where(x => !userNames.Contains(x.Code)) -// // .Select(x => new -// // { -// // x.Code, -// // x.Factory, -// // x.Name, -// // }) -// // .ToListAsync(); - -// //var httpClient = _httpClientFactory.CreateClient(); -// //httpClient.BaseAddress = new Uri(_identityServerOptions.Authority); - -// //var hasher = new PasswordHasher(); -// //foreach (var item in newUser) -// //{ -// // var userName = item.Code; -// // var password = item.Code; -// // var factory = item.Factory; - -// // if (password.Length > 6) -// // { -// // password = string.Join("", password.TakeLast(6)); -// // } -// // else -// // { -// // password = password.PadLeft(6, '0'); -// // } -// // var user = new UserEntity -// // { -// // UserName = userName, -// // NormalizedUserName = userName.ToUpper(), -// // ConcurrencyStamp = Guid.NewGuid().ToString(), -// // SecurityStamp = Guid.NewGuid().ToString(), -// // }; - -// // password = hasher.HashPassword(user, password); -// // user.PasswordHash = password; -// // var id = (int)await identityDbContext.InsertReturnIdentityAsync(user); - -// // await identityDbContext.InsertAsync(new UserTenantEntity() { UserId = id, TenantCode = factory }); -// // await identityDbContext.InsertAsync(new List -// // { -// // new UserClaimEntity{ UserId = id , ClaimType ="preferred_username", ClaimValue = item.Name}, -// // new UserClaimEntity{ UserId = id , ClaimType ="jinko-code", ClaimValue = item.Code}, -// // }); -// //} -// } -// catch (Exception ex) -// { -// _logger.LogError(ex, "创建用户失败:{message}", ex.Message); -// } -// finally -// { -// _dbContext.Client.QueryFilter.Restore(); -// } -// } -// } - - -//} diff --git a/Src/Dlhis.ScheduledTask/CommonJobs/UpdateUserPasswordJob.cs b/Src/Dlhis.ScheduledTask/CommonJobs/UpdateUserPasswordJob.cs new file mode 100644 index 00000000..e40fea37 --- /dev/null +++ b/Src/Dlhis.ScheduledTask/CommonJobs/UpdateUserPasswordJob.cs @@ -0,0 +1,61 @@ +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 _logger; + + public UpdateUserPasswordJob( + IDbContext dbContext, + ILogger logger) + { + _dbContext = dbContext; + _logger = logger; + } + + public async Task RunAsync() + { + _dbContext.Client.QueryFilter.ClearAndBackup(); + try + { + + var identityDbContext = _dbContext.ChangeDb("identity"); + + var whitelist = new List() { "admin", "WB-0741", "WB-1091" }; + + var users = await identityDbContext.Queryable() + .Where(x => !whitelist.Contains(x.UserName)) + .ToListAsync(); + + var gainfos = await _dbContext.Queryable().ToListAsync(); + + var hasher = new PasswordHasher(); + 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(); + } + } +} diff --git a/Src/Dlhis.ScheduledTask/Packs/JobPack.cs b/Src/Dlhis.ScheduledTask/Packs/JobPack.cs index 1d238070..a1c827b2 100644 --- a/Src/Dlhis.ScheduledTask/Packs/JobPack.cs +++ b/Src/Dlhis.ScheduledTask/Packs/JobPack.cs @@ -59,11 +59,11 @@ public class JobPack : BaseAppPack TimeZone = TimeZoneInfo.Local }); - - //RecurringJob.AddOrUpdate("TempCreateUser", s => s.RunAsync(), "0 0 5 * * ?", new RecurringJobOptions - //{ - // TimeZone = TimeZoneInfo.Local - //}); + //更新用户密码 + RecurringJob.AddOrUpdate("UpdateUserPassword", s => s.RunAsync(), "0 0 5 * * ?", new RecurringJobOptions + { + TimeZone = TimeZoneInfo.Local + }); //删除前三天前的日志 RecurringJob.AddOrUpdate("DelApiLogTask", s => s.RunAsync(), "0 0 */7 * *", new RecurringJobOptions