Search in sources :

Example 1 with ProjectRole

use of com.epam.ta.reportportal.entity.project.ProjectRole in project service-api by reportportal.

the class UpdateProjectHandlerImpl method updateProjectUserRoles.

private void updateProjectUserRoles(Map<String, String> userRoles, Project project, ReportPortalUser user) {
    if (!user.getUserRole().equals(UserRole.ADMINISTRATOR)) {
        expect(userRoles.get(user.getUsername()), isNull()).verify(ErrorType.UNABLE_TO_UPDATE_YOURSELF_ROLE, user.getUsername());
    }
    if (MapUtils.isNotEmpty(userRoles)) {
        userRoles.forEach((key, value) -> {
            Optional<ProjectRole> newProjectRole = ProjectRole.forName(value);
            expect(newProjectRole, isPresent()).verify(ErrorType.ROLE_NOT_FOUND, value);
            Optional<ProjectUser> updatingProjectUser = ofNullable(ProjectUtils.findUserConfigByLogin(project, key));
            expect(updatingProjectUser, isPresent()).verify(ErrorType.USER_NOT_FOUND, key);
            if (UserRole.ADMINISTRATOR != user.getUserRole()) {
                ProjectRole principalRole = projectExtractor.extractProjectDetails(user, project.getName()).getProjectRole();
                ProjectRole updatingUserRole = ofNullable(ProjectUtils.findUserConfigByLogin(project, key)).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, key)).getProjectRole();
                /*
					 * Validate principal role level is high enough
					 */
                if (principalRole.sameOrHigherThan(updatingUserRole)) {
                    expect(newProjectRole.get(), Preconditions.isLevelEnough(principalRole)).verify(ErrorType.ACCESS_DENIED);
                } else {
                    expect(updatingUserRole, Preconditions.isLevelEnough(principalRole)).verify(ErrorType.ACCESS_DENIED);
                }
            }
            updatingProjectUser.get().setProjectRole(newProjectRole.get());
        });
    }
}
Also used : ProjectUser(com.epam.ta.reportportal.entity.user.ProjectUser) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) ProjectRole(com.epam.ta.reportportal.entity.project.ProjectRole)

Example 2 with ProjectRole

use of com.epam.ta.reportportal.entity.project.ProjectRole in project service-api by reportportal.

the class BaseProjectPermission method isAllowed.

/**
 * Validates project exists and user assigned to project. After that
 * delegates permission check to subclass
 */
@Override
public boolean isAllowed(Authentication authentication, Object projectName) {
    if (!authentication.isAuthenticated()) {
        return false;
    }
    OAuth2Authentication oauth = (OAuth2Authentication) authentication;
    ReportPortalUser rpUser = (ReportPortalUser) oauth.getUserAuthentication().getPrincipal();
    BusinessRule.expect(rpUser, Objects::nonNull).verify(ErrorType.ACCESS_DENIED);
    final String resolvedProjectName = String.valueOf(projectName);
    final ReportPortalUser.ProjectDetails projectDetails = projectExtractor.findProjectDetails(rpUser, resolvedProjectName).orElseThrow(() -> new ReportPortalException(ErrorType.ACCESS_DENIED));
    fillProjectDetails(rpUser, resolvedProjectName, projectDetails);
    ProjectRole role = projectDetails.getProjectRole();
    return checkAllowed(rpUser, projectName.toString(), role);
}
Also used : ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) ProjectRole(com.epam.ta.reportportal.entity.project.ProjectRole)

Example 3 with ProjectRole

use of com.epam.ta.reportportal.entity.project.ProjectRole in project service-api by reportportal.

the class CreateUserHandlerImpl method saveUser.

private Pair<UserActivityResource, CreateUserRS> saveUser(CreateUserRQFull request) {
    final Project projectToAssign = getProjectHandler.getRaw(normalizeId(request.getDefaultProject()));
    final ProjectRole projectRole = forName(request.getProjectRole()).orElseThrow(() -> new ReportPortalException(ROLE_NOT_FOUND, request.getProjectRole()));
    final User user = convert(request);
    try {
        userRepository.save(user);
    } catch (PersistenceException pe) {
        if (pe.getCause() instanceof ConstraintViolationException) {
            fail().withError(RESOURCE_ALREADY_EXISTS, ((ConstraintViolationException) pe.getCause()).getConstraintName());
        }
        throw new ReportPortalException("Error while User creating: " + pe.getMessage(), pe);
    } catch (Exception exp) {
        throw new ReportPortalException("Error while User creating: " + exp.getMessage(), exp);
    }
    userAuthenticator.authenticate(user);
    projectUserHandler.assign(user, projectToAssign, projectRole);
    final Project personalProject = createProjectHandler.createPersonal(user);
    projectUserHandler.assign(user, personalProject, ProjectRole.PROJECT_MANAGER);
    final CreateUserRS response = new CreateUserRS();
    response.setId(user.getId());
    response.setLogin(user.getLogin());
    return Pair.of(TO_ACTIVITY_RESOURCE.apply(user, projectToAssign.getId()), response);
}
Also used : Project(com.epam.ta.reportportal.entity.project.Project) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ProjectRole(com.epam.ta.reportportal.entity.project.ProjectRole) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) PersistenceException(javax.persistence.PersistenceException)

