protected override void PersistUpdatedItem(IIdentityUserLogin entity) { entity.UpdatingEntity(); var dto = ExternalLoginFactory.BuildDto(entity); Database.Update(dto); entity.ResetDirtyProperties(); }
protected override void PersistNewItem(IIdentityUserLogin entity) { entity.AddingEntity(); var dto = ExternalLoginFactory.BuildDto(entity); var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; entity.ResetDirtyProperties(); }
/// <inheritdoc /> public void Save(Guid userOrMemberKey, IEnumerable <IExternalLogin> logins) { Sql <ISqlContext> sql = Sql() .Select <ExternalLoginDto>() .From <ExternalLoginDto>() .Where <ExternalLoginDto>(x => x.UserOrMemberKey == userOrMemberKey) .ForUpdate(); // deduplicate the logins logins = logins.DistinctBy(x => x.ProviderKey + x.LoginProvider).ToList(); var toUpdate = new Dictionary <int, IExternalLogin>(); var toDelete = new List <int>(); var toInsert = new List <IExternalLogin>(logins); List <ExternalLoginDto>?existingLogins = Database.Fetch <ExternalLoginDto>(sql); foreach (ExternalLoginDto?existing in existingLogins) { IExternalLogin?found = logins.FirstOrDefault(x => x.LoginProvider.Equals(existing.LoginProvider, StringComparison.InvariantCultureIgnoreCase) && x.ProviderKey.Equals(existing.ProviderKey, StringComparison.InvariantCultureIgnoreCase)); if (found != null) { toUpdate.Add(existing.Id, found); // if it's an update then it's not an insert toInsert.RemoveAll(x => x.ProviderKey == found.ProviderKey && x.LoginProvider == found.LoginProvider); } else { toDelete.Add(existing.Id); } } // do the deletes, updates and inserts if (toDelete.Count > 0) { Database.DeleteMany <ExternalLoginDto>().Where(x => toDelete.Contains(x.Id)).Execute(); } foreach (KeyValuePair <int, IExternalLogin> u in toUpdate) { Database.Update(ExternalLoginFactory.BuildDto(userOrMemberKey, u.Value, u.Key)); } Database.InsertBulk(toInsert.Select(i => ExternalLoginFactory.BuildDto(userOrMemberKey, i))); }
/// <inheritdoc /> public void Save(Guid userOrMemberKey, IEnumerable <IExternalLoginToken> tokens) { // get the existing logins (provider + id) var existingUserLogins = Database .Fetch <ExternalLoginDto>(GetBaseQuery(false).Where <ExternalLoginDto>(x => x.UserOrMemberKey == userOrMemberKey)) .ToDictionary(x => x.LoginProvider, x => x.Id); // deduplicate the tokens tokens = tokens.DistinctBy(x => x.LoginProvider + x.Name).ToList(); var providers = tokens.Select(x => x.LoginProvider).Distinct().ToList(); Sql <ISqlContext> sql = GetBaseTokenQuery(true) .WhereIn <ExternalLoginDto>(x => x.LoginProvider, providers) .Where <ExternalLoginDto>(x => x.UserOrMemberKey == userOrMemberKey); var toUpdate = new Dictionary <int, (IExternalLoginToken externalLoginToken, int externalLoginId)>(); var toDelete = new List <int>(); var toInsert = new List <IExternalLoginToken>(tokens); var existingTokens = Database.Fetch <ExternalLoginTokenDto>(sql); foreach (ExternalLoginTokenDto existing in existingTokens) { IExternalLoginToken found = tokens.FirstOrDefault(x => x.LoginProvider.InvariantEquals(existing.ExternalLoginDto.LoginProvider) && x.Name.InvariantEquals(existing.Name)); if (found != null) { toUpdate.Add(existing.Id, (found, existing.ExternalLoginId)); // if it's an update then it's not an insert toInsert.RemoveAll(x => x.LoginProvider.InvariantEquals(found.LoginProvider) && x.Name.InvariantEquals(found.Name)); } else { toDelete.Add(existing.Id); } } // do the deletes, updates and inserts if (toDelete.Count > 0) { Database.DeleteMany <ExternalLoginTokenDto>().Where(x => toDelete.Contains(x.Id)).Execute(); } foreach (KeyValuePair <int, (IExternalLoginToken externalLoginToken, int externalLoginId)> u in toUpdate) { Database.Update(ExternalLoginFactory.BuildDto(u.Value.externalLoginId, u.Value.externalLoginToken, u.Key)); } var insertDtos = new List <ExternalLoginTokenDto>(); foreach (IExternalLoginToken t in toInsert) { if (!existingUserLogins.TryGetValue(t.LoginProvider, out int externalLoginId)) { throw new InvalidOperationException($"A token was attempted to be saved for login provider {t.LoginProvider} which is not assigned to this user"); } insertDtos.Add(ExternalLoginFactory.BuildDto(externalLoginId, t)); } Database.InsertBulk(insertDtos); }