Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Fixes JSON rules notification with linked user realm query in the action target (Fixes #1447) (#1453)

Rich Turner7e0dc4e73524
diff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
index 8b424d53c..1a797a53b 100644
--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java
@@ -785,7 +785,7 @@ public class JsonRulesBuilder extends RulesBuilder {
Notification notification = ValueUtil.clone(notificationAction.notification);
String body;
- boolean linkedUsersTarget = ruleAction.target != null && ruleAction.target.linkedUsers != null && ruleAction.target.linkedUsers;
+ boolean linkedUsersTarget = ruleAction.target != null && Boolean.TRUE.equals(ruleAction.target.linkedUsers);
boolean isEmail = Objects.equals(notification.getMessage().getType(), EmailNotificationMessage.TYPE);
boolean isPush = Objects.equals(notification.getMessage().getType(), PushNotificationMessage.TYPE);
boolean isHtml;
@@ -807,7 +807,7 @@ public class JsonRulesBuilder extends RulesBuilder {
// Transfer the rule action target into notification targets
Notification.TargetType targetType = Notification.TargetType.ASSET;
if (ruleAction.target != null) {
- if (ruleAction.target.users != null
+ if ((ruleAction.target.users != null || Boolean.TRUE.equals(ruleAction.target.linkedUsers))
&& ruleAction.target.conditionAssets == null
&& ruleAction.target.assets == null
&& ruleAction.target.matchedAssets == null) {
@@ -824,13 +824,12 @@ public class JsonRulesBuilder extends RulesBuilder {
boolean bodyContainsTriggeredAssetInfo = !TextUtil.isNullOrEmpty(body) && body.contains(PLACEHOLDER_TRIGGER_ASSETS);
if (linkedUsersTarget) {
- targetType = Notification.TargetType.USER;
-
- // Find users linked to the matched assets
+ // Find users linked to the matched assets applying any additional use query in the action
Set<String> assetIds = useUnmatched ? ruleState.otherwiseMatchedAssetIds : ruleState.thenMatchedAssetIds;
+ UserQuery userQuery = ruleAction.target.users != null ? ruleAction.target.users : new UserQuery();
List<String> userIds = assetIds == null || assetIds.isEmpty()
? Collections.emptyList()
- : usersFacade.getResults(new UserQuery().assets(assetIds.toArray(String[]::new))).toList();
+ : usersFacade.getResults(userQuery.assets(assetIds.toArray(String[]::new))).toList();
if (userIds.isEmpty()) {
LOG.info("No users linked to matched assets for triggered rule so nothing to do: " + jsonRuleset);
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index c30d7846b..7987c9b25 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -346,6 +346,12 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
assert pushTargetsAndMessages.count {it.v2.title == "Linked user test 2" && it.v1.type == Notification.TargetType.ASSET && it.v1.id == consoleRegistration2.id} == 1
}
+ and: "no push notification should have been sent for the test-realm-role-3 notification action"
+ conditions.eventually {
+ assert pushTargetsAndMessages.count {it.v2.title == "Linked user test 3" && it.v1.type == Notification.TargetType.ASSET && it.v1.id == consoleRegistration.id} == 0
+ assert pushTargetsAndMessages.count {it.v2.title == "Linked user test 3" && it.v1.type == Notification.TargetType.ASSET && it.v1.id == consoleRegistration2.id} == 0
+ }
+
and: "an email notification should have been sent to [email protected] with the triggered asset in the body but only containing the triggered asset states"
conditions.eventually {
assert emailMessages.any {it.getRecipients(jakarta.mail.Message.RecipientType.TO).length == 1
diff --git a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json
index a24239ee7..39c0399ca 100644
--- a/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json
+++ b/test/src/test/resources/org/openremote/test/rules/BasicJsonRules.json
@@ -94,6 +94,27 @@
}
}
},
+ {
+ "action": "notification",
+ "target": {
+ "linkedUsers": true,
+ "users": {
+ "realmRoles": [
+ {
+ "predicateType": "string",
+ "value": "test-realm-role-3"
+ }
+ ]
+ }
+ },
+ "notification": {
+ "name": "linkedPushTest",
+ "message": {
+ "type": "push",
+ "title": "Linked user test 3"
+ }
+ }
+ },
{
"action": "notification",
"target": {