From 59794a6bf1a587ac110eabc7db88b16f376b9819 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 8 Apr 2024 19:50:16 +0000 Subject: [PATCH] --- server/AyaNova/biz/UserBiz.cs | 40 +++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/server/AyaNova/biz/UserBiz.cs b/server/AyaNova/biz/UserBiz.cs index ebd79d7a..1471bb4f 100644 --- a/server/AyaNova/biz/UserBiz.cs +++ b/server/AyaNova/biz/UserBiz.cs @@ -87,33 +87,47 @@ namespace AyaNova.Biz 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"); + 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) { //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 && ( + var InsideUserCanLoginByLastLoginList = await ct.User.Where(z => z.Id != 1 && z.Active == true && ( 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) { - u.Active = false; - var msg = $"User {u.Name} automatically set to inactive accomodate downgraded User count license"; - await NotifyEventHelper.AddOpsProblemEvent(msg); - _log.LogInformation(msg); + if (CountOfUsersToSetInactive > 0 && u.LastLogin == null) + { + u.Active = false; + 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(); - #if (SUBSCRIPTION_BUILD) - //Customer users as above - #endif - } }