Skip to content

Commit ff9de18

Browse files
committed
Fixing single sign to deal with multiple user records in public.user
1 parent bcc1311 commit ff9de18

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

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

+19-20
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
using Microsoft.AspNetCore.Mvc;
1212
using Microsoft.Extensions.Logging;
1313
using System;
14-
using System.Threading;
15-
14+
using System.Threading;
15+
using System.Threading.Tasks;
16+
1617
[Authorize, Route("api/[controller]")]
1718
public class AuthController : BaseController<AuthController>
1819
{
@@ -30,7 +31,7 @@ public IActionResult OnAADLogin()
3031
//extract user name from identity passed in via token
3132
//check if that user record is in DB. If not, add it.
3233
//InitLocalUser: this property checks for user, adds to db and returns a fully formed user model if one does not exist.
33-
var returned = InitLocalUser();
34+
var returned = InitLocalUser().Result;
3435
UserModel user = returned.Item1;
3536
String strError = returned.Item2;
3637

@@ -62,7 +63,7 @@ public IActionResult QueryCurrentOrganization()
6263
/// </summary>
6364
/// <returns></returns>
6465
/// <exception cref="ArgumentNullException"></exception>
65-
protected (UserModel,string) InitLocalUser()
66+
protected async Task<(UserModel, string)> InitLocalUser()
6667
{
6768
bool bCheckOrganization = false;
6869
bool bUpdateUser = false;
@@ -143,7 +144,7 @@ public IActionResult QueryCurrentOrganization()
143144
else
144145
{
145146
// 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+
// once. This is okay, but we pick the most recent (newest) one.
147148
// listMatchEmailAddress.Sort((em1, em2) => DateTime?.Compare(em1.Created, em2.Created));
148149
listMatchEmailAddress.Sort((em1, em2) =>
149150
{
@@ -152,8 +153,8 @@ public IActionResult QueryCurrentOrganization()
152153
return DateTime.Compare(dt1, dt2);
153154
});
154155

155-
// We must have at least 2 records or this line gives an exception
156-
int iItem = listMatchEmailAddress.Count - 1;
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
157158

158159
// We use the most recent records that we have for this user.
159160
um = listMatchEmailAddress[iItem];
@@ -165,20 +166,19 @@ public IActionResult QueryCurrentOrganization()
165166
bUpdateUser = true; // Synch UserModel changes
166167
bCheckOrganization = true; // Check the user's organization.
167168

168-
// Note: If we want to automate the removal of duplicate records and logging of it,
169-
// we would do that here with code like the following:
169+
// Removing duplicate records.
170+
// We log it, just so there is a record of it.
170171

171-
////////for (int iDeleteMe = 0; iDeleteMe < listMatchEmailAddress.Count - 1; iDeleteMe++)
172-
////////{
173-
//////// if (listMatchEmailAddress[iDeleteMe].ID != null)
174-
//////// {
175-
//////// string strWarning = $"InitLocalUser|| About to delete record {iDeleteMe} of {listMatchEmailAddress.Count} from public.user. Id: {listMatchEmailAddress[iDeleteMe].ID.Value} Email: {listMatchEmailAddress[iDeleteMe].Email}";
176-
//////// _logger.LogWarning(strWarning);
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);
177178

178-
//////// await _dalUser.DeleteAsync(listMatchEmailAddress[iDeleteMe].ID.Value, base.DalUserToken);
179-
//////// }
180-
////////}
181-
179+
await _dalUser.DeleteAsync(listMatchEmailAddress[iDeleteMe].ID.Value, base.DalUserToken);
180+
}
181+
}
182182
}
183183
}
184184

@@ -233,6 +233,5 @@ public IActionResult QueryCurrentOrganization()
233233
return (um,null);
234234

235235
}
236-
237236
}
238237
}

common

0 commit comments

Comments
 (0)