Skip to content

Commit fab67c5

Browse files
committed
optimization
1 parent c7d456b commit fab67c5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/main/java/io/mixeway/db/entity/User.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.boot.autoconfigure.domain.EntityScan;
77
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
88

9+
import java.util.HashSet;
910
import java.util.Set;
1011

1112
@Entity
@@ -23,7 +24,7 @@ public class User {
2324
private String username;
2425
private int logins;
2526
private int failedLogins;
26-
private Set<Project> projects;
27+
private Set<Project> projects = new HashSet<>();;
2728
private String apiKey;
2829

2930
@Column(name = "apikey")
@@ -35,7 +36,7 @@ public void setApiKey(String apiKey) {
3536
this.apiKey = apiKey;
3637
}
3738

38-
@ManyToMany
39+
@ManyToMany(fetch = FetchType.EAGER)
3940
@JoinTable(
4041
name = "user_project",
4142
joinColumns = { @JoinColumn(name = "users_id") },

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class PermissionFactory {
3434
* @return boolean
3535
*/
3636
public boolean canUserAccessProject(Principal principal, Project project){
37+
if (project == null || project.getId() == null) {
38+
// Handle null project appropriately
39+
return false;
40+
}
3741
User user = getUserFromPrincipal(principal);
3842
if (apiKeyAccessProject(principal,project)){
3943
return true;
@@ -44,7 +48,10 @@ else if (user != null && (
4448
user.getPermisions().equals(Constants.ROLE_AUDITOR))){
4549
return true;
4650
} else if ( user != null && (user.getPermisions().equals(Constants.ROLE_USER) || user.getPermisions().equals(Constants.ROLE_PROJECT_OWNER) || user.getPermisions().equals(Constants.ROLE_EDITOR_RUNNER))){
47-
return user.getProjects().stream().map(Project::getId).collect(Collectors.toList()).contains(project.getId());
51+
return user.getProjects() != null && user.getProjects().stream()
52+
.map(Project::getId)
53+
.anyMatch(id -> id.equals(project.getId()));
54+
4855
} else if (principal.getName().equals(Constants.STRATEGY_SCHEDULER)) {
4956
return true;
5057
}else

0 commit comments

Comments
 (0)