Skip to content

Commit

Permalink
Merge pull request #3056 from SCADA-LTS/feature/#3055_Added_validatio…
Browse files Browse the repository at this point in the history
…n_Cyclic_dependency_for_Meta_Data_Point

#3055 Added validation Cyclic dependency for Meta Data Point:
  • Loading branch information
Limraj authored Nov 29, 2024
2 parents 358f13c + 8218732 commit 284961e
Show file tree
Hide file tree
Showing 41 changed files with 236 additions and 70 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ test {
includeTestsMatching "org.scada_lts.web.security.XssUtilsTestsSuite"
includeTestsMatching "org.scada_lts.web.mvc.api.validation.css.CssValidatorTestsSuite"
includeTestsMatching "org.scada_lts.web.beans.validation.xss.XssValidatorTestsSuite"
includeTestsMatching "org.scada_lts.utils.CyclicDependencyValidationUtilsTest"
}

failFast = true
Expand Down
22 changes: 17 additions & 5 deletions src/com/serotonin/mango/vo/dataSource/meta/MetaPointLocatorVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.serotonin.db.IntValuePair;
import com.serotonin.json.JsonArray;
Expand All @@ -47,13 +46,16 @@
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.dataSource.AbstractPointLocatorVO;
import com.serotonin.mango.vo.TimePeriodType;
import com.serotonin.mango.vo.dataSource.PointLocatorVO;
import com.serotonin.timer.CronTimerTrigger;
import com.serotonin.util.SerializationHelper;
import com.serotonin.util.StringUtils;
import com.serotonin.web.dwr.DwrResponseI18n;
import com.serotonin.web.i18n.LocalizableMessage;
import org.scada_lts.mango.service.DataPointService;

import static org.scada_lts.utils.ValidationDwrUtils.validateVarNameScript;
import static org.scada_lts.utils.ValidationUtils.isCyclicDependency;
import static org.scada_lts.web.security.XssProtectUtils.escapeHtml;

