SuperX-Kernmodul
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

146 lines
5.6 KiB

package de.superx.bianalysis.service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import de.superx.bianalysis.metadata.Identifier;
import de.superx.bianalysis.models.Right;
import de.superx.bianalysis.models.RightParam;
import de.superx.common.AccessDeniedException;
import de.superx.common.SxUser;
import de.superx.spring.service.UserService;
@Service()
public class BiAnalysisRightService {
@Autowired
DbMetaAdapter dbAdapter;
@Autowired
UserService userService;
public void checkCreateRights() {
SxUser user = (SxUser) userService.currentUserDetails();
if ( user != null && !user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_CREATE_ANALYSIS") &&
!user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_EDIT_ANALYSIS_TABLE")) {
throw new AccessDeniedException("No right to access Create functions");
}
}
public boolean isCreateRight() {
SxUser user = (SxUser) userService.currentUserDetails();
if ( user != null && !user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_CREATE_ANALYSIS") &&
!user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_EDIT_ANALYSIS_TABLE")) {
return false;
}
return true;
}
public void checkCreateOrViewRights() {
SxUser user = (SxUser) userService.currentUserDetails();
if ( user != null && !user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_CREATE_ANALYSIS") &&
!user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_VIEW_ANALYSIS_TABLE") &&
!user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_VIEW_ANALYSIS_EMBEDDED") &&
!user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_EDIT_ANALYSIS_TABLE") ) {
throw new AccessDeniedException("No right to access Create and View functions");
}
}
public void checkDeleteRights() {
SxUser user = (SxUser) userService.currentUserDetails();
if ( user != null && !user.getHis1Rights().contains("RIGHT_CS_BIA_ANALYSIS_DELETE_ANALYSIS")) {
throw new AccessDeniedException("No right to delete analysis");
}
}
public List<Integer> getSachgebiete(Right... rights) {
List<String> values = getRightParamValues(RightParam.TOPIC_AREA, rights);
List<String> valuesForTopics = dbAdapter.getSachgebieteForFactTables(getRightParamValues(RightParam.TOPIC, rights));
values.addAll(valuesForTopics);
if (values.isEmpty()) {
return new ArrayList<>();
}
List<Integer> sachgebietValues = values.stream()
.map(Integer::parseInt).collect(Collectors.toList());
return sachgebietValues;
}
public List<Identifier> getFactTables(Right... rights) {
List<String> values = getRightParamValues(RightParam.TOPIC, rights);
if (values.isEmpty()) {
return new ArrayList<>();
}
List<Identifier> factsValues = values.stream()
.map(value -> new Identifier(value)).collect(Collectors.toList());
return factsValues;
}
public void checkSachgebiet(int sachgebiet, Right... rights) {
List<Integer> sachgebiete = getSachgebiete(rights);
if (sachgebiete.isEmpty()) {
return;
} else if (!sachgebiete.contains(Integer.valueOf(sachgebiet))){
throw new AccessDeniedException("No right to access Sachgebiet " + sachgebiet);
}
}
public void checkFactTable(Identifier fact, Right... rights) {
List<Identifier> factTables = getFactTables(rights);
if (factTables.isEmpty()) {
return;
} else if (!factTables.contains(fact)){
throw new AccessDeniedException("No right to access Fact Table " + fact.composedId);
}
}
/**
* Return the values of a RightParam for a given array of Rights.
* @param param RightParam which can be assigned to multiple Rights.
* @param rights Rights which may or may not contain the RightParam.
* @return List of values for the RightParam (first occurrence for multiple Rights).
*/
public List<String> getRightParamValues(RightParam param, Right... rights) {
SxUser user = (SxUser) userService.currentUserDetails();
if(user == null) {
return new ArrayList<>();
}
Map<String, Map<String,String>> rightsMap = user.getRightsMap();
Map<String, String> rightParamMap = null;
boolean noRights = true;
for (Right right : rights) {
if(rightsMap.containsKey(right.getString())) {
rightParamMap = rightsMap.get(right.getString());
noRights = false;
if(rightParamMap != null) {
break;
}
}
}
if(noRights) {
throw new AccessDeniedException("Missing rights: " + Right.getPrintableRights(rights));
}
if(rightParamMap == null || rightParamMap.isEmpty()) {
return new ArrayList<>();
}
String paramValues = rightParamMap.get(param.getString());
if (paramValues != null) {
List<String> paramValuesResult = new ArrayList<>();
for (String string : StringUtils.split(paramValues, ',')) {
paramValuesResult.add(string);
}
return paramValuesResult;
}
return new ArrayList<>();
}
}