fix 用户不存在则创建
This commit is contained in:
@@ -28,24 +28,68 @@ public class UpdateUserPasswordJob
|
||||
|
||||
var identityDbContext = _dbContext.ChangeDb("identity");
|
||||
|
||||
var whitelist = new List<string>() { "admin", "WB-0741", "WB-1091" };
|
||||
var whitelist = new List<string>() { "admin", "WB-0741", "WB-1091", "0123456" };
|
||||
|
||||
var users = await identityDbContext.Queryable<UserEntity>()
|
||||
.Where(x => !whitelist.Contains(x.UserName))
|
||||
.ToListAsync();
|
||||
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().ToListAsync();
|
||||
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => !whitelist.Contains(x.Code)).ToListAsync();
|
||||
|
||||
var hasher = new PasswordHasher<UserEntity>();
|
||||
foreach (var user in users)
|
||||
foreach (var gainfo in gainfos)
|
||||
{
|
||||
var gainfo = gainfos.FirstOrDefault(x => x.Code == user.UserName);
|
||||
if (gainfo != null && gainfo.IdNo.IsPresent())
|
||||
if (gainfo.IdNo.IsPresent())
|
||||
{
|
||||
var password = gainfo.IdNo.TakeLast(6).Join("");
|
||||
password = hasher.HashPassword(user, password);
|
||||
user.PasswordHash = password;
|
||||
await identityDbContext.UpdateAsync(user);
|
||||
try
|
||||
{
|
||||
var password = gainfo.IdNo.TakeLast(6).Join("");
|
||||
|
||||
var user = users.FirstOrDefault(x => x.UserName == gainfo.Code);
|
||||
if (user == null)
|
||||
{
|
||||
user = new UserEntity
|
||||
{
|
||||
UserName = gainfo.Code,
|
||||
NormalizedUserName = gainfo.Code.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 AddUserClaim(identityDbContext, id, "jinko-code", gainfo.Code);
|
||||
await AddUserClaim(identityDbContext, id, "name", gainfo.Name);
|
||||
if (!string.IsNullOrEmpty(gainfo.Factory))
|
||||
{
|
||||
var userTenant = await identityDbContext.Queryable<UserTenantEntity>().Where(x => x.UserId == user.Id).FirstAsync();
|
||||
if (userTenant == null)
|
||||
{
|
||||
userTenant = new UserTenantEntity
|
||||
{
|
||||
UserId = user.Id,
|
||||
TenantCode = gainfo.Factory
|
||||
};
|
||||
await identityDbContext.InsertAsync(userTenant);
|
||||
}
|
||||
else
|
||||
{
|
||||
userTenant.TenantCode = gainfo.Factory;
|
||||
await identityDbContext.UpdateAsync(userTenant);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
password = hasher.HashPassword(user, password);
|
||||
user.PasswordHash = password;
|
||||
await identityDbContext.UpdateAsync(user);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,4 +102,24 @@ public class UpdateUserPasswordJob
|
||||
_dbContext.Client.QueryFilter.Restore();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AddUserClaim(IDbContext dbContext, int userId, string type, string value)
|
||||
{
|
||||
var claim = await dbContext.Queryable<UserClaimEntity>().Where(x => x.UserId == userId).FirstAsync(x => x.ClaimType == type);
|
||||
if (claim == null)
|
||||
{
|
||||
claim = new UserClaimEntity()
|
||||
{
|
||||
UserId = userId,
|
||||
ClaimType = type,
|
||||
ClaimValue = value
|
||||
};
|
||||
await dbContext.InsertAsync(claim);
|
||||
}
|
||||
else
|
||||
{
|
||||
claim.ClaimValue = value;
|
||||
await dbContext.UpdateAsync(claim);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user