feat 同步账号

This commit is contained in:
2024-03-10 00:55:51 +08:00
parent 4e0485d51f
commit 64a63e2e49
5 changed files with 130 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ namespace Dlhis.Entity.Sys.Entities;
[SugarTable(TableName = "UserTenants")]
public class UserTenantEntity
{
[SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
public int Id { get; set; }
public int UserId { get; set; }

View File

@@ -0,0 +1,99 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Sys.Entities;
using Kurisu.AspNetCore.Authentication.Options;
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<TempCreateUserJob> _logger;
private readonly IdentityServerOptions _identityServerOptions;
private readonly IHttpClientFactory _httpClientFactory;
public TempCreateUserJob(
IDbContext dbContext,
ILogger<TempCreateUserJob> logger,
IOptions<IdentityServerOptions> options,
IHttpClientFactory httpClientFactory)
{
_dbContext = dbContext;
_logger = logger;
_httpClientFactory = httpClientFactory;
_identityServerOptions = options.Value;
}
public async Task RunAsync()
{
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
try
{
var identityDbContext = _dbContext.ChangeDb("identity");
var userNames = await identityDbContext.Queryable<UserEntity>().Select(x => x.UserName).ToListAsync();
var newUser = await _dbContext.Queryable<GainfoEntity>()
.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<UserEntity>();
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<UserClaimEntity>
{
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();
}
}
}
}

View File

@@ -11,7 +11,6 @@ public class JobPack : BaseAppPack
public override int Order => 999;
public override bool IsBeforeUseRouting { get; }
public override void Invoke(IServiceProvider serviceProvider)
{
RecurringJob.AddOrUpdate<SyncOrganizationJob>("SyncSap", s => s.RunAsync(), "0 0 2 * * ?", new RecurringJobOptions
@@ -53,5 +52,11 @@ public class JobPack : BaseAppPack
{
TimeZone = TimeZoneInfo.Local
});
RecurringJob.AddOrUpdate<TempCreateUserJob>("TempCreateUser", s => s.RunAsync(), "0 0 5 * * ?", new RecurringJobOptions
{
TimeZone = TimeZoneInfo.Local
});
}
}

View File

@@ -1,5 +1,6 @@
using Kurisu.AspNetCore.Startup;
using Kurisu.SqlSugar.Extensions;
using SqlSugar;
namespace Dlhis.ScheduledTask.Packs;
@@ -9,6 +10,27 @@ public class SqlSugarPack : BaseAppPack
public override void ConfigureServices(IServiceCollection services)
{
services.AddSqlSugar();
services.AddSqlSugar(sp =>
{
var configuration = sp.GetService<IConfiguration>();
return new List<ConnectionConfig>
{
new ConnectionConfig
{
ConfigId = "identity",
ConnectionString = configuration.GetSection("SqlSugarOptions:IdentityConnectionString").Value,
DbType = DbType.MySql,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
MoreSettings = new ConnMoreSettings
{
DisableNvarchar = true,
}
},
};
});
}
}

View File

@@ -17,6 +17,7 @@
"DefaultConnectionString": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=yikatong;charset=utf8mb4;",
"SlaveConnectionString": [],
"FaceConnectionString": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=face;charset=utf8mb4;",
"IdentityConnectionString": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=identity;charset=utf8mb4;",
"Timeout": 30,
"SlowSqlTime": 1,
"EnableSqlLog": false