com.epam.reportportal.extension.ProjectMemberCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-api Show documentation
Show all versions of plugin-api Show documentation
EPAM Report portal. Plugin extension APIs
The newest version!
package com.epam.reportportal.extension;
import com.epam.reportportal.rules.commons.validation.BusinessRule;
import com.epam.reportportal.rules.commons.validation.Suppliers;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.dao.ProjectRepository;
import com.epam.ta.reportportal.entity.project.Project;
import com.epam.reportportal.rules.exception.ErrorType;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import static java.util.Optional.ofNullable;
/**
* @author Ivan Budayeu
*/
public abstract class ProjectMemberCommand extends AbstractRoleBasedCommand {
public static final String PROJECT_ID_PARAM = "projectId";
protected final ProjectRepository projectRepository;
protected ProjectMemberCommand(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}
@Override
public void validateRole(Map params) {
Long projectId = retrieveLong(params, PROJECT_ID_PARAM);
Project project = projectRepository.findById(projectId)
.orElseThrow(() -> new ReportPortalException(ErrorType.PROJECT_NOT_FOUND, projectId));
ReportPortalUser user = (ReportPortalUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
BusinessRule.expect(user, Objects::nonNull).verify(ErrorType.ACCESS_DENIED);
validatePermissions(user, project);
}
protected void validatePermissions(ReportPortalUser user, Project project) {
BusinessRule.expect(ofNullable(user.getProjectDetails()).flatMap(detailsMapping -> ofNullable(detailsMapping.get(project.getName()))),
Optional::isPresent
).verify(ErrorType.ACCESS_DENIED);
}
public static Long retrieveLong(Map params, String param) {
return ofNullable(params.get(param)).map(String::valueOf)
.map(ProjectMemberCommand::safeParseLong)
.orElseThrow(() -> new ReportPortalException(ErrorType.BAD_REQUEST_ERROR,
Suppliers.formattedSupplier("Parameter '{}' was not provided", param).get()
));
}
public static Long safeParseLong(String param) {
try {
return Long.parseLong(param);
} catch (NumberFormatException ex) {
throw new ReportPortalException(ErrorType.BAD_REQUEST_ERROR, ex.getMessage());
}
}
}