Skip to content

Commit 5769e92

Browse files
committed
optimization
1 parent a268f5f commit 5769e92

File tree

6 files changed

+58
-14
lines changed

6 files changed

+58
-14
lines changed

src/main/java/io/mixeway/api/dashboard/controller/DashboardController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public SourceDetectionChartData getSourceTrendData(Principal principal) throws I
5555

5656
@PreAuthorize("hasAuthority('ROLE_USER')")
5757
@GetMapping(value = "/projects")
58-
public List<Projects> getProjects(Principal principal) {
58+
public List<ProjectDTO> getProjects(Principal principal) {
5959
return dashboardService.getProjects(principal);
6060
}
6161

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package io.mixeway.api.dashboard.model;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class ProjectDTO {
7+
private Long id;
8+
private String ciid;
9+
private String name;
10+
private String description;
11+
private int risk;
12+
private int enableVulnManage;
13+
14+
// Constructor
15+
public ProjectDTO(Long id, String ciid, String name, String description, int risk, int enableVulnManage) {
16+
this.id = id;
17+
this.ciid = ciid;
18+
this.name = name;
19+
this.description = description;
20+
this.risk = risk;
21+
this.enableVulnManage = enableVulnManage;
22+
}
23+
}

src/main/java/io/mixeway/api/dashboard/service/DashboardService.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,8 @@ public SourceDetectionChartData getSourceTrendData(Principal principal) {
6767

6868
return findVulnHistoryService.getSourceTrendData(principal);
6969
}
70-
public List<Projects> getProjects(Principal principal) {
71-
List<Projects> projects = new ArrayList<>();
72-
for (Project p : permissionFactory.getProjectForPrincipal(principal)){
73-
Projects projects1 = new Projects();
74-
projects1.setId(p.getId());
75-
projects1.setCiid(p.getCiid());
76-
projects1.setName(p.getName());
77-
projects1.setDescription(p.getDescription());
78-
projects1.setRisk(p.getRisk());
79-
projects1.setEnableVulnManage(p.isEnableVulnManage() ? 1 : 0 );
80-
projects.add(projects1);
81-
}
82-
return projects;
70+
public List<ProjectDTO> getProjects(Principal principal) {
71+
return permissionFactory.getProjectForPrincipalWithDTO(principal);
8372
}
8473

8574
public ResponseEntity<Status> putProject(String projectName, String projectDescription, String ciid, int enableVulnManage, Principal principal) {

src/main/java/io/mixeway/db/repository/ProjectRepository.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.Optional;
55

6+
import io.mixeway.api.dashboard.model.ProjectDTO;
67
import io.mixeway.db.entity.Project;
78
import io.mixeway.db.entity.User;
89
import org.springframework.data.jpa.repository.JpaRepository;
@@ -40,4 +41,14 @@ public interface ProjectRepository extends JpaRepository<Project, Long>{
4041
@Query(value="select distinct(p.id) from project p, asset a, interface i where p.id=a.project_id and i.asset_id=a.id and i.scanrunning=true", nativeQuery = true)
4142
List<Long> getProjectIdWithScanRunningOnInterface();
4243

44+
45+
@Query("SELECT new io.mixeway.api.dashboard.model.ProjectDTO(p.id, p.ciid, p.name, p.description, p.risk, " +
46+
"CASE WHEN p.enableVulnManage = true THEN 1 ELSE 0 END) " +
47+
"FROM Project p JOIN p.users u WHERE u.username = :username")
48+
List<ProjectDTO> findProjectDTOsByUsername(@Param("username") String username);
49+
50+
@Query("SELECT new io.mixeway.api.dashboard.model.ProjectDTO(p.id, p.ciid, p.name, p.description, p.risk, " +
51+
"CASE WHEN p.enableVulnManage = true THEN 1 ELSE 0 END) " +
52+
"FROM Project p")
53+
List<ProjectDTO> findAllProjectDTOs();
4354
}

src/main/java/io/mixeway/utils/PermissionFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.mixeway.utils;
22

3+
import io.mixeway.api.dashboard.model.ProjectDTO;
34
import io.mixeway.config.Constants;
45
import io.mixeway.db.entity.Project;
56
import io.mixeway.db.entity.User;
@@ -142,6 +143,21 @@ public List<Project> getProjectForPrincipal(Principal principal){
142143
}
143144
}
144145

146+
public List<ProjectDTO> getProjectForPrincipalWithDTO(Principal principal){
147+
Optional<User> userOptional = userRepository.findByUsernameOrApiKey(principal.getName(), principal.getName());
148+
if (userOptional.isPresent()) {
149+
String permission = userOptional.get().getPermisions();
150+
if (Arrays.asList(Constants.ROLE_API, Constants.ROLE_USER, Constants.ROLE_PROJECT_OWNER, Constants.ROLE_EDITOR_RUNNER).contains(permission)) {
151+
// Return projects for the user
152+
return projectRepository.findProjectDTOsByUsername(principal.getName());
153+
} else if (Arrays.asList(Constants.ROLE_ADMIN, Constants.ROLE_AUDITOR).contains(permission)) {
154+
// Return all projects
155+
return projectRepository.findAllProjectDTOs();
156+
}
157+
}
158+
return Collections.emptyList();
159+
}
160+
145161
/**
146162
* Update user permissions
147163
*

src/main/resources/db/changelog/db.changelog-master.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,3 +1410,8 @@ update assethistory set low=0 where low is null;
14101410
CREATE INDEX idx_project_vulnerability_project_id ON projectvulnerability (project_id);
14111411
CREATE INDEX idx_project_vulnerability_status_id ON projectvulnerability (status_id);
14121412
CREATE INDEX idx_project_vulnerability_grade ON projectvulnerability (grade);
1413+
1414+
--changeset siewer:add_new_indexes
1415+
CREATE INDEX idx_user_id ON users (id);
1416+
CREATE INDEX idx_users_project_users ON user_project(users_id);
1417+
CREATE INDEX idx_users_project_project ON user_project(project_id);

0 commit comments

Comments
 (0)