/**
Expand Down Expand Up @@ -187,11 +189,21 @@ public void validate(DwrResponseI18n response, int dataPointId) {
if (StringUtils.isEmpty(script))
response.addContextualMessage("script", "validate.required");

List<String> varNameSpace = new ArrayList<String>();
DataPointService dataPointService = new DataPointService();
Map<Integer, DataPointVO> dataPoints = dataPointService.getDataPoints(null, true)
.stream()
.collect(Collectors.toMap(DataPointVO::getId, Function.identity()));

List<String> varNameSpace = new ArrayList<>();
for (IntValuePair point : context) {
String varName = point.getValue();
int pointId = point.getKey();

if(pointId != Common.NEW_ID && isCyclicDependency(pointId, dataPointId, dataPoints, 10)) {
response.addContextualMessage("context", "validate.cyclicDependency", escapeHtml(varName));
break;
}

if(pointId != Common.NEW_ID && pointId == dataPointId) {
response.addContextualMessage("context", "validate.invalidVariable", escapeHtml(varName));
break;
Expand Down
21 changes: 0 additions & 21 deletions src/org/scada_lts/mango/service/DataPointService.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,27 +254,6 @@ public List<PointValueDTO> valuesPointBooleanBaseOnNameFilter2DTO(Map<DataPointV
return result;
}

@Deprecated(since = "2.7.7")
public void save(User user, String value, String xid, int pointValueType) {
DataPointVO dpvo = dataPointDAO.getDataPoint(xid);
new PointValueDAO4REST().save(value, pointValueType, dpvo.getId());
setPoint(user, dpvo, value);
}

@Deprecated(since = "2.7.7")
private void setPoint(User user, DataPointVO point, String valueStr) {
Permissions.ensureDataPointSetPermission(user, point);
setPointImpl(point, valueStr, user);

}

@Deprecated(since = "2.7.7")
public void saveAPI(User user, String value, String xid) {
DataPointVO dpvo = dataPointDAO.getDataPoint(xid);
Permissions.ensureDataPointSetPermission(user, dpvo);
setPointImpl(dpvo, value, user);
}

public void save(User user, String value, String xid, int pointValueType, SetPointSource source) {
DataPointVO point = dataPointDAO.getDataPoint(xid);
if(point == null || point.getPointLocator() == null)
Expand Down
36 changes: 36 additions & 0 deletions src/org/scada_lts/utils/ValidationUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.scada_lts.utils;
import com.serotonin.db.IntValuePair;
import com.serotonin.mango.Common;
import com.serotonin.mango.util.LoggingUtils;
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.User;
import com.serotonin.mango.vo.dataSource.PointLocatorVO;
import com.serotonin.mango.vo.dataSource.meta.MetaPointLocatorVO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.scada_lts.serorepl.utils.StringUtils;
Expand All @@ -13,6 +17,8 @@
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
Expand Down Expand Up @@ -128,4 +134,34 @@ public static void checkIfNonAdminThenUnauthorized(HttpServletRequest request) {
throw new UnauthorizedException(request.getRequestURI());
}
}

public static boolean isCyclicDependency(int starDataPointId, int findDataPointId, Map<Integer, DataPointVO> dataPoints, int safe) {
if(starDataPointId == findDataPointId) {
return true;
}
if(safe < 0) {
return false;
}
DataPointVO dataPoint = dataPoints.get(starDataPointId);
PointLocatorVO pointLocator = dataPoint.getPointLocator();
if(pointLocator instanceof MetaPointLocatorVO) {
MetaPointLocatorVO metaPointLocator = (MetaPointLocatorVO) pointLocator;
List<IntValuePair> context = metaPointLocator.getContext();
if (context == null || context.isEmpty()) {
return false;
}
for (IntValuePair keyValue : context) {
int contextDataPointId = keyValue.getKey();
DataPointVO contextDataPoint = dataPoints.get(contextDataPointId);
if(contextDataPoint.getPointLocator() instanceof MetaPointLocatorVO) {
if (contextDataPointId == findDataPointId) {
return true;
} else {
return isCyclicDependency(contextDataPointId, findDataPointId, dataPoints, --safe);
}
}
}
}
return false;
}
}
2 changes: 1 addition & 1 deletion test/br/org/scadabr/db/dao/UsersProfileDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import br.org.scadabr.db.dao.mocks.MockViewDao;
import br.org.scadabr.db.dao.mocks.MockWatchlistDao;
import br.org.scadabr.db.scenarios.DatalessDatabaseScenario;
import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.permission.ViewAccess;
import br.org.scadabr.vo.permission.WatchListAccess;
import br.org.scadabr.vo.usersProfiles.UsersProfileVO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import br.org.scadabr.db.dao.mocks.MockViewDao;
import br.org.scadabr.db.dao.mocks.MockWatchlistDao;
import br.org.scadabr.db.scenarios.DatalessDatabaseScenario;
import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.permission.ViewAccess;
import br.org.scadabr.vo.permission.WatchListAccess;
import br.org.scadabr.vo.usersProfiles.UsersProfileVO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import br.org.scadabr.db.dao.mocks.MockDataSourceDao;
import br.org.scadabr.db.dao.mocks.MockViewDao;
import br.org.scadabr.db.dao.mocks.MockWatchlistDao;
import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.permission.ViewAccess;
import br.org.scadabr.vo.permission.WatchListAccess;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serotonin.mango.rt.dataImage.datapointrt.config;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.Common;
import com.serotonin.mango.db.dao.DataPointDao;
import com.serotonin.mango.db.dao.DataSourceDao;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serotonin.mango.rt.dataSource;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.db.IntValuePair;
import com.serotonin.mango.Common;
import com.serotonin.mango.db.dao.DataPointDao;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.serotonin.mango.util;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.rt.dataSource.DataSourceRT;
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.dataSource.meta.MetaPointLocatorVO;
Expand Down
3 changes: 1 addition & 2 deletions test/com/serotonin/mango/vo/LoggedUsersMultiThreadTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.serotonin.mango.vo;

import br.org.scadabr.db.utils.TestUtils;
import org.junit.After;
import utils.TestUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down
4 changes: 1 addition & 3 deletions test/com/serotonin/mango/vo/LoggedUsersTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.serotonin.mango.vo;

import br.org.scadabr.db.utils.TestUtils;
import org.junit.After;
import utils.TestUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -13,7 +12,6 @@
import org.scada_lts.login.LoggedUsers;
import org.scada_lts.web.beans.ApplicationBeans;
import org.springframework.mock.web.MockHttpSession;
import utils.TestConcurrentUtils;

import javax.servlet.http.HttpSession;
import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static br.org.scadabr.db.utils.TestUtils.*;
import static utils.TestUtils.*;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.List;

import static br.org.scadabr.db.utils.TestUtils.*;
import static utils.TestUtils.*;

@RunWith(Parameterized.class)
public class CalculateLinesImageChartUtilsExceptionTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.ArrayList;
import java.util.List;

import static br.org.scadabr.db.utils.TestUtils.*;
import static utils.TestUtils.*;
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
Expand Down
3 changes: 1 addition & 2 deletions test/org/scada_lts/dao/PointValueDAOTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.scada_lts.dao;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.rt.dataImage.AnnotatedPointValueTime;
import com.serotonin.mango.vo.User;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.ds.messaging.channel;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.rt.dataImage.DataPointRT;
import com.serotonin.mango.vo.DataPointVO;
import org.junit.Assert;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.scada_lts.ds.messaging.channel;

import br.org.scadabr.db.utils.TestUtils;
import com.serotonin.mango.rt.dataImage.DataPointRT;
import com.serotonin.mango.vo.DataPointVO;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import utils.mock.MockUtils;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.mango.service;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.permission.ViewAccess;
import br.org.scadabr.vo.permission.WatchListAccess;
import br.org.scadabr.vo.usersProfiles.UsersProfileVO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.permissions.migration;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.permission.ViewAccess;
import br.org.scadabr.vo.permission.WatchListAccess;
import br.org.scadabr.vo.usersProfiles.UsersProfileVO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.permissions.migration;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.permission.ViewAccess;
import br.org.scadabr.vo.permission.WatchListAccess;
import br.org.scadabr.vo.usersProfiles.UsersProfileVO;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.permissions.migration;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import br.org.scadabr.vo.usersProfiles.UsersProfileVO;
import com.serotonin.mango.view.ShareUser;
import com.serotonin.mango.view.View;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.permissions.service.util;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.vo.User;
import com.serotonin.mango.vo.permission.DataPointAccess;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.permissions.service.util;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.vo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Arrays;
import java.util.Collection;

import static br.org.scadabr.db.utils.TestUtils.*;
import static utils.TestUtils.*;

@RunWith(Parameterized.class)
public class TruncateStringUtilsExceptionTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Arrays;
import java.util.Collection;

import static br.org.scadabr.db.utils.TestUtils.*;
import static utils.TestUtils.*;
import static org.junit.Assert.*;

@RunWith(Parameterized.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.service.pointhierarchy;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.view.ShareUser;
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.User;
Expand All @@ -22,7 +22,6 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.scada_lts.permissions.migration.MigrationPermissionsUtils.generateDataPointAccess;
import static org.scada_lts.utils.PointHierarchyUtils.sort;

public class GetPointHierarchyByKeyTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scada_lts.service.pointhierarchy;

import br.org.scadabr.db.utils.TestUtils;
import utils.TestUtils;
import com.serotonin.mango.view.ShareUser;
import com.serotonin.mango.vo.DataPointVO;
import com.serotonin.mango.vo.User;
Expand All @@ -13,7 +13,6 @@
import org.scada_lts.dao.HierarchyDAO;
import org.scada_lts.dao.model.pointhierarchy.PointHierarchyNode;
import org.scada_lts.dao.pointhierarchy.PointHierarchyXidDAO;
import org.scada_lts.utils.PointHierarchyUtils;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down
Loading

0 comments on commit 284961e

Please sign in to comment.