Search in sources :

Example 1 with UserSynchronizationException

use of com.epam.reportportal.auth.oauth.UserSynchronizationException in project service-authorization by reportportal.

the class GitHubUserReplicator method replicateUser.

/**
 * Replicates GitHub user to internal database (if does NOT exist). Creates personal project for that user
 *
 * @param userInfo     GitHub user to be replicated
 * @param gitHubClient Configured github client
 * @return Internal User representation
 */
public User replicateUser(UserResource userInfo, GitHubClient gitHubClient) {
    String login = normalizeId(userInfo.login);
    User user = userRepository.findOne(login);
    if (null == user) {
        user = new User();
        user.setLogin(login);
        String email = userInfo.email;
        if (Strings.isNullOrEmpty(email)) {
            email = gitHubClient.getUserEmails().stream().filter(EmailResource::isVerified).filter(EmailResource::isPrimary).findAny().get().getEmail();
        }
        email = normalizeId(email);
        checkEmail(email);
        user.setEmail(email);
        if (!Strings.isNullOrEmpty(userInfo.name)) {
            user.setFullName(userInfo.name);
        }
        user.setMetaInfo(defaultMetaInfo());
        user.setType(UserType.GITHUB);
        user.setRole(UserRole.USER);
        Object avatarUrl = userInfo.avatarUrl;
        user.setPhotoId(uploadAvatar(gitHubClient, login, avatarUrl));
        user.setIsExpired(false);
        user.setDefaultProject(generatePersonalProject(user));
        userRepository.save(user);
    } else if (!UserType.GITHUB.equals(user.getType())) {
        // if user with such login exists, but it's not GitHub user than throw an exception
        throw new UserSynchronizationException("User with login '" + user.getId() + "' already exists");
    }
    return user;
}
Also used : User(com.epam.ta.reportportal.database.entity.user.User) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException)

Example 2 with UserSynchronizationException

use of com.epam.reportportal.auth.oauth.UserSynchronizationException in project service-authorization by reportportal.

the class LdapUserReplicator method replicateUser.

/**
 * Replicates LDAP user to internal database (if does NOT exist). Creates personal project for that user
 *
 * @param name       Username
 * @param ctx        LDAP context
 * @param attributes Synchronization Attributes
 * @return Internal User representation
 */
public User replicateUser(String name, DirContextOperations ctx, SynchronizationAttributes attributes) {
    String email = (String) ctx.getObjectAttribute(attributes.getEmail());
    if (isNullOrEmpty(email)) {
        throw new UserSynchronizationException("Email not provided");
    }
    email = normalizeId(email);
    String login = normalizeId(name);
    User user = userRepository.findOne(login);
    if (null == user) {
        User newUser = new User();
        newUser.setLogin(login);
        ofNullable(attributes.getFullName()).flatMap(it -> ofNullable(ctx.getStringAttribute(it))).ifPresent(newUser::setFullName);
        ofNullable(attributes.getPhoto()).flatMap(it -> ofNullable(ctx.getObjectAttribute(it))).filter(photo -> photo instanceof byte[]).map(photo -> (byte[]) photo).ifPresent(photo -> newUser.setPhotoId(uploadPhoto(login, photo)));
        checkEmail(email);
        newUser.setEmail(email);
        newUser.setMetaInfo(defaultMetaInfo());
        newUser.setType(UserType.LDAP);
        newUser.setRole(UserRole.USER);
        newUser.setIsExpired(false);
        newUser.setDefaultProject(generatePersonalProject(newUser));
        userRepository.save(newUser);
        user = newUser;
    } else if (!UserType.LDAP.equals(user.getType())) {
        // if user with such login exists, but it's not GitHub user than throw an exception
        throw new UserSynchronizationException("User with login '" + user.getId() + "' already exists");
    }
    return user;
}
Also used : AbstractUserReplicator(com.epam.reportportal.auth.integration.AbstractUserReplicator) DirContextOperations(org.springframework.ldap.core.DirContextOperations) DataStorage(com.epam.ta.reportportal.database.DataStorage) Optional.ofNullable(java.util.Optional.ofNullable) PersonalProjectService(com.epam.ta.reportportal.database.personal.PersonalProjectService) ProjectRepository(com.epam.ta.reportportal.database.dao.ProjectRepository) UserRepository(com.epam.ta.reportportal.database.dao.UserRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) UserRole(com.epam.ta.reportportal.database.entity.user.UserRole) Component(org.springframework.stereotype.Component) EntityUtils.normalizeId(com.epam.ta.reportportal.commons.EntityUtils.normalizeId) SynchronizationAttributes(com.epam.reportportal.auth.store.entity.ldap.SynchronizationAttributes) User(com.epam.ta.reportportal.database.entity.user.User) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) UserType(com.epam.ta.reportportal.database.entity.user.UserType) User(com.epam.ta.reportportal.database.entity.user.User) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException)

Aggregations

UserSynchronizationException (com.epam.reportportal.auth.oauth.UserSynchronizationException)2 User (com.epam.ta.reportportal.database.entity.user.User)2 AbstractUserReplicator (com.epam.reportportal.auth.integration.AbstractUserReplicator)1 SynchronizationAttributes (com.epam.reportportal.auth.store.entity.ldap.SynchronizationAttributes)1 EntityUtils.normalizeId (com.epam.ta.reportportal.commons.EntityUtils.normalizeId)1 DataStorage (com.epam.ta.reportportal.database.DataStorage)1 ProjectRepository (com.epam.ta.reportportal.database.dao.ProjectRepository)1 UserRepository (com.epam.ta.reportportal.database.dao.UserRepository)1 UserRole (com.epam.ta.reportportal.database.entity.user.UserRole)1 UserType (com.epam.ta.reportportal.database.entity.user.UserType)1 PersonalProjectService (com.epam.ta.reportportal.database.personal.PersonalProjectService)1 Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 DirContextOperations (org.springframework.ldap.core.DirContextOperations)1 Component (org.springframework.stereotype.Component)1