case 4578

This commit is contained in:
2024-04-08 19:10:49 +00:00
parent 83e092f61a
commit 5f094510d9
2 changed files with 54 additions and 37 deletions

View File

@@ -43,6 +43,8 @@ namespace AyaNova.Biz
using (AyContext ct = ServiceProviderProvider.DBContext)
{
//all users who are not the superuser, active and not a customer or headoffice
//NOTE: AllowLogin is not counted here like it is with a Customer login account, we don't care, active is all that matters for license
//consumption
var ret = await ct.User.AsNoTracking().Where(z => z.Id != 1 && z.Active == true && (
z.UserType != UserType.Customer &&
z.UserType != UserType.HeadOffice)).LongCountAsync();
@@ -75,6 +77,48 @@ namespace AyaNova.Biz
}
}
//Called by license processor in case customer has downgraded the number of internal user licenses so that key install doesn't fail
internal static async Task DeActivateExcessiveInternallUsers(long KeepThisManyActiveInternalUsers, ILogger _log)
{
//case 4578 several modifications to this from prior existing function before switching to license by any internal users not just tech
var TotalActiveInternalUsers = await ActiveInternalUserCountAsync();
int CountOfUsersToSetInactive = (int)(TotalActiveInternalUsers - KeepThisManyActiveInternalUsers);
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");
using (AyContext ct = ServiceProviderProvider.DBContext)
{
//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
var InsideUserCanLoginByLastLoginList = await ct.User.Where(z => z.Active == true && (
z.UserType != UserType.Customer &&
z.UserType != UserType.HeadOffice)).OrderByDescending(z => z.LastLogin).Take(CountOfUsersToSetInactive).ToListAsync();
//TODO: examine this list, is it correct and in correct order
CountOfUsersToSetInactive -= InsideUserCanLoginByLastLoginList.Count();
foreach (User u in InsideUserCanLoginByLastLoginList)
{
u.Active = false;
var msg = $"User {u.Name} automatically set to inactive accomodate downgraded User count license";
await NotifyEventHelper.AddOpsProblemEvent(msg);
_log.LogInformation(msg);
}
await ct.SaveChangesAsync();
#if (SUBSCRIPTION_BUILD)
//Customer users as above
#endif
}
}
// //Called by license processor when use downgrades to lesser amount of techs
// internal static async Task DeActivateExcessiveTechs(long KeepThisManyActiveTechs, ILogger _log)
// {