Skip to content

Commit

Permalink
Use projections / result class to determine ProjectVersions
Browse files Browse the repository at this point in the history
Co-authored-by: Niklas <[email protected]>
Signed-off-by: Walter de Boer <[email protected]>
  • Loading branch information
Walter de Boer and nscuro committed Jun 27, 2023
1 parent c02fe38 commit 8580c05
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
15 changes: 11 additions & 4 deletions src/main/java/org/dependencytrack/model/ProjectVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import java.io.Serializable;
import java.util.UUID;

/**
* Value object holding UUID and version for a project
Expand All @@ -29,20 +30,26 @@ public class ProjectVersion implements Serializable {

private static final long serialVersionUID = 1L;

private String uuid;
private UUID uuid;

private String version;

public ProjectVersion(String uuid, String version) {
public ProjectVersion() {
this.uuid = null;
this.version = null;
}

public ProjectVersion(UUID uuid, String version) {
this.uuid = uuid;
this.version = version;

}
public void setUuid(String uuid) {

public void setUuid(UUID uuid) {
this.uuid = uuid;
}

public String getUuid() {
public UUID getUuid() {
return uuid;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -132,6 +132,7 @@ public PaginatedResult getProjects(final boolean includeMetrics, final boolean e
* Returns a list of all projects.
* @return a List of Projects
*/
@Override
public PaginatedResult getProjects(final boolean includeMetrics) {
return getProjects(includeMetrics, false, false);
}
Expand All @@ -140,6 +141,7 @@ public PaginatedResult getProjects(final boolean includeMetrics) {
* Returns a list of all projects.
* @return a List of Projects
*/
@Override
public PaginatedResult getProjects() {
return getProjects(false);
}
Expand All @@ -149,6 +151,7 @@ public PaginatedResult getProjects() {
* This method if designed NOT to provide paginated results.
* @return a List of Projects
*/
@Override
public List<Project> getAllProjects() {
return getAllProjects(false);
}
Expand All @@ -158,6 +161,7 @@ public List<Project> getAllProjects() {
* 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) {
Expand All @@ -172,6 +176,7 @@ public List<Project> getAllProjects(boolean excludeInactive) {
* @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) {
Expand Down Expand Up @@ -199,6 +204,7 @@ public PaginatedResult getProjects(final String name, final boolean excludeInact
* @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) {
Expand All @@ -217,6 +223,7 @@ public Project getProject(final String uuid) {
* @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);

Expand Down Expand Up @@ -245,6 +252,7 @@ public Project getProject(final String name, final String version) {
* @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) {
Expand Down Expand Up @@ -272,6 +280,7 @@ public PaginatedResult getProjects(final Team team, final boolean excludeInactiv
* @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);
Expand Down Expand Up @@ -313,6 +322,7 @@ public PaginatedResult getProjects(final Tag tag, final boolean includeMetrics,
* @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);
Expand Down Expand Up @@ -349,6 +359,7 @@ public PaginatedResult getProjects(final Classifier classifier, final boolean in
* @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);
}
Expand Down Expand Up @@ -387,6 +398,7 @@ private synchronized List<Tag> resolveTags(final List<Tag> tags) {
* @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");
Expand All @@ -399,6 +411,7 @@ public Tag getTagByName(final String name) {
* @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);
Expand Down Expand Up @@ -440,6 +453,7 @@ private List<Tag> createTags(final List<String> names) {
* @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);
Expand Down Expand Up @@ -478,6 +492,7 @@ public Project createProject(String name, String description, String version, Li
* @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");
Expand All @@ -503,6 +518,7 @@ public Project createProject(final Project project, List<Tag> tags, boolean comm
* @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);
Expand Down Expand Up @@ -530,6 +546,7 @@ public Project updateProject(UUID uuid, String name, String description, String
* @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());
Expand Down Expand Up @@ -575,6 +592,7 @@ public Project updateProject(Project transientProject, boolean commitIndex) {
return result;
}

@Override
public Project clone(UUID from, String newVersion, boolean includeTags, boolean includeProperties,
boolean includeComponents, boolean includeServices, boolean includeAuditHistory,
boolean includeACL) {
Expand Down Expand Up @@ -686,6 +704,7 @@ public Project clone(UUID from, String newVersion, boolean includeTags, boolean
* @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()) {
Expand Down Expand Up @@ -727,6 +746,7 @@ public void recursivelyDelete(final Project project, final boolean commitIndex)
* @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) {
Expand All @@ -747,6 +767,7 @@ public ProjectProperty createProjectProperty(final Project project, final String
* @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);
Expand All @@ -758,6 +779,7 @@ public ProjectProperty getProjectProperty(final Project project, final String gr
* @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");
Expand Down Expand Up @@ -797,17 +819,18 @@ public void bind(Project project, List<Tag> tags) {
* @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);
project.setLastBomImportFormat(bomFormat);
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;
}
Expand All @@ -820,8 +843,7 @@ public boolean hasAccess(final Principal principal, final Project project) {
}
}
}
} else if (principal instanceof ApiKey ){
final ApiKey apiKey = (ApiKey) principal;
} else if (principal instanceof final ApiKey apiKey ){
if (super.hasAccessManagementPermission(apiKey)) {
return true;
}
Expand Down Expand Up @@ -850,8 +872,7 @@ public boolean hasAccess(final Principal principal, final Project project) {
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);
Expand Down Expand Up @@ -895,9 +916,9 @@ private void preprocessACLs(final Query<Project> query, final String inputFilter
* @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");
Expand All @@ -912,6 +933,7 @@ public boolean updateNewProjectACL(Project project, Principal principal) {
return false;
}

@Override
public boolean hasAccessManagementPermission(final UserPrincipal userPrincipal) {
for (Permission permission: getEffectivePermissions(userPrincipal)) {
if (Permissions.ACCESS_MANAGEMENT.name().equals(permission.getName())) {
Expand All @@ -921,11 +943,13 @@ public boolean hasAccessManagementPermission(final UserPrincipal userPrincipal)
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);
Expand Down Expand Up @@ -965,6 +989,7 @@ public PaginatedResult getChildrenProjects(final UUID uuid, final boolean includ
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);
Expand Down Expand Up @@ -993,6 +1018,7 @@ public PaginatedResult getChildrenProjects(final Classifier classifier, final UU
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);
Expand Down Expand Up @@ -1025,6 +1051,7 @@ public PaginatedResult getChildrenProjects(final Tag tag, final UUID uuid, final
return result;
}

@Override
public PaginatedResult getProjectsWithoutDescendantsOf(final boolean exludeInactive, final Project project){
final PaginatedResult result;
final Query<Project> query = pm.newQuery(Project.class);
Expand Down Expand Up @@ -1059,6 +1086,7 @@ public PaginatedResult getProjectsWithoutDescendantsOf(final boolean exludeInact
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);
Expand Down Expand Up @@ -1122,9 +1150,10 @@ private static boolean hasActiveChild(Project project) {
}

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);
}
}

0 comments on commit 8580c05

Please sign in to comment.