mirror of
https://github.com/DependencyTrack/dependency-track.git
synced 2025-10-20 00:13:20 +00:00
Use projections / result class to determine ProjectVersions
Co-authored-by: Niklas <nscuro@protonmail.com> Signed-off-by: Walter de Boer <walterdeboer@dbso.nl>
This commit is contained in:
parent
c02fe38e7b
commit
8580c05d89
2 changed files with 52 additions and 16 deletions
|
@ -31,7 +31,6 @@ import alpine.resources.AlpineRequest;
|
|||
import com.github.packageurl.PackageURL;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.datanucleus.api.jdo.JDOQuery;
|
||||
import org.dependencytrack.auth.Permissions;
|
||||
import org.dependencytrack.event.IndexEvent;
|
||||
import org.dependencytrack.model.Analysis;
|
||||
|
@ -86,6 +85,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* Returns a list of all projects.
|
||||
* @return a List of Projects
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final boolean includeMetrics, final boolean excludeInactive, final boolean onlyRoot) {
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -132,6 +132,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* Returns a list of all projects.
|
||||
* @return a List of Projects
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final boolean includeMetrics) {
|
||||
return getProjects(includeMetrics, false, false);
|
||||
}
|
||||
|
@ -140,6 +141,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* Returns a list of all projects.
|
||||
* @return a List of Projects
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects() {
|
||||
return getProjects(false);
|
||||
}
|
||||
|
@ -149,6 +151,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* This method if designed NOT to provide paginated results.
|
||||
* @return a List of Projects
|
||||
*/
|
||||
@Override
|
||||
public List<Project> getAllProjects() {
|
||||
return getAllProjects(false);
|
||||
}
|
||||
|
@ -158,6 +161,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* This method if designed NOT to provide paginated results.
|
||||
* @return a List of Projects
|
||||
*/
|
||||
@Override
|
||||
public List<Project> getAllProjects(boolean excludeInactive) {
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
if (excludeInactive) {
|
||||
|
@ -172,6 +176,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param name the name of the Projects (required)
|
||||
* @return a List of Project objects
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final String name, final boolean excludeInactive, final boolean onlyRoot) {
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
if (orderBy == null) {
|
||||
|
@ -199,6 +204,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param uuid the uuid of the Project (required)
|
||||
* @return a Project object, or null if not found
|
||||
*/
|
||||
@Override
|
||||
public Project getProject(final String uuid) {
|
||||
final Project project = getObjectByUuid(Project.class, uuid, Project.FetchGroup.ALL.name());
|
||||
if (project != null) {
|
||||
|
@ -217,6 +223,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param version the version of the Project (or null)
|
||||
* @return a Project object, or null if not found
|
||||
*/
|
||||
@Override
|
||||
public Project getProject(final String name, final String version) {
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
||||
|
@ -245,6 +252,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param team the team the has access to Projects
|
||||
* @return a List of Project objects
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final Team team, final boolean excludeInactive, final boolean bypass, final boolean onlyRoot) {
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
if (orderBy == null) {
|
||||
|
@ -272,6 +280,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param tag the tag associated with the Project
|
||||
* @return a List of Projects that contain the tag
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final Tag tag, final boolean includeMetrics, final boolean excludeInactive, final boolean onlyRoot) {
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -313,6 +322,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param classifier the classifier of the Project
|
||||
* @return a List of Projects of the specified classifier
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final Classifier classifier, final boolean includeMetrics, final boolean excludeInactive, final boolean onlyRoot) {
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -349,6 +359,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param tag the tag associated with the Project
|
||||
* @return a List of Projects that contain the tag
|
||||
*/
|
||||
@Override
|
||||
public PaginatedResult getProjects(final Tag tag) {
|
||||
return getProjects(tag, false, false, false);
|
||||
}
|
||||
|
@ -387,6 +398,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param name the name of the Tag
|
||||
* @return a Tag object
|
||||
*/
|
||||
@Override
|
||||
public Tag getTagByName(final String name) {
|
||||
final String loweredTrimmedTag = StringUtils.lowerCase(StringUtils.trimToNull(name));
|
||||
final Query<Tag> query = pm.newQuery(Tag.class, "name == :name");
|
||||
|
@ -399,6 +411,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param name the name of the Tag to create
|
||||
* @return the created Tag object
|
||||
*/
|
||||
@Override
|
||||
public Tag createTag(final String name) {
|
||||
final String loweredTrimmedTag = StringUtils.lowerCase(StringUtils.trimToNull(name));
|
||||
final Tag resolvedTag = getTagByName(loweredTrimmedTag);
|
||||
|
@ -440,6 +453,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param commitIndex specifies if the search index should be committed (an expensive operation)
|
||||
* @return the created Project
|
||||
*/
|
||||
@Override
|
||||
public Project createProject(String name, String description, String version, List<Tag> tags, Project parent, PackageURL purl, boolean active, boolean commitIndex) {
|
||||
final Project project = new Project();
|
||||
project.setName(name);
|
||||
|
@ -478,6 +492,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param commitIndex specifies if the search index should be committed (an expensive operation)
|
||||
* @return the created Project
|
||||
*/
|
||||
@Override
|
||||
public Project createProject(final Project project, List<Tag> tags, boolean commitIndex) {
|
||||
if (project.getParent() != null && !Boolean.TRUE.equals(project.getParent().isActive())){
|
||||
throw new IllegalArgumentException("An inactive Parent cannot be selected as parent");
|
||||
|
@ -503,6 +518,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param commitIndex specifies if the search index should be committed (an expensive operation)
|
||||
* @return the updated Project
|
||||
*/
|
||||
@Override
|
||||
public Project updateProject(UUID uuid, String name, String description, String version, List<Tag> tags, PackageURL purl, boolean active, boolean commitIndex) {
|
||||
final Project project = getObjectByUuid(Project.class, uuid);
|
||||
project.setName(name);
|
||||
|
@ -530,6 +546,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param commitIndex specifies if the search index should be committed (an expensive operation)
|
||||
* @return the updated Project
|
||||
*/
|
||||
@Override
|
||||
public Project updateProject(Project transientProject, boolean commitIndex) {
|
||||
final Project project = getObjectByUuid(Project.class, transientProject.getUuid());
|
||||
project.setAuthor(transientProject.getAuthor());
|
||||
|
@ -575,6 +592,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Project clone(UUID from, String newVersion, boolean includeTags, boolean includeProperties,
|
||||
boolean includeComponents, boolean includeServices, boolean includeAuditHistory,
|
||||
boolean includeACL) {
|
||||
|
@ -686,6 +704,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param project the Project to delete
|
||||
* @param commitIndex specifies if the search index should be committed (an expensive operation)
|
||||
*/
|
||||
@Override
|
||||
public void recursivelyDelete(final Project project, final boolean commitIndex) {
|
||||
if (project.getChildren() != null) {
|
||||
for (final Project child: project.getChildren()) {
|
||||
|
@ -727,6 +746,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param description a description of the property
|
||||
* @return the created ProjectProperty object
|
||||
*/
|
||||
@Override
|
||||
public ProjectProperty createProjectProperty(final Project project, final String groupName, final String propertyName,
|
||||
final String propertyValue, final ProjectProperty.PropertyType propertyType,
|
||||
final String description) {
|
||||
|
@ -747,6 +767,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param propertyName the name of the property
|
||||
* @return a ProjectProperty object
|
||||
*/
|
||||
@Override
|
||||
public ProjectProperty getProjectProperty(final Project project, final String groupName, final String propertyName) {
|
||||
final Query<ProjectProperty> query = this.pm.newQuery(ProjectProperty.class, "project == :project && groupName == :groupName && propertyName == :propertyName");
|
||||
query.setRange(0, 1);
|
||||
|
@ -758,6 +779,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param project the project the property belongs to
|
||||
* @return a List ProjectProperty objects
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<ProjectProperty> getProjectProperties(final Project project) {
|
||||
final Query<ProjectProperty> query = this.pm.newQuery(ProjectProperty.class, "project == :project");
|
||||
|
@ -797,6 +819,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param bomFormat the format and version of the bom format
|
||||
* @return the updated Project
|
||||
*/
|
||||
@Override
|
||||
public Project updateLastBomImport(Project p, Date date, String bomFormat) {
|
||||
final Project project = getObjectById(Project.class, p.getId());
|
||||
project.setLastBomImport(date);
|
||||
|
@ -804,10 +827,10 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return persist(project);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccess(final Principal principal, final Project project) {
|
||||
if (isEnabled(ConfigPropertyConstants.ACCESS_MANAGEMENT_ACL_ENABLED)) {
|
||||
if (principal instanceof UserPrincipal) {
|
||||
final UserPrincipal userPrincipal = (UserPrincipal) principal;
|
||||
if (principal instanceof final UserPrincipal userPrincipal) {
|
||||
if (super.hasAccessManagementPermission(userPrincipal)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -820,8 +843,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if (principal instanceof ApiKey ){
|
||||
final ApiKey apiKey = (ApiKey) principal;
|
||||
} else if (principal instanceof final ApiKey apiKey ){
|
||||
if (super.hasAccessManagementPermission(apiKey)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -850,8 +872,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
private void preprocessACLs(final Query<Project> query, final String inputFilter, final Map<String, Object> params, final boolean bypass) {
|
||||
if (super.principal != null && isEnabled(ConfigPropertyConstants.ACCESS_MANAGEMENT_ACL_ENABLED) && !bypass) {
|
||||
final List<Team> teams;
|
||||
if (super.principal instanceof UserPrincipal) {
|
||||
final UserPrincipal userPrincipal = ((UserPrincipal) super.principal);
|
||||
if (super.principal instanceof final UserPrincipal userPrincipal) {
|
||||
teams = userPrincipal.getTeams();
|
||||
if (super.hasAccessManagementPermission(userPrincipal)) {
|
||||
query.setFilter(inputFilter);
|
||||
|
@ -895,9 +916,9 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
* @param principal
|
||||
* @return True if ACL was updated
|
||||
*/
|
||||
@Override
|
||||
public boolean updateNewProjectACL(Project project, Principal principal) {
|
||||
if (isEnabled(ConfigPropertyConstants.ACCESS_MANAGEMENT_ACL_ENABLED) && principal instanceof ApiKey) {
|
||||
ApiKey apiKey = (ApiKey) principal;
|
||||
if (isEnabled(ConfigPropertyConstants.ACCESS_MANAGEMENT_ACL_ENABLED) && principal instanceof ApiKey apiKey) {
|
||||
final var apiTeam = apiKey.getTeams().stream().findFirst();
|
||||
if (apiTeam.isPresent()) {
|
||||
LOGGER.debug("adding Team to ACL of newly created project");
|
||||
|
@ -912,6 +933,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccessManagementPermission(final UserPrincipal userPrincipal) {
|
||||
for (Permission permission: getEffectivePermissions(userPrincipal)) {
|
||||
if (Permissions.ACCESS_MANAGEMENT.name().equals(permission.getName())) {
|
||||
|
@ -921,11 +943,13 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAccessManagementPermission(final ApiKey apiKey) {
|
||||
return hasPermission(apiKey, Permissions.ACCESS_MANAGEMENT.name());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public PaginatedResult getChildrenProjects(final UUID uuid, final boolean includeMetrics, final boolean excludeInactive) {
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -965,6 +989,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaginatedResult getChildrenProjects(final Classifier classifier, final UUID uuid, final boolean includeMetrics, final boolean excludeInactive) {
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -993,6 +1018,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaginatedResult getChildrenProjects(final Tag tag, final UUID uuid, final boolean includeMetrics, final boolean excludeInactive) {
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -1025,6 +1051,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaginatedResult getProjectsWithoutDescendantsOf(final boolean exludeInactive, final Project project){
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -1059,6 +1086,7 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PaginatedResult getProjectsWithoutDescendantsOf(final String name, final boolean excludeInactive, Project project){
|
||||
final PaginatedResult result;
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
|
@ -1122,9 +1150,10 @@ final class ProjectQueryManager extends QueryManager implements IQueryManager {
|
|||
}
|
||||
|
||||
private List<ProjectVersion> getProjectVersions(Project project) {
|
||||
final Query<Object[]> query = pm.newQuery(JDOQuery.SQL, "SELECT UUID, VERSION FROM PROJECT WHERE NAME = ?");
|
||||
final Query<Project> query = pm.newQuery(Project.class);
|
||||
query.setFilter("name == :name");
|
||||
query.setParameters(project.getName());
|
||||
final var stream = query.executeList().stream();
|
||||
return stream.map(i -> new ProjectVersion(i[0].toString(), i[1].toString())).toList();
|
||||
query.setResult("uuid, version");
|
||||
return query.executeResultList(ProjectVersion.class);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue