Search in sources :

Example 1 with UserType

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;
}
Also used : Project(com.epam.ta.reportportal.entity.project.Project) PersonalProjectService(com.epam.ta.reportportal.util.PersonalProjectService) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) ErrorType(com.epam.ta.reportportal.ws.model.ErrorType) StringUtils(org.apache.commons.lang3.StringUtils) Strings(com.google.common.base.Strings) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) UserRole(com.epam.ta.reportportal.entity.user.UserRole) ZoneOffset(java.time.ZoneOffset) UserType(com.epam.ta.reportportal.entity.user.UserType) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) Resource(org.springframework.core.io.Resource) BinaryData(com.epam.ta.reportportal.entity.attachment.BinaryData) AbstractUserReplicator(com.epam.reportportal.auth.integration.AbstractUserReplicator) ContentTypeResolver(com.epam.reportportal.commons.ContentTypeResolver) BusinessRule(com.epam.ta.reportportal.commons.validation.BusinessRule) Optional.ofNullable(java.util.Optional.ofNullable) User(com.epam.ta.reportportal.entity.user.User) UserRepository(com.epam.ta.reportportal.dao.UserRepository) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Objects(java.util.Objects) Metadata(com.epam.ta.reportportal.entity.Metadata) Component(org.springframework.stereotype.Component) EntityUtils.normalizeId(com.epam.ta.reportportal.commons.EntityUtils.normalizeId) ProjectRepository(com.epam.ta.reportportal.dao.ProjectRepository) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) UserBinaryDataService(com.epam.ta.reportportal.binary.UserBinaryDataService) Transactional(org.springframework.transaction.annotation.Transactional) InputStream(java.io.InputStream) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) User(com.epam.ta.reportportal.entity.user.User) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) Metadata(com.epam.ta.reportportal.entity.Metadata)

Aggregations

AbstractUserReplicator (com.epam.reportportal.auth.integration.AbstractUserReplicator)1 UserSynchronizationException (com.epam.reportportal.auth.oauth.UserSynchronizationException)1 ContentTypeResolver (com.epam.reportportal.commons.ContentTypeResolver)1 UserBinaryDataService (com.epam.ta.reportportal.binary.UserBinaryDataService)1 EntityUtils.normalizeId (com.epam.ta.reportportal.commons.EntityUtils.normalizeId)1 ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)1 BusinessRule (com.epam.ta.reportportal.commons.validation.BusinessRule)1 ProjectRepository (com.epam.ta.reportportal.dao.ProjectRepository)1 UserRepository (com.epam.ta.reportportal.dao.UserRepository)1 Metadata (com.epam.ta.reportportal.entity.Metadata)1 BinaryData (com.epam.ta.reportportal.entity.attachment.BinaryData)1 Project (com.epam.ta.reportportal.entity.project.Project)1 User (com.epam.ta.reportportal.entity.user.User)1 UserRole (com.epam.ta.reportportal.entity.user.UserRole)1 UserType (com.epam.ta.reportportal.entity.user.UserType)1 PersonalProjectService (com.epam.ta.reportportal.util.PersonalProjectService)1 ErrorType (com.epam.ta.reportportal.ws.model.ErrorType)1 Strings (com.google.common.base.Strings)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Maps (com.google.common.collect.Maps)1