use of com.epam.ta.reportportal.entity.user.UserType in project service-authorization by reportportal.
the class GitHubUserReplicator method synchronizeUser.
public User synchronizeUser(String accessToken) {
GitHubClient gitHubClient = GitHubClient.withAccessToken(accessToken);
UserResource userResource = gitHubClient.getUser();
Optional<User> userOptional = userRepository.findByLogin(normalizeId(userResource.getLogin()));
BusinessRule.expect(userOptional, Optional::isPresent).verify(ErrorType.USER_NOT_FOUND, userResource.getLogin());
User user = userOptional.get();
BusinessRule.expect(user.getUserType(), userType -> Objects.equals(userType, UserType.GITHUB)).verify(ErrorType.INCORRECT_AUTHENTICATION_TYPE, "User '" + userResource.getLogin() + "' is not GitHUB user");
if (StringUtils.isNotBlank(userResource.getName())) {
user.setFullName(userResource.getName());
}
String email = userResource.getEmail();
if (Strings.isNullOrEmpty(email)) {
email = retrieveEmail(gitHubClient).orElseThrow(() -> new UserSynchronizationException("User 'email' has not been provided"));
}
email = normalizeId(email);
if (!user.getEmail().equals(email)) {
checkEmail(email);
user.setEmail(email);
}
Metadata metadata = ofNullable(user.getMetadata()).orElse(new Metadata(Maps.newHashMap()));
metadata.getMetadata().put("synchronizationDate", Date.from(ZonedDateTime.now(ZoneOffset.UTC).toInstant()));
user.setMetadata(metadata);
uploadAvatar(gitHubClient, user, userResource.getAvatarUrl());
userRepository.save(user);
return user;
}
Aggregations