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 b9465e4 commit 380a239
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ public Authentication authenticate(Authentication authentication) throws Authent
.orElseGet(() -> {
// create a new

// check conflict by username
userAccountRepository.findByUsername(userName).ifPresent(ua -> {
throw new UserAlreadyPresentException("User with login '" + userName + "' is already present");
});

String email = null;
if (StringUtils.hasLength(aaaProperties.ldap().attributeNames().email())) {
email = ldapEntry.email();
Expand All @@ -103,11 +98,12 @@ public Authentication authenticate(Authentication authentication) throws Authent
}
var mappedRoles = RoleMapper.map(aaaProperties.roleMappings().ldap(), rawRoles);

// check conflict by username
userService.checkLoginIsFreeOrThrow(userName);

// check conflict by email
if (StringUtils.hasLength(email)) {
if (!userService.checkEmailIsFree(email)){
throw new UserAlreadyPresentException("User with email '" + email + "' is already present");
}
userService.checkEmailIsFreeOrThrow(email);
}

var user = userAccountRepository.save(UserAccountConverter.buildUserAccountEntityForLdapInsert(
Expand Down
13 changes: 11 additions & 2 deletions aaa/src/main/java/name/nkonev/aaa/services/CheckService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public class CheckService {
@Autowired
private UserAccountRepository userAccountRepository;

public void checkLoginIsFree(String newLogin) {
if(userAccountRepository.findByUsername(newLogin).isPresent()){
public boolean checkLoginIsFree(String newLogin) {
return userAccountRepository.findByUsername(newLogin).isEmpty();
}

public void checkLoginIsFreeOrThrow(String newLogin) {
if (!checkLoginIsFree(newLogin)){
throw new UserAlreadyPresentException("User with login '" + newLogin + "' is already present");
}
}
Expand All @@ -29,4 +33,9 @@ public boolean checkEmailIsFree(String email) {
}
}

public void checkEmailIsFreeOrThrow(String email) {
if (checkEmailIsFree(email)) {
throw new UserAlreadyPresentException("User with email '" + email + "' is already present");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void register(EditUserDTO editUserDTO, Language language, String referer,
validateLengthEmail(userAccountDTO.email());

var userAccountOuter = transactionTemplate.execute(status -> {
userService.checkLoginIsFree(userAccountDTO.login());
userService.checkLoginIsFreeOrThrow(userAccountDTO.login());

if (!userService.checkEmailIsFree(userAccountDTO.email())){
return null; // we care for user email leak
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public UserSelfProfileDTO editNonEmpty(

// check login already present
if (userAccountDTO.login() != null && !exists.username().equals(userAccountDTO.login())) {
checkService.checkLoginIsFree(userAccountDTO.login());
checkService.checkLoginIsFreeOrThrow(userAccountDTO.login());
}

var resp = userAccountConverter.updateUserAccountEntityNotEmpty(userAccountDTO, exists, passwordEncoder);
Expand Down
2 changes: 1 addition & 1 deletion aaa/src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ custom.ldap:
filter: "uid={0}"
attribute-names:
id: uidNumber # name of attribute, which is considered as ldap_id. any id-like attribute, which won't be changed on user rename, it can be number or string
role: memberOf
role: ""
email: ""
locked: ""
username: uid
Expand Down

0 comments on commit 380a239

Please sign in to comment.