Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Sep 19, 2024
1 parent 35f99aa commit 5391d7d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import name.nkonev.aaa.entity.jdbc.UserAccount;
import name.nkonev.aaa.entity.ldap.LdapEntity;
import name.nkonev.aaa.repository.jdbc.UserAccountRepository;
import name.nkonev.aaa.services.ConflictResolingActions;
import name.nkonev.aaa.services.ConflictResolvingActions;
import name.nkonev.aaa.services.ConflictService;
import name.nkonev.aaa.services.EventService;
import org.slf4j.Logger;
Expand All @@ -33,7 +33,7 @@

// https://spring.io/guides/gs/authenticating-ldap
@Component
public class LdapAuthenticationProvider implements AuthenticationProvider, ConflictResolingActions {
public class LdapAuthenticationProvider implements AuthenticationProvider, ConflictResolvingActions {

@Autowired
private LdapOperations ldapOperations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import name.nkonev.aaa.entity.jdbc.UserAccount;

public interface ConflictResolingActions {
public interface ConflictResolvingActions {

void saveUser(UserAccount userAccount);

Expand Down
36 changes: 18 additions & 18 deletions aaa/src/main/java/name/nkonev/aaa/services/ConflictService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,68 @@ private Map<ConflictBy, UserAccount> checkForConflicts(String username, String e
return conflicts;
}

public void process(UserAccount newUser, ConflictResolingActions conflictResolingActions) {
public void process(UserAccount newUser, ConflictResolvingActions conflictResolvingActions) {
var conflicts = checkForConflicts(newUser.username(), newUser.email());

if (conflicts.isEmpty()) {
conflictResolingActions.saveUser(newUser);
conflictResolvingActions.saveUser(newUser);
return;
}

LOGGER.info("For new user {} found conflicting old users: {}", newUser, conflicts);

if (conflicts.containsKey(ConflictBy.USERNAME)) {
solveUsernameConflict(conflictResolingActions, conflicts.get(ConflictBy.USERNAME), newUser);
}

if (conflicts.containsKey(ConflictBy.EMAIL)) {
solveEmailConflict(conflictResolingActions, conflicts.get(ConflictBy.EMAIL), newUser);
for (var ce : conflicts.entrySet()) {
switch (ce.getKey()) {
case ConflictBy.USERNAME -> solveUsernameConflict(conflictResolvingActions, ce.getValue(), newUser);
case ConflictBy.EMAIL -> solveEmailConflict(conflictResolvingActions, ce.getValue(), newUser);
default -> throw new IllegalStateException("Unexpected conflict resolving action: " + ce.getKey());
}
}
}


private void solveUsernameConflict(ConflictResolingActions conflictResolingActions, UserAccount oldUser, UserAccount newUser) {
private void solveUsernameConflict(ConflictResolvingActions conflictResolvingActions, UserAccount oldUser, UserAccount newUser) {
switch (aaaProperties.ldap().resolveConflictsStrategy()) {
case IGNORE:
LOGGER.info("Skipping importing an user with conflicting username {}", newUser.username());
return;
case WRITE_NEW_AND_REMOVE_OLD:
LOGGER.info("Removing old conflicting user {}", oldUser);
conflictResolingActions.removeUser(oldUser);
conflictResolvingActions.removeUser(oldUser);
LOGGER.info("Saving new user {}", newUser);
conflictResolingActions.saveUser(newUser);
conflictResolvingActions.saveUser(newUser);
return;
case WRITE_NEW_AND_RENAME_OLD:
var rl = LDAP_CONFLICT_PREFIX + oldUser.username();
LOGGER.info("Saving old conflicting user {} with renamed login {}", oldUser, rl);
oldUser = oldUser.withUsername(rl);
conflictResolingActions.saveUser(oldUser);
conflictResolvingActions.saveUser(oldUser);
LOGGER.info("Saving new user {}", newUser);
conflictResolingActions.saveUser(newUser);
conflictResolvingActions.saveUser(newUser);
return;
default:
throw new IllegalStateException("Missed action for conflict strategy: " + aaaProperties.ldap().resolveConflictsStrategy());
}
}

private void solveEmailConflict(ConflictResolingActions conflictResolingActions, UserAccount oldUser, UserAccount newUser) {
private void solveEmailConflict(ConflictResolvingActions conflictResolvingActions, UserAccount oldUser, UserAccount newUser) {
switch (aaaProperties.ldap().resolveConflictsStrategy()) {
case IGNORE:
LOGGER.info("Skipping importing an user with conflicting email {}", newUser.email());
return;
case WRITE_NEW_AND_REMOVE_OLD:
LOGGER.info("Removing old conflicting user {}", oldUser);
conflictResolingActions.removeUser(oldUser);
conflictResolvingActions.removeUser(oldUser);
LOGGER.info("Saving new user {}", newUser);
conflictResolingActions.saveUser(newUser);
conflictResolvingActions.saveUser(newUser);
return;
case WRITE_NEW_AND_RENAME_OLD:
var re = LDAP_CONFLICT_PREFIX + oldUser.email();
LOGGER.info("Saving old conflicting user {} with renamed email {}", oldUser, re);
oldUser = oldUser.withEmail(re);
conflictResolingActions.saveUser(oldUser);
conflictResolvingActions.saveUser(oldUser);
LOGGER.info("Saving new user {}", newUser);
conflictResolingActions.saveUser(newUser);
conflictResolvingActions.saveUser(newUser);
return;
default:
throw new IllegalStateException("Missed action for conflict strategy: " + aaaProperties.ldap().resolveConflictsStrategy());
Expand Down
4 changes: 2 additions & 2 deletions aaa/src/main/java/name/nkonev/aaa/tasks/SyncLdapTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import name.nkonev.aaa.repository.jdbc.UserAccountRepository;
import name.nkonev.aaa.security.AaaUserDetailsService;
import name.nkonev.aaa.security.RoleMapper;
import name.nkonev.aaa.services.ConflictResolingActions;
import name.nkonev.aaa.services.ConflictResolvingActions;
import name.nkonev.aaa.services.ConflictService;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.slf4j.Logger;
Expand Down Expand Up @@ -38,7 +38,7 @@
import static name.nkonev.aaa.utils.TimeUtil.getNowUTC;

@Service
public class SyncLdapTask implements ConflictResolingActions {
public class SyncLdapTask implements ConflictResolvingActions {
@Autowired
private AaaProperties aaaProperties;

Expand Down

0 comments on commit 5391d7d

Please sign in to comment.