Skip to content

Commit

Permalink
aaa conflict on update - need to implement non-conflicting with myself
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Oct 19, 2024
1 parent 8935583 commit 378aa99
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private UserAccountDetailsDTO createOrGetExistingUser(String oauthId, String log
var newUserAccount = buildEntity(oauthId, login, attributes, roles);

// insert (optionally with conflict solving)
conflictService.process(getConflictPrefix(), getConflictResolveStrategy(), newUserAccount, this, eventsContainer);
conflictService.process(getConflictPrefix(), getConflictResolveStrategy(), ConflictService.PotentiallyConflictingAction.INSERT, newUserAccount, this, eventsContainer);
// due to conflict we can ignore the user and not to save him or we can create a new
// so we try to lookup him
userAccount = findByOAuth2Id(oauthId)
Expand Down Expand Up @@ -187,6 +187,14 @@ public void insertUsers(Collection<UserAccount> users, List<EventWrapper<?>> eve
}
}

@Override
public void updateUsers(Collection<UserAccount> users, List<EventWrapper<?>> eventsContainer) {
for (UserAccount userAccount : users) {
eventsContainer.add(eventService.convertProfileUpdated(userAccount));
}
userAccountRepository.saveAll(users);
}

@Override
public void removeUser(UserAccount userAccount, List<EventWrapper<?>> eventsContainer) {
userAccountRepository.deleteById(userAccount.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public Authentication authenticate(Authentication authentication) throws Authent
getNowUTC()
);
// check for conflicts by username or email and create the user if conflict resolution is not "ignore"
conflictService.process(LDAP_CONFLICT_PREFIX, aaaProperties.ldap().resolveConflictsStrategy(), userToInsert, this, eventsContainer);
conflictService.process(LDAP_CONFLICT_PREFIX, aaaProperties.ldap().resolveConflictsStrategy(), ConflictService.PotentiallyConflictingAction.INSERT, userToInsert, this, eventsContainer);
// due to conflict we can ignore the user and not to save him or we can create a new
// so we try to lookup him
var foundNewUser = userAccountRepository.findByLdapId(ldapUserId)
Expand Down Expand Up @@ -180,6 +180,14 @@ public void insertUsers(Collection<UserAccount> users, List<EventWrapper<?>> eve
}
}

@Override
public void updateUsers(Collection<UserAccount> users, List<EventWrapper<?>> eventsContainer) {
for (UserAccount userAccount : users) {
eventsContainer.add(eventService.convertProfileUpdated(userAccount));
}
userAccountRepository.saveAll(users);
}

