6-project-permissions #19

Merged
romanov73 merged 4 commits from 6-project-permissions into master 2025-02-26 12:07:43 +04:00
6 changed files with 48 additions and 23 deletions

View File

@ -28,7 +28,7 @@ public class ProjectService {
Project project = projectRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Project not found by id"));
checkUserProjectWithThrow(project, userService.getCurrentUser());
checkIsCurrentUserProjectWithThrow(project);
return project;
}
@ -52,8 +52,8 @@ public class ProjectService {
projectRepository.deleteById(projectForm.getId());
}
private void checkUserProjectWithThrow(Project project, User currentUser) {
if (!isUserProject(project, currentUser)) {
public void checkIsCurrentUserProjectWithThrow(Project project) {
if (!isUserProject(project, userService.getCurrentUser())) {
throw new RuntimeException("User can not get access to project");
}
}

View File

@ -22,16 +22,13 @@ public class FuzzyTermRestController {
this.fuzzyTermService = fuzzyTermService;
}
@GetMapping("/list/{projectId}/{variableId}")
public List<FuzzyTerm> getList(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "variableId") Integer variableId) {
return fuzzyTermService.getAll(projectId, variableId);
@GetMapping("/list/{variableId}")
public List<FuzzyTerm> getList(@PathVariable(value = "variableId") Integer variableId) {
return fuzzyTermService.getAll(variableId);
}
@GetMapping("/get/{projectId}/{variableId}/{fuzzyTermId}")
public FuzzyTerm getById(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "variableId") Integer variableId,
@PathVariable(value = "fuzzyTermId") Integer fuzzyTermId) {
@GetMapping("/get/{fuzzyTermId}")
public FuzzyTerm getById(@PathVariable(value = "fuzzyTermId") Integer fuzzyTermId) {
return fuzzyTermService.getById(fuzzyTermId);
}

View File

@ -1,8 +1,13 @@
package ru.ulstu.fc.rule.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.Variable;
public interface FuzzyTermRepository extends JpaRepository<FuzzyTerm, Integer> {
@Query("SELECT v FROM Variable v LEFT JOIN v.fuzzyTerms ft WHERE ft = :fuzzyTerm")
Variable findByFuzzyTerm(@Param("fuzzyTerm") FuzzyTerm fuzzyTerm);
}

View File

@ -11,15 +11,18 @@ public class FuzzyRuleService {
private final FuzzyRuleRepository ruleRepository;
private final ProjectService projectService;
public FuzzyRuleService(FuzzyRuleRepository ruleRepository, ProjectService projectService) {
public FuzzyRuleService(FuzzyRuleRepository ruleRepository,
ProjectService projectService) {
this.ruleRepository = ruleRepository;
this.projectService = projectService;
}
public FuzzyRule getById(Integer id) {
return ruleRepository
FuzzyRule fuzzyRule = ruleRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Rule not found by id"));
checkIsCurrentUserFuzzyRuleWithThrow(fuzzyRule);
return fuzzyRule;
}
public FuzzyRule save(FuzzyRuleForm ruleForm) {
@ -35,7 +38,10 @@ public class FuzzyRuleService {
}
public void delete(FuzzyRuleForm ruleForm) {
getById(ruleForm.getId());
ruleRepository.deleteById(ruleForm.getId());
ruleRepository.delete(getById(ruleForm.getId()));
}
public void checkIsCurrentUserFuzzyRuleWithThrow(FuzzyRule fuzzyRule) {
projectService.checkIsCurrentUserProjectWithThrow(fuzzyRule.getProject());
}
}

View File

@ -1,6 +1,7 @@
package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.FuzzyTermForm;
import ru.ulstu.fc.rule.repository.FuzzyTermRepository;
@ -12,20 +13,26 @@ import java.util.List;
public class FuzzyTermService {
private final FuzzyTermRepository fuzzyTermRepository;
private final VariableService variableService;
private final ProjectService projectService;
public FuzzyTermService(FuzzyTermRepository fuzzyTermRepository,
VariableService variableService) {
VariableService variableService,
ProjectService projectService) {
this.fuzzyTermRepository = fuzzyTermRepository;
this.variableService = variableService;
this.projectService = projectService;
}
public FuzzyTerm getById(Integer id) {
return fuzzyTermRepository
FuzzyTerm fuzzyTerm = fuzzyTermRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Term not found by id"));
checkIsCurrentUserFuzzyTermWithThrow(fuzzyTerm);
return fuzzyTerm;
}
public FuzzyTerm save(FuzzyTermForm fuzzyTermForm) {
variableService.getById(fuzzyTermForm.getVariableId());
FuzzyTerm term;
if (fuzzyTermForm.getId() == null || fuzzyTermForm.getId() == 0) {
term = new FuzzyTerm();
@ -39,12 +46,10 @@ public class FuzzyTermService {
variableService.addFuzzyTerm(fuzzyTermForm.getVariableId(), ft);
}
return ft;
}
public void delete(FuzzyTermForm fuzzyTermForm) {
getById(fuzzyTermForm.getId());
fuzzyTermRepository.deleteById(fuzzyTermForm.getId());
fuzzyTermRepository.delete(getById(fuzzyTermForm.getId()));
}
public List<FuzzyTerm> getByVariableId(Integer variableId) {
@ -54,7 +59,12 @@ public class FuzzyTermService {
return variableService.getById(variableId).getFuzzyTerms();
}
public List<FuzzyTerm> getAll(Integer projectId, Integer variableId) {
public List<FuzzyTerm> getAll(Integer variableId) {
variableService.getById(variableId);
return getByVariableId(variableId);
}
public void checkIsCurrentUserFuzzyTermWithThrow(FuzzyTerm fuzzyTerm) {
projectService.checkIsCurrentUserProjectWithThrow(fuzzyTermRepository.findByFuzzyTerm(fuzzyTerm).getProject());
}
}

View File

@ -14,15 +14,18 @@ public class VariableService {
private final VariableRepository variableRepository;
private final ProjectService projectService;
public VariableService(VariableRepository variableRepository, ProjectService projectService) {
public VariableService(VariableRepository variableRepository,
ProjectService projectService) {
this.variableRepository = variableRepository;
this.projectService = projectService;
}
public Variable getById(Integer id) {
return variableRepository
Variable variable = variableRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Variable not found by id"));
checkIsCurrentUserVariableWithThrow(variable);
return variable;
}
public Variable save(VariableForm variableForm) {
@ -65,4 +68,8 @@ public class VariableService {
public List<Variable> getAllByProject(Integer projectId) {
return variableRepository.getByProject(projectService.getById(projectId));
}
public void checkIsCurrentUserVariableWithThrow(Variable variable) {
projectService.checkIsCurrentUserProjectWithThrow(variable.getProject());
}
}