Skip to content

Commit

Permalink
optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
siewer committed Sep 16, 2024
1 parent c7d456b commit fab67c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/java/io/mixeway/db/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.HashSet;
import java.util.Set;

@Entity
Expand All @@ -23,7 +24,7 @@ public class User {
private String username;
private int logins;
private int failedLogins;
private Set<Project> projects;
private Set<Project> projects = new HashSet<>();;
private String apiKey;

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

@ManyToMany
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "user_project",
joinColumns = { @JoinColumn(name = "users_id") },
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/io/mixeway/utils/PermissionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class PermissionFactory {
* @return boolean
*/
public boolean canUserAccessProject(Principal principal, Project project){
if (project == null || project.getId() == null) {
// Handle null project appropriately
return false;
}
User user = getUserFromPrincipal(principal);
if (apiKeyAccessProject(principal,project)){
return true;
Expand All @@ -44,7 +48,10 @@ else if (user != null && (
user.getPermisions().equals(Constants.ROLE_AUDITOR))){
return true;
} else if ( user != null && (user.getPermisions().equals(Constants.ROLE_USER) || user.getPermisions().equals(Constants.ROLE_PROJECT_OWNER) || user.getPermisions().equals(Constants.ROLE_EDITOR_RUNNER))){
return user.getProjects().stream().map(Project::getId).collect(Collectors.toList()).contains(project.getId());
return user.getProjects() != null && user.getProjects().stream()
.map(Project::getId)
.anyMatch(id -> id.equals(project.getId()));

} else if (principal.getName().equals(Constants.STRATEGY_SCHEDULER)) {
return true;
}else
Expand Down

0 comments on commit fab67c5

Please sign in to comment.