Skip to content

Commit

Permalink
KAA-1293: Move email and user existence check methods from Web UI to …
Browse files Browse the repository at this point in the history
…REST API method
  • Loading branch information
Pyshankov committed Sep 14, 2016
1 parent 52f0884 commit ffdac54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
Expand Up @@ -29,12 +29,9 @@


import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;


import static org.kaaproject.kaa.server.admin.services.util.Utils.checkEmailUniquieness; import static org.kaaproject.kaa.server.admin.services.util.Utils.checkFieldUniquieness;
import static org.kaaproject.kaa.server.admin.services.util.Utils.checkNotNull;
import static org.kaaproject.kaa.server.admin.services.util.Utils.checkEmailFormat;
import static org.kaaproject.kaa.server.admin.services.util.Utils.getCurrentUser; import static org.kaaproject.kaa.server.admin.services.util.Utils.getCurrentUser;
import static org.kaaproject.kaa.server.admin.shared.util.Utils.isEmpty; import static org.kaaproject.kaa.server.admin.shared.util.Utils.isEmpty;


Expand Down Expand Up @@ -107,8 +104,8 @@ public org.kaaproject.kaa.common.dto.admin.UserDto getUser(String userId) throws
public org.kaaproject.kaa.common.dto.admin.UserDto editUser(org.kaaproject.kaa.common.dto.admin.UserDto user) public org.kaaproject.kaa.common.dto.admin.UserDto editUser(org.kaaproject.kaa.common.dto.admin.UserDto user)
throws KaaAdminServiceException { throws KaaAdminServiceException {
try { try {
User stored = userFacade.findByUserName(user.getUsername());
boolean createNewUser = (stored == null); boolean createNewUser = (user.getId() == null);


String tempPassword = null; String tempPassword = null;
if (createNewUser) { if (createNewUser) {
Expand All @@ -121,22 +118,22 @@ public org.kaaproject.kaa.common.dto.admin.UserDto editUser(org.kaaproject.kaa.c
} }
} }


checkEmailFormat(user.getMail()); checkFieldUniquieness(

checkEmailUniquieness(
user.getMail(), user.getMail(),
userFacade.getAll().stream().map(u -> u.getMail()).collect(Collectors.toSet()) userFacade.getAll().stream().map(u -> u.getMail()).collect(Collectors.toSet())
); );


checkFieldUniquieness(
user.getUsername(),
userFacade.getAll().stream().map(u -> u.getUsername()).collect(Collectors.toSet())
);

CreateUserResult result = userFacade.saveUserDto(user, passwordEncoder); CreateUserResult result = userFacade.saveUserDto(user, passwordEncoder);
user.setExternalUid(result.getUserId().toString()); user.setExternalUid(result.getUserId().toString());
tempPassword = result.getPassword(); tempPassword = result.getPassword();
} else { } else {
if (isEmpty(user.getId())) { User stored = userFacade.findByUserName(user.getUsername());
controlService.getUsers().stream()
.filter(u -> u.getUsername().equals(user.getUsername())).findFirst()
.ifPresent(u -> user.setId(u.getId()));
}
user.setExternalUid(String.valueOf(stored.getId())); user.setExternalUid(String.valueOf(stored.getId()));
checkUserId(user.getId()); checkUserId(user.getId());


Expand Down
Expand Up @@ -77,11 +77,11 @@ public static <T> T checkNotNull(T reference) throws KaaAdminServiceException {
return reference; return reference;
} }


public static void checkEmailUniquieness(String email, Set<String> storedEmails) throws KaaAdminServiceException { public static void checkFieldUniquieness(String email, Set<String> storedEmails) throws KaaAdminServiceException {
checkNotNull(email); checkNotNull(email);
boolean isAdded = storedEmails.add(email); boolean isAdded = storedEmails.add(email);
if (!isAdded) { if (!isAdded) {
throw new KaaAdminServiceException("Entered email is already used by another user!", ServiceErrorCode.INVALID_ARGUMENTS); throw new KaaAdminServiceException("Entered property is already used by another user!", ServiceErrorCode.INVALID_ARGUMENTS);
} }
} }


Expand Down
Expand Up @@ -39,4 +39,4 @@ public interface UserService extends RemoteService {
void deleteUser(String userId) throws KaaAdminServiceException; void deleteUser(String userId) throws KaaAdminServiceException;


List<UserDto> findAllTenantAdminsByTenantId(String id) throws KaaAdminServiceException; List<UserDto> findAllTenantAdminsByTenantId(String id) throws KaaAdminServiceException;
} }

0 comments on commit ffdac54

Please sign in to comment.