Skip to content

armgit / core

Public Imported from GitHub
0

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

Inverted notification attributes

Michael Rademaker684b21c77d7c
diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
index ed527f97f..a509ee9d2 100644
--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
@@ -51,7 +51,7 @@ import java.util.stream.Collectors;
import static org.openremote.container.util.MapAccess.getBoolean;
import static org.openremote.container.util.MapAccess.getInteger;
-import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED;
+import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED;
import static org.openremote.model.Constants.*;
public class EmailNotificationHandler implements NotificationHandler {
@@ -171,7 +171,7 @@ public class EmailNotificationHandler implements NotificationHandler {
mappedTargets.addAll(
Arrays.stream(users)
- .filter(user -> Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED, Collections.singletonList("true")).get(0)))
+ .filter(user -> !Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0)))
.map(user -> {
Notification.Target userAssetTarget = new Notification.Target(Notification.TargetType.USER, user.getId());
userAssetTarget.setData(new EmailNotificationMessage.Recipient(user.getFullName(), user.getEmail()));
diff --git a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java
index 2f9723a0c..e5da7ce07 100644
--- a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java
+++ b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java
@@ -55,13 +55,12 @@ import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
-import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static org.openremote.container.concurrent.GlobalLock.withLock;
import static org.openremote.container.persistence.PersistenceEvent.*;
import static org.openremote.manager.gateway.GatewayService.isNotForGateway;
-import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED;
+import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED;
import static org.openremote.model.notification.PushNotificationMessage.TargetType.*;
@SuppressWarnings("deprecation")
@@ -215,12 +214,12 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio
.types(ConsoleAsset.class)
.attributes(new AttributePredicate(ConsoleAsset.CONSOLE_PROVIDERS, null, false, new NameValuePredicate.Path(PushNotificationMessage.TYPE))));
- // Get all user ids which have pushNotificationsEnabled set to false
+ // Get all user ids which have pushNotificationsDisabled set to false
String[] userIds = Arrays.stream(managerIdentityService
.getIdentityProvider()
.queryUsers(new UserQuery().tenant(new TenantPredicate((targetId))))
)
- .filter(user -> !Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED, Collections.singletonList("true")).get(0)))
+ .filter(user -> Boolean.parseBoolean(user.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0)))
.map(User::getId)
.toArray(String[]::new);
@@ -229,7 +228,7 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio
.map(userAssetLink -> userAssetLink.getId().getAssetId())
.toArray(String[]::new);
- //Remove consoleAssets which are linked to an User which has pushNotificationsEnabled set to false
+ //Remove consoleAssets which are linked to an User which has pushNotificationsDisabled set to false
consoleAssets = consoleAssets.stream()
.filter(consoleAsset -> Arrays.stream(assetIds).noneMatch(assetId -> assetId.equals(consoleAsset.getId())))
.collect(Collectors.toList());
@@ -246,7 +245,7 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio
.getIdentityProvider()
.queryUsers(new UserQuery().ids(targetId))).findFirst();
- if (user.isPresent() && Boolean.parseBoolean(user.get().getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED, Collections.singletonList("true")).get(0))) {
+ if (user.isPresent() && !Boolean.parseBoolean(user.get().getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0))) {
// Get all console assets linked to the specified user
String[] ids = assetStorageService.findUserAssetLinks(null, targetId, null)
@@ -285,19 +284,19 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio
.flatMap(Collection::stream)
.toArray(UserAssetLink[]::new);
- // Get all user ids which have pushNotificationsEnabled set to false
+ // Get all user ids which have pushNotificationsDisabled set to false
assetIds = Arrays.stream(userAssetLinks)
.filter(userAssetLink -> Arrays.stream(managerIdentityService
.getIdentityProvider()
.queryUsers(new UserQuery().asset(new UserAssetPredicate(userAssetLink.getId().getAssetId())))
)
- .filter(user1 -> !Boolean.parseBoolean(user1.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED, Collections.singletonList("true")).get(0)))
+ .filter(user1 -> Boolean.parseBoolean(user1.getAttributes().getOrDefault(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED, Collections.singletonList("false")).get(0)))
.map(User::getId)
.anyMatch(userId -> userId.equals(userAssetLink.getId().getUserId()))
).map(userAssetLink -> userAssetLink.getId().getAssetId())
.toArray(String[]::new);
- //Remove consoleAssets which are linked to an User which has pushNotificationsEnabled set to false
+ //Remove consoleAssets which are linked to an User which has pushNotificationsDisabled set to false
consoleAssets = consoleAssets.stream()
.filter(consoleAsset -> Arrays.stream(assetIds).noneMatch(assetId -> assetId.equals(consoleAsset.getId())))
.collect(Collectors.toList());
diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
index 4e1540f15..c0b83e46d 100644
--- a/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
+++ b/manager/src/main/java/org/openremote/manager/security/ManagerKeycloakIdentityProvider.java
@@ -86,8 +86,8 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
public static final String KEYCLOAK_GRANT_FILE = "KEYCLOAK_GRANT_FILE";
public static final String KEYCLOAK_GRANT_FILE_DEFAULT = "manager/build/keycloak.json";
public static final String KEYCLOAK_DEFAULT_ROLES_PREFIX = "default-roles-";
- public static final String KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED = "emailNotificationsEnabled";
- public static final String KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED = "pushNotificationsEnabled";
+ public static final String KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED = "emailNotificationsDisabled";
+ public static final String KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED = "pushNotificationsDisabled";
protected PersistenceService persistenceService;
protected TimerService timerService;
@@ -238,14 +238,6 @@ public class ManagerKeycloakIdentityProvider extends KeycloakIdentityProvider im
}
}
- if (!user.isServiceAccount() && !user.getAttributes().containsKey(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED)) {
- user.setAttribute(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED,"true");
- }
-
- if (!user.isServiceAccount() && !user.getAttributes().containsKey(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED)) {
- user.setAttribute(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED, "true");
- }
-
// For service users we don't actually create the user - keycloak does that when the client is created
if (isUpdate) {
diff --git a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
index 46a3ca2c6..2f4fb4ac3 100644
--- a/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
+++ b/manager/src/main/java/org/openremote/manager/setup/AbstractKeycloakSetup.java
@@ -32,8 +32,8 @@ import org.openremote.model.security.User;
import java.util.Arrays;
import java.util.Collections;
-import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED;
-import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED;
+import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED;
+import static org.openremote.manager.security.ManagerKeycloakIdentityProvider.KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED;
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID;
public abstract class AbstractKeycloakSetup implements Setup {
@@ -83,18 +83,18 @@ public abstract class AbstractKeycloakSetup implements Setup {
}
protected User createUser(String realm, String username, String password, String firstName, String lastName, String email, boolean enabled, ClientRole[] roles) {
- return createUser(realm, username, password, firstName, lastName, email, enabled, true, true, roles);
+ return createUser(realm, username, password, firstName, lastName, email, enabled, false, false, roles);
}
- protected User createUser(String realm, String username, String password, String firstName, String lastName, String email, boolean enabled, boolean emailNotificationsEnabled, boolean pushNotificationsEnabled, ClientRole[] roles) {
+ protected User createUser(String realm, String username, String password, String firstName, String lastName, String email, boolean enabled, boolean emailNotificationsDisabled, boolean pushNotificationsDisabled, ClientRole[] roles) {
User user = new User();
user.setUsername(username);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setEnabled(enabled);
- user.setAttribute(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_ENABLED, Boolean.toString(emailNotificationsEnabled));
- user.setAttribute(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_ENABLED, Boolean.toString(pushNotificationsEnabled));
+ user.setAttribute(KEYCLOAK_USER_ATTRIBUTE_EMAIL_NOTIFICATIONS_DISABLED, Boolean.toString(emailNotificationsDisabled));
+ user.setAttribute(KEYCLOAK_USER_ATTRIBUTE_PUSH_NOTIFICATIONS_DISABLED, Boolean.toString(pushNotificationsDisabled));
user = keycloakProvider.createUpdateUser(realm, user, password);
if (user == null) {
return null;
diff --git a/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java b/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java
index dc704dabd..e4c03b08a 100644
--- a/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java
+++ b/test/src/main/java/org/openremote/test/setup/KeycloakTestSetup.java
@@ -84,14 +84,14 @@ public class KeycloakTestSetup extends AbstractKeycloakSetup {
User testuser1 = createUser(MASTER_REALM, "testuser1", "testuser1", "DemoMaster", "DemoLast", null, true, container.isDevMode() ? REGULAR_USER_ROLES : demoUserRoles);
this.testuser1Id = testuser1.getId();
keycloakProvider.updateUserRoles(MASTER_REALM, testuser1Id, "account"); // Remove all roles for account client
- User testuser2 = createUser(tenantBuilding.getRealm(), "testuser2", "testuser2", "DemoA2", "DemoLast", "[email protected]", true, true, false, new ClientRole[] {
+ User testuser2 = createUser(tenantBuilding.getRealm(), "testuser2", "testuser2", "DemoA2", "DemoLast", "[email protected]", true, false, true, new ClientRole[] {
ClientRole.WRITE_USER,
ClientRole.READ_MAP,
ClientRole.READ_ASSETS
});
this.testuser2Id = testuser2.getId();
keycloakProvider.updateUserRoles(tenantBuilding.getRealm(), testuser2Id, "account"); // Remove all roles for account client
- User testuser3 = createUser(tenantBuilding.getRealm(), "testuser3", "testuser3", "DemoA3", "DemoLast", "[email protected]", true, false, true, container.isDevMode() ? REGULAR_USER_ROLES : demoUserRoles);
+ User testuser3 = createUser(tenantBuilding.getRealm(), "testuser3", "testuser3", "DemoA3", "DemoLast", "[email protected]", true, true, false, container.isDevMode() ? REGULAR_USER_ROLES : demoUserRoles);
this.testuser3Id = testuser3.getId();
keycloakProvider.updateUserRoles(tenantBuilding.getRealm(), testuser3Id, "account"); // Remove all roles for account client
User buildingUser = createUser(tenantBuilding.getRealm(), "building", "building", "Building", "User", "[email protected]", true, demoUserRoles);
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
index 20c32460b..27b32874e 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
@@ -195,7 +195,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
WebApplicationException ex = thrown()
ex.response.status == 403
- when: "the admin user sends a notification to a user in a different realm with emailNotificationsEnabled set to false"
+ when: "the admin user sends a notification to a user in a different realm with emailNotificationsDisabled set to true"
notification.targets = [new Notification.Target(Notification.TargetType.USER, keycloakTestSetup.testuser2Id)]
advancePseudoClock(1, TimeUnit.HOURS, container)
adminNotificationResource.sendNotification(null, notification)
@@ -205,7 +205,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
ex.response.status == 400
notificationIds.size() == 3
- when: "the admin user sends a notification to a user in a different realm with emailNotificationsEnabled set to true"
+ when: "the admin user sends a notification to a user in a different realm with emailNotificationsDisabled set to false"
notification.targets = [new Notification.Target(Notification.TargetType.USER, keycloakTestSetup.testuser3Id)]
advancePseudoClock(1, TimeUnit.HOURS, container)
adminNotificationResource.sendNotification(null, notification)