@Override
public void removeUser(UserAccount userAccount, List<EventWrapper<?>> eventsContainer) {
userAccountRepository.deleteById(userAccount.id());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface ConflictResolvingActions {
void removeUser(UserAccount userAccount, List<EventWrapper<?>> eventsContainer);

void insertUsers(Collection<UserAccount> users, List<EventWrapper<?>> eventsContainer);

void updateUsers(Collection<UserAccount> users, List<EventWrapper<?>> eventsContainer);
}
48 changes: 40 additions & 8 deletions aaa/src/main/java/name/nkonev/aaa/services/ConflictService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ public class ConflictService {
@Autowired
private CheckService checkService;

public enum PotentiallyConflictingAction {
INSERT,
UPDATE
}

private static final Logger LOGGER = LoggerFactory.getLogger(ConflictService.class);

public void process(String renamingPrefix, ConflictResolveStrategy resolveConflictsStrategy, UserAccount newUser, ConflictResolvingActions conflictResolvingActions, List<EventWrapper<?>> eventsContainer) {
process(renamingPrefix, resolveConflictsStrategy, List.of(newUser), conflictResolvingActions, eventsContainer);
public void process(String renamingPrefix, ConflictResolveStrategy resolveConflictsStrategy, PotentiallyConflictingAction action, UserAccount newUser, ConflictResolvingActions conflictResolvingActions, List<EventWrapper<?>> eventsContainer) {
process(renamingPrefix, resolveConflictsStrategy, action, List.of(newUser), conflictResolvingActions, eventsContainer);
}

// we suppose that vast majority of users will not have any conflicts ...
public void process(String renamingPrefix, ConflictResolveStrategy resolveConflictsStrategy, Collection<UserAccount> newUsers, ConflictResolvingActions conflictResolvingActions, List<EventWrapper<?>> eventsContainer) {
public void process(String renamingPrefix, ConflictResolveStrategy resolveConflictsStrategy, PotentiallyConflictingAction action, Collection<UserAccount> newUsers, ConflictResolvingActions conflictResolvingActions, List<EventWrapper<?>> eventsContainer) {
if (newUsers.isEmpty()) {
return;
}
Expand All @@ -37,7 +42,16 @@ public void process(String renamingPrefix, ConflictResolveStrategy resolveConfli

// ... so we save them in batch
if (!nonConflictingUsers.isEmpty()) {
conflictResolvingActions.insertUsers(nonConflictingUsers, eventsContainer);
switch (action) {
case INSERT:
conflictResolvingActions.insertUsers(nonConflictingUsers, eventsContainer);
break;
case UPDATE:
conflictResolvingActions.updateUsers(nonConflictingUsers, eventsContainer);
break;
default:
throw new IllegalStateException("Unexpected value: " + action);
}
}

if (nonConflictingUsers.size() == newUsers.size()) {
Expand Down Expand Up @@ -70,12 +84,12 @@ public void process(String renamingPrefix, ConflictResolveStrategy resolveConfli
conflictBy.put(ConflictBy.EMAIL, oldUserConflictingByEmail);
}

solveConflict(renamingPrefix, resolveConflictsStrategy, conflictResolvingActions, newUser, conflictBy, eventsContainer);
solveConflict(renamingPrefix, resolveConflictsStrategy, action, conflictResolvingActions, newUser, conflictBy, eventsContainer);
}
}


private void solveConflict(String renamingPrefix, ConflictResolveStrategy resolveConflictsStrategy, ConflictResolvingActions conflictResolvingActions, UserAccount newUser, Map<ConflictBy, UserAccount> conflictBy, List<EventWrapper<?>> eventsContainer) {
private void solveConflict(String renamingPrefix, ConflictResolveStrategy resolveConflictsStrategy, PotentiallyConflictingAction action, ConflictResolvingActions conflictResolvingActions, UserAccount newUser, Map<ConflictBy, UserAccount> conflictBy, List<EventWrapper<?>> eventsContainer) {
switch (resolveConflictsStrategy) {
case IGNORE:
LOGGER.info("Skipping importing an user {} with conflicting by {}", newUser, conflictBy.keySet());
Expand All @@ -86,7 +100,16 @@ private void solveConflict(String renamingPrefix, ConflictResolveStrategy resolv
conflictResolvingActions.removeUser(oldUser, eventsContainer);
});
LOGGER.info("Saving new user {}", newUser);
conflictResolvingActions.insertUser(newUser, eventsContainer);
switch (action) {
case INSERT:
conflictResolvingActions.insertUser(newUser, eventsContainer);
break;
case UPDATE:
conflictResolvingActions.updateUser(newUser, eventsContainer);
break;
default:
throw new IllegalStateException("Unexpected value: " + action);
}
return;
case WRITE_NEW_AND_RENAME_OLD:
conflictBy.forEach((cb, oldUser) -> {
Expand All @@ -108,7 +131,16 @@ private void solveConflict(String renamingPrefix, ConflictResolveStrategy resolv
}
});
LOGGER.info("Saving new user {}", newUser);
conflictResolvingActions.insertUser(newUser, eventsContainer);
switch (action) {
case INSERT:
conflictResolvingActions.insertUser(newUser, eventsContainer);
break;
case UPDATE:
conflictResolvingActions.updateUser(newUser, eventsContainer);
break;
default:
throw new IllegalStateException("Unexpected value: " + action);
}
return;
default:
throw new IllegalStateException("Missed action for conflict strategy: " + resolveConflictsStrategy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void insertUsers(Collection<UserAccount> users, List<EventWrapper<?>> eve
}
}

@Override
public void updateUsers(Collection<UserAccount> users, List<EventWrapper<?>> eventsContainer) {
for (UserAccount userAccount : users) {
eventsContainer.add(eventService.convertProfileUpdated(userAccount));
Expand Down Expand Up @@ -176,7 +177,7 @@ protected void processUpsertBatch(List<T> entries) {

getLogger().info("Inserting {} users to database", toInsert.size());
var convertedToInsert = toInsert.stream().map(this::prepareUserAccountForInsert).toList();
conflictService.process(getRenamingPrefix(), getConflictResolvingStrategy(), convertedToInsert, this, eventsContainer);
conflictService.process(getRenamingPrefix(), getConflictResolvingStrategy(), ConflictService.PotentiallyConflictingAction.INSERT, convertedToInsert, this, eventsContainer);

if (!toUpdateSetExtSyncTime.isEmpty()) {
getLogger().info("Updating {} sync time for {} untoucned users", getName(), toUpdateSetExtSyncTime.size());
Expand Down

0 comments on commit 378aa99

Please sign in to comment.