Search in sources :

Example 1 with Project

use of com.epam.ta.reportportal.entity.project.Project in project commons-dao by reportportal.

the class UserRepositoryTest method createUserTest.

@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void createUserTest() {
    User reg = new User();
    reg.setEmail("email.com");
    reg.setFullName("test");
    reg.setLogin("created");
    reg.setPassword("new");
    reg.setUserType(UserType.INTERNAL);
    reg.setRole(UserRole.USER);
    Map<String, Object> map = new HashMap<>();
    map.put("last_login", new Date());
    reg.setMetadata(new Metadata(map));
    Project defaultProject = projectRepository.findByName("superadmin_personal").get();
    Set<ProjectUser> projectUsers = defaultProject.getUsers();
    projectUsers.add(new ProjectUser().withProjectRole(ProjectRole.CUSTOMER).withUser(reg).withProject(defaultProject));
    defaultProject.setUsers(projectUsers);
    userRepository.save(reg);
    final Optional<User> created = userRepository.findByLogin("created");
    assertTrue(created.isPresent());
}
Also used : Project(com.epam.ta.reportportal.entity.project.Project) ProjectUser(com.epam.ta.reportportal.entity.user.ProjectUser) ProjectUser(com.epam.ta.reportportal.entity.user.ProjectUser) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) User(com.epam.ta.reportportal.entity.user.User) Metadata(com.epam.ta.reportportal.entity.Metadata) BaseTest(com.epam.ta.reportportal.BaseTest) Test(org.junit.jupiter.api.Test)

Example 2 with Project

use of com.epam.ta.reportportal.entity.project.Project in project commons-dao by reportportal.

the class PersonalProjectService method generatePersonalProject.

/**
 * Generates personal project for provided user
 *
 * @param user User project should be created for
 * @return Built Project object
 */
public Project generatePersonalProject(User user) {
    Project project = new Project();
    project.setName(generatePersonalProjectName(user.getLogin()));
    project.setCreationDate(Date.from(ZonedDateTime.now().toInstant()));
    project.setProjectType(ProjectType.PERSONAL);
    ProjectUser projectUser = new ProjectUser().withUser(user).withProjectRole(ProjectRole.PROJECT_MANAGER).withProject(project);
    project.setUsers(Sets.newHashSet(projectUser));
    project.setMetadata(new Metadata(Collections.singletonMap("additional_info", "Personal project of " + (isNullOrEmpty(user.getFullName()) ? user.getLogin() : user.getFullName()))));
    project.setProjectAttributes(defaultProjectAttributes(project, attributeRepository.getDefaultProjectAttributes()));
    project.setProjectIssueTypes(defaultIssueTypes(project, issueTypeRepository.getDefaultIssueTypes()));
    return project;
}
Also used : Project(com.epam.ta.reportportal.entity.project.Project) ProjectUser(com.epam.ta.reportportal.entity.user.ProjectUser) Metadata(com.epam.ta.reportportal.entity.Metadata)

Example 3 with Project

use of com.epam.ta.reportportal.entity.project.Project in project service-authorization by reportportal.

the class GitHubUserReplicator method replicateUser.

/**
 * Replicates GitHub user to internal database (if does NOT exist). Updates if exist. Creates personal project for that user
 *
 * @param userResource GitHub user to be replicated
 * @param gitHubClient Configured github client
 * @return Internal User representation
 */
@Transactional
public ReportPortalUser replicateUser(UserResource userResource, GitHubClient gitHubClient) {
    String login = normalizeId(userResource.getLogin());
    User user = userRepository.findByLogin(login).map(u -> {
        if (UserType.GITHUB.equals(u.getUserType())) {
            updateUser(u, userResource, gitHubClient);
        } else {
            // if user with such login exists, but it's not GitHub user than throw an exception
            throw new UserSynchronizationException("User with login '" + u.getLogin() + "' already exists");
        }
        return u;
    }).orElseGet(() -> userRepository.save(createUser(userResource, gitHubClient)));
    return ReportPortalUser.userBuilder().fromUser(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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Project

use of com.epam.ta.reportportal.entity.project.Project in project service-authorization by reportportal.

the class UiAuthenticationSuccessEventHandler method onApplicationEvent.

@EventListener
@Transactional
public void onApplicationEvent(UiUserSignedInEvent event) {
    String username = event.getAuthentication().getName();
    userRepository.updateLastLoginDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getTimestamp()), ZoneOffset.UTC), username);
    if (MapUtils.isEmpty(acquireUser(event.getAuthentication()).getProjectDetails())) {
        User user = userRepository.findByLogin(username).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));
        Project project = personalProjectService.generatePersonalProject(user);
        user.getProjects().addAll(project.getUsers());
    }
}
Also used : Project(com.epam.ta.reportportal.entity.project.Project) User(com.epam.ta.reportportal.entity.user.User) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) EventListener(org.springframework.context.event.EventListener) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Project

use of com.epam.ta.reportportal.entity.project.Project 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 syncAttributes Synchronization Attributes
 * @return Internal User representation
 */
