This commit is contained in:
2024-04-08 19:50:16 +00:00
parent 5f094510d9
commit 59794a6bf1

View File

@@ -87,33 +87,47 @@ namespace AyaNova.Biz
var TotalActiveInternalUsers = await ActiveInternalUserCountAsync(); var TotalActiveInternalUsers = await ActiveInternalUserCountAsync();
int CountOfUsersToSetInactive = (int)(TotalActiveInternalUsers - KeepThisManyActiveInternalUsers); int CountOfUsersToSetInactive = (int)(TotalActiveInternalUsers - KeepThisManyActiveInternalUsers);
if (CountOfUsersToSetInactive < 1) return; if (CountOfUsersToSetInactive < 1) return;
_log.LogInformation($"New license is a downgrade with fewer Users - deactivating {CountOfUsersToSetInactive} least recently logged in Users automatically so the license can be installed"); await NotifyEventHelper.AddOpsProblemEvent($"New license is a downgrade with fewer Users - deactivating {CountOfUsersToSetInactive} least recently logged in Users automatically so the license can be installed");
using (AyContext ct = ServiceProviderProvider.DBContext) using (AyContext ct = ServiceProviderProvider.DBContext)
{ {
//Algorithm: For deactivation favor ones that have no login records and finally favor by oldest last login //Algorithm: For deactivation favor ones that have no login records and finally favor by oldest last login
//theory is to catch the least likely to be currently active user so least affected //theory is to catch the least likely to be currently active user so least affected
var InsideUserCanLoginByLastLoginList = await ct.User.Where(z => z.Active == true && ( var InsideUserCanLoginByLastLoginList = await ct.User.Where(z => z.Id != 1 && z.Active == true && (
z.UserType != UserType.Customer && z.UserType != UserType.Customer &&
z.UserType != UserType.HeadOffice)).OrderByDescending(z => z.LastLogin).Take(CountOfUsersToSetInactive).ToListAsync(); z.UserType != UserType.HeadOffice)).OrderBy(z => z.LastLogin).ToListAsync();
//TODO: examine this list, is it correct and in correct order
CountOfUsersToSetInactive -= InsideUserCanLoginByLastLoginList.Count(); //iterate users and set to inactive preferring the least likely to be affected users
//first do the ones that have never logged in
foreach (User u in InsideUserCanLoginByLastLoginList) foreach (User u in InsideUserCanLoginByLastLoginList)
{ {
u.Active = false; if (CountOfUsersToSetInactive > 0 && u.LastLogin == null)
var msg = $"User {u.Name} automatically set to inactive accomodate downgraded User count license"; {
await NotifyEventHelper.AddOpsProblemEvent(msg); u.Active = false;
_log.LogInformation(msg); CountOfUsersToSetInactive--;
var msg = $"User {u.Name} deactivated";
await NotifyEventHelper.AddOpsProblemEvent(msg);
}
}
//Now do the ones that logged in least recently (the natural order so just do the ones that are not lastlogin null)
foreach (User u in InsideUserCanLoginByLastLoginList)
{
//Now do the ones that logged in least recently (the natural order so just do the ones that are not lastlogin null)
if (CountOfUsersToSetInactive > 0 && u.LastLogin != null)
{
u.Active = false;
CountOfUsersToSetInactive--;
var msg = $"User {u.Name} deactivated";
await NotifyEventHelper.AddOpsProblemEvent(msg);
}
} }
await ct.SaveChangesAsync(); await ct.SaveChangesAsync();
#if (SUBSCRIPTION_BUILD)
//Customer users as above
#endif
} }
} }