Skip to content

Commit a8d9a95

Browse files
committed
Sign-In: Handle properly the case where two or more database records are found in user table.
1 parent 25c099c commit a8d9a95

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

api/CESMII.ProfileDesigner.Api/Controllers/AuthController.cs

+39-30
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public IActionResult QueryCurrentOrganization()
105105
var listMatchEmailAddress = _dalUser.Where(x => x.EmailAddress.ToLower().Equals(userAAD.Email.ToLower()) && x.ObjectIdAAD == null, null).Data;
106106
if (listMatchEmailAddress.Count == 0)
107107
{
108-
// Here for (1) manually created users, or (2) users from marketplace self-service sign-up.
108+
// Here for (1) manually created users, or (2) users from profile designer or marketplace self-service sign-up.
109109
um = new UserModel()
110110
{
111111
ObjectIdAAD = userAAD.ObjectIdAAD,
@@ -140,36 +140,45 @@ public IActionResult QueryCurrentOrganization()
140140
_logger.LogWarning(strError);
141141
}
142142
}
143-
else
143+
else if (listMatchEmailAddress.Count > 1)
144144
{
145-
// When more than 1 record, it means they have signed up (and then left) more than
146-
// once. This is okay, but we pick the most recent one.
147-
// listMatchEmailAddress.Sort((em1, em2) => DateTime?.Compare(em1.LastLogin, em2.LastLogin));
148-
listMatchEmailAddress.Sort((em1, em2) =>
149-
{
150-
DateTime dt1 = new DateTime(em1.LastLogin.Value.Ticks);
151-
DateTime dt2 = new DateTime(em2.LastLogin.Value.Ticks);
152-
return DateTime.Compare(dt1, dt2);
153-
});
154-
155-
int iItem = listMatchEmailAddress.Count - 1;
156-
um = listMatchEmailAddress[iItem];
157-
if (um.ObjectIdAAD == null)
158-
{
159-
um.ObjectIdAAD = userAAD.ObjectIdAAD;
160-
um.Email = userAAD.Email;
161-
um.DisplayName = userAAD.DisplayName;
162-
um.LastLogin = DateTime.UtcNow;
163-
164-
bUpdateUser = true; // Synch UserModel changes
165-
bCheckOrganization = true; // Check the user's organization.
166-
}
167-
else
168-
{
169-
bErrorCondition = true;
170-
strError = $"InitLocalUser||More than one Profile designer user record found with email {userAAD.Email}. {listMatchEmailAddress.Count} records found. Existing object id = {um.ObjectIdAAD}";
171-
_logger.LogWarning(strError);
172-
}
145+
// When more than 1 record, it means they have signed up (and then left) more than
146+
// once. This is okay, but we pick the most recent (newest) one.
147+
// listMatchEmailAddress.Sort((em1, em2) => DateTime?.Compare(em1.Created, em2.Created));
148+
listMatchEmailAddress.Sort((em1, em2) =>
149+
{
150+
DateTime dt1 = new DateTime(em1.Created.Value.Ticks);
151+
DateTime dt2 = new DateTime(em2.Created.Value.Ticks);
152+
return DateTime.Compare(dt1, dt2);
153+
});
154+
155+
156+
int iItem = listMatchEmailAddress.Count - 1; // We must have at least 2 records this index causes an exception
157+
if (iItem < 0) iItem = 0; // Just to make sure this is within range
158+
159+
// We use the most recent records that we have for this user.
160+
um = listMatchEmailAddress[iItem];
161+
um.ObjectIdAAD = userAAD.ObjectIdAAD;
162+
um.Email = userAAD.Email;
163+
um.DisplayName = userAAD.DisplayName;
164+
um.LastLogin = DateTime.UtcNow;
165+
166+
bUpdateUser = true; // Synch UserModel changes
167+
bCheckOrganization = true; // Check the user's organization.
168+
169+
// Note: If we want to automate the removal of duplicate records and logging of it,
170+
// we would do that here with code like the following:
171+
172+
////////for (int iDeleteMe = 0; iDeleteMe < listMatchEmailAddress.Count - 1; iDeleteMe++)
173+
////////{
174+
//////// if (listMatchEmailAddress[iDeleteMe].ID != null)
175+
//////// {
176+
//////// string strWarning = $"InitLocalUser|| About to delete record {iDeleteMe} of {listMatchEmailAddress.Count} from public.user. Id: {listMatchEmailAddress[iDeleteMe].ID.Value} Email: {listMatchEmailAddress[iDeleteMe].Email}";
177+
//////// _logger.LogWarning(strWarning);
178+
179+
//////// await _dalUser.DeleteAsync(listMatchEmailAddress[iDeleteMe].ID.Value, base.DalUserToken);
180+
//////// }
181+
////////}
173182
}
174183
}
175184

common

0 commit comments

Comments
 (0)