Example 4 with ProjectRole

use of com.epam.ta.reportportal.entity.project.ProjectRole in project service-api by reportportal.

the class UpdateProjectHandlerImpl method assignUsers.

@Override
public OperationCompletionRS assignUsers(String projectName, AssignUsersRQ assignUsersRQ, ReportPortalUser user) {
    if (UserRole.ADMINISTRATOR.equals(user.getUserRole())) {
        Project project = projectRepository.findByName(normalizeId(projectName)).orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, normalizeId(projectName)));
        List<String> assignedUsernames = project.getUsers().stream().map(u -> u.getUser().getLogin()).collect(toList());
        assignUsersRQ.getUserNames().forEach((name, role) -> {
            ProjectRole projectRole = ProjectRole.forName(role).orElseThrow(() -> new ReportPortalException(ROLE_NOT_FOUND, role));
            assignUser(name, projectRole, assignedUsernames, project);
        });
    } else {
        expect(assignUsersRQ.getUserNames().keySet(), not(Preconditions.contains(equalTo(user.getUsername())))).verify(UNABLE_ASSIGN_UNASSIGN_USER_TO_PROJECT, "User should not assign himself to project.");
        ReportPortalUser.ProjectDetails projectDetails = projectExtractor.extractProjectDetails(user, projectName);
        Project project = projectRepository.findById(projectDetails.getProjectId()).orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, normalizeId(projectName)));
        List<String> assignedUsernames = project.getUsers().stream().map(u -> u.getUser().getLogin()).collect(toList());
        assignUsersRQ.getUserNames().forEach((name, role) -> {
            ProjectRole projectRole = ProjectRole.forName(role).orElseThrow(() -> new ReportPortalException(ROLE_NOT_FOUND, role));
            ProjectRole modifierRole = projectDetails.getProjectRole();
            expect(modifierRole.sameOrHigherThan(projectRole), BooleanUtils::isTrue).verify(ACCESS_DENIED);
            assignUser(name, projectRole, assignedUsernames, project);
        });
    }
    return new OperationCompletionRS("User(s) with username='" + assignUsersRQ.getUserNames().keySet() + "' was successfully assigned to project='" + normalizeId(projectName) + "'");
}
Also used : SenderCase(com.epam.ta.reportportal.entity.project.email.SenderCase) OperationCompletionRS(com.epam.ta.reportportal.ws.model.OperationCompletionRS) Autowired(org.springframework.beans.factory.annotation.Autowired) ProjectAttributesActivityResource(com.epam.ta.reportportal.ws.model.activity.ProjectAttributesActivityResource) ProjectAttributeEnum(com.epam.ta.reportportal.entity.enums.ProjectAttributeEnum) MessageBus(com.epam.ta.reportportal.core.events.MessageBus) ProjectResource(com.epam.ta.reportportal.ws.model.project.ProjectResource) Predicates(com.epam.ta.reportportal.commons.Predicates) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) UserType(com.epam.ta.reportportal.entity.user.UserType) Preconditions(com.epam.ta.reportportal.commons.Preconditions) ProjectAnalyzerConfigEvent(com.epam.ta.reportportal.core.events.activity.ProjectAnalyzerConfigEvent) ProjectIndexEvent(com.epam.ta.reportportal.core.events.activity.ProjectIndexEvent) Suppliers.formattedSupplier(com.epam.ta.reportportal.commons.validation.Suppliers.formattedSupplier) Collectors.toSet(java.util.stream.Collectors.toSet) NotificationConfigConverter(com.epam.ta.reportportal.ws.converter.converters.NotificationConfigConverter) ShareableObjectsHandler(com.epam.ta.reportportal.auth.acl.ShareableObjectsHandler) LogIndexer(com.epam.ta.reportportal.core.analyzer.auto.LogIndexer) UpdateProjectRQ(com.epam.ta.reportportal.ws.model.project.UpdateProjectRQ) Predicate(java.util.function.Predicate) BusinessRule.expect(com.epam.ta.reportportal.commons.validation.BusinessRule.expect) ProjectEvent(com.epam.reportportal.extension.event.ProjectEvent) UserRepository(com.epam.ta.reportportal.dao.UserRepository) ProjectUtils(com.epam.ta.reportportal.entity.project.ProjectUtils) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) Collectors(java.util.stream.Collectors) AnalyzerUtils(com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerUtils) ProjectRole(com.epam.ta.reportportal.entity.project.ProjectRole) NotificationsConfigUpdatedEvent(com.epam.ta.reportportal.core.events.activity.NotificationsConfigUpdatedEvent) EmailRulesValidator(com.epam.ta.reportportal.util.email.EmailRulesValidator) AUTO_ANALYZER_KEY(com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerStatusCache.AUTO_ANALYZER_KEY) ProjectConfigurationUpdate(com.epam.ta.reportportal.ws.model.project.config.ProjectConfigurationUpdate) SendCase.findByName(com.epam.ta.reportportal.entity.enums.SendCase.findByName) IndexerStatusCache(com.epam.ta.reportportal.core.analyzer.auto.indexer.IndexerStatusCache) java.util(java.util) Project(com.epam.ta.reportportal.entity.project.Project) ProjectUser(com.epam.ta.reportportal.entity.user.ProjectUser) Preconditions.contains(com.epam.ta.reportportal.commons.Preconditions.contains) BooleanUtils(org.apache.commons.lang3.BooleanUtils) TO_ACTIVITY_RESOURCE(com.epam.ta.reportportal.ws.converter.converters.ProjectActivityConverter.TO_ACTIVITY_RESOURCE) ProjectConverter(com.epam.ta.reportportal.ws.converter.converters.ProjectConverter) ErrorType(com.epam.ta.reportportal.ws.model.ErrorType) AssignUsersRQ(com.epam.ta.reportportal.ws.model.project.AssignUsersRQ) UpdateProjectHandler(com.epam.ta.reportportal.core.project.UpdateProjectHandler) ProjectUpdatedEvent(com.epam.ta.reportportal.core.events.activity.ProjectUpdatedEvent) ProjectType(com.epam.ta.reportportal.entity.enums.ProjectType) ProjectExtractor(com.epam.ta.reportportal.util.ProjectExtractor) MailServiceFactory(com.epam.ta.reportportal.util.email.MailServiceFactory) Lists(com.google.common.collect.Lists) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) CollectionUtils(org.apache.commons.collections.CollectionUtils) Service(org.springframework.stereotype.Service) UserRole(com.epam.ta.reportportal.entity.user.UserRole) BasePermission(org.springframework.security.acls.domain.BasePermission) AnalyzerStatusCache(com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerStatusCache) UserPreferenceRepository(com.epam.ta.reportportal.dao.UserPreferenceRepository) SenderCaseDTO(com.epam.ta.reportportal.ws.model.project.email.SenderCaseDTO) ProjectAttributeValidator(com.epam.ta.reportportal.core.project.validator.attribute.ProjectAttributeValidator) MapUtils(org.apache.commons.collections.MapUtils) Optional.ofNullable(java.util.Optional.ofNullable) ProjectUserRepository(com.epam.ta.reportportal.dao.ProjectUserRepository) User(com.epam.ta.reportportal.entity.user.User) UnassignUsersRQ(com.epam.ta.reportportal.ws.model.project.UnassignUsersRQ) EntityUtils.normalizeId(com.epam.ta.reportportal.commons.EntityUtils.normalizeId) Collectors.toList(java.util.stream.Collectors.toList) ProjectRepository(com.epam.ta.reportportal.dao.ProjectRepository) BusinessRule.fail(com.epam.ta.reportportal.commons.validation.BusinessRule.fail) Cache(com.google.common.cache.Cache) AnalyzerServiceClient(com.epam.ta.reportportal.core.analyzer.auto.client.AnalyzerServiceClient) ProjectNotificationConfigDTO(com.epam.ta.reportportal.ws.model.project.email.ProjectNotificationConfigDTO) Project(com.epam.ta.reportportal.entity.project.Project) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) ProjectRole(com.epam.ta.reportportal.entity.project.ProjectRole) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) OperationCompletionRS(com.epam.ta.reportportal.ws.model.OperationCompletionRS)

Aggregations

ProjectRole (com.epam.ta.reportportal.entity.project.ProjectRole)4 ReportPortalException (com.epam.ta.reportportal.exception.ReportPortalException)4 ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)3 Project (com.epam.ta.reportportal.entity.project.Project)2 ProjectUser (com.epam.ta.reportportal.entity.user.ProjectUser)2 ProjectEvent (com.epam.reportportal.extension.event.ProjectEvent)1 ShareableObjectsHandler (com.epam.ta.reportportal.auth.acl.ShareableObjectsHandler)1 EntityUtils.normalizeId (com.epam.ta.reportportal.commons.EntityUtils.normalizeId)1 Preconditions (com.epam.ta.reportportal.commons.Preconditions)1 Preconditions.contains (com.epam.ta.reportportal.commons.Preconditions.contains)1 Predicates (com.epam.ta.reportportal.commons.Predicates)1 BusinessRule.expect (com.epam.ta.reportportal.commons.validation.BusinessRule.expect)1 BusinessRule.fail (com.epam.ta.reportportal.commons.validation.BusinessRule.fail)1 Suppliers.formattedSupplier (com.epam.ta.reportportal.commons.validation.Suppliers.formattedSupplier)1 LogIndexer (com.epam.ta.reportportal.core.analyzer.auto.LogIndexer)1 AnalyzerServiceClient (com.epam.ta.reportportal.core.analyzer.auto.client.AnalyzerServiceClient)1 AnalyzerStatusCache (com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerStatusCache)1 AUTO_ANALYZER_KEY (com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerStatusCache.AUTO_ANALYZER_KEY)1 AnalyzerUtils (com.epam.ta.reportportal.core.analyzer.auto.impl.AnalyzerUtils)1 IndexerStatusCache (com.epam.ta.reportportal.core.analyzer.auto.indexer.IndexerStatusCache)1