@Transactional
public User replicateUser(String name, DirContextOperations ctx, Map<String, String> syncAttributes) {
    String emailAttribute = ofNullable(syncAttributes.get(LdapParameter.EMAIL_ATTRIBUTE.getParameterName())).orElseThrow(() -> new UserSynchronizationException("Email attribute not provided"));
    String email = (String) ctx.getObjectAttribute(emailAttribute);
    if (isNullOrEmpty(email)) {
        throw new UserSynchronizationException("Email not provided");
    }
    email = normalizeId(email);
    String login = CROP_DOMAIN.apply(name);
    Optional<User> userOptional = userRepository.findByLogin(login);
    if (userOptional.isEmpty()) {
        User newUser = new User();
        newUser.setLogin(login);
        ofNullable(syncAttributes.get(LdapParameter.FULL_NAME_ATTRIBUTE.getParameterName())).filter(StringUtils::isNotBlank).flatMap(it -> ofNullable(ctx.getStringAttribute(it))).ifPresent(newUser::setFullName);
        ofNullable(syncAttributes.get(LdapParameter.PHOTO_ATTRIBUTE.getParameterName())).filter(StringUtils::isNotBlank).flatMap(it -> ofNullable(ctx.getObjectAttribute(it)));
        checkEmail(email);
        newUser.setEmail(email);
        newUser.setMetadata(defaultMetaData());
        newUser.setUserType(UserType.LDAP);
        newUser.setRole(UserRole.USER);
        newUser.setExpired(false);
        final Project project = generatePersonalProject(newUser);
        newUser.getProjects().add(project.getUsers().iterator().next());
        userRepository.save(newUser);
        return newUser;
    } else if (!UserType.LDAP.equals(userOptional.get().getUserType())) {
        // if user with such login exists, but it's not GitHub user than throw an exception
        throw new UserSynchronizationException("User with login '" + userOptional.get().getLogin() + "' already exists");
    }
    return userOptional.get();
}
Also used : AbstractUserReplicator(com.epam.reportportal.auth.integration.AbstractUserReplicator) ContentTypeResolver(com.epam.reportportal.commons.ContentTypeResolver) DirContextOperations(org.springframework.ldap.core.DirContextOperations) StringUtils(org.apache.commons.lang.StringUtils) Project(com.epam.ta.reportportal.entity.project.Project) PersonalProjectService(com.epam.ta.reportportal.util.PersonalProjectService) Optional.ofNullable(java.util.Optional.ofNullable) User(com.epam.ta.reportportal.entity.user.User) UserRepository(com.epam.ta.reportportal.dao.UserRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) LdapParameter(com.epam.reportportal.auth.integration.parameter.LdapParameter) Component(org.springframework.stereotype.Component) EntityUtils.normalizeId(com.epam.ta.reportportal.commons.EntityUtils.normalizeId) ProjectRepository(com.epam.ta.reportportal.dao.ProjectRepository) UserRole(com.epam.ta.reportportal.entity.user.UserRole) Map(java.util.Map) Optional(java.util.Optional) UserType(com.epam.ta.reportportal.entity.user.UserType) UserBinaryDataService(com.epam.ta.reportportal.binary.UserBinaryDataService) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) CROP_DOMAIN(com.epam.reportportal.auth.util.AuthUtils.CROP_DOMAIN) Transactional(org.springframework.transaction.annotation.Transactional) Project(com.epam.ta.reportportal.entity.project.Project) User(com.epam.ta.reportportal.entity.user.User) StringUtils(org.apache.commons.lang.StringUtils) UserSynchronizationException(com.epam.reportportal.auth.oauth.UserSynchronizationException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Project (com.epam.ta.reportportal.entity.project.Project)8 User (com.epam.ta.reportportal.entity.user.User)6 ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AbstractUserReplicator (com.epam.reportportal.auth.integration.AbstractUserReplicator)3 ContentTypeResolver (com.epam.reportportal.commons.ContentTypeResolver)3 UserBinaryDataService (com.epam.ta.reportportal.binary.UserBinaryDataService)3 ProjectRepository (com.epam.ta.reportportal.dao.ProjectRepository)3 UserRepository (com.epam.ta.reportportal.dao.UserRepository)3 Metadata (com.epam.ta.reportportal.entity.Metadata)3 UserRole (com.epam.ta.reportportal.entity.user.UserRole)3 UserType (com.epam.ta.reportportal.entity.user.UserType)3 PersonalProjectService (com.epam.ta.reportportal.util.PersonalProjectService)3 Optional (java.util.Optional)3 Component (org.springframework.stereotype.Component)3 UserSynchronizationException (com.epam.reportportal.auth.oauth.UserSynchronizationException)2 CROP_DOMAIN (com.epam.reportportal.auth.util.AuthUtils.CROP_DOMAIN)2 BaseTest (com.epam.ta.reportportal.BaseTest)2 EntityUtils.normalizeId (com.epam.ta.reportportal.commons.EntityUtils.normalizeId)2 Integration (com.epam.ta.reportportal.entity.integration.Integration)2