Skip to content

armgit / core

Public Imported from GitHub
0

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

Users can only acknowledge their own notifications

Christian Bauer32420bc4aa28
diff --git a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
index f9a6dba06..3d1b11810 100644
--- a/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
+++ b/manager/client/src/main/java/org/openremote/manager/client/admin/users/AdminUserActivity.java
@@ -288,7 +288,7 @@ public class AdminUserActivity
this.alertNotification = updatedNotification;
environment.getRequestService().execute(
alertNotificationMapper,
- requestParams -> notificationResource.storeAlertNotificationForUser(requestParams, userId, this.alertNotification),
+ requestParams -> notificationResource.storeNotificationForUser(requestParams, userId, this.alertNotification),
204,
() -> environment.getEventBus().dispatch(new ShowSuccessEvent(
environment.getMessages().notificationSentToUser()
diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java
index cb0137499..a955c77df 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java
@@ -22,16 +22,16 @@ package org.openremote.manager.server.notification;
import org.openremote.container.web.WebResource;
import org.openremote.manager.shared.http.RequestParams;
import org.openremote.manager.shared.notification.DeviceNotificationToken;
-import org.openremote.model.notification.AlertNotification;
import org.openremote.manager.shared.notification.NotificationResource;
+import org.openremote.model.notification.AlertNotification;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
-
import java.util.List;
import java.util.logging.Logger;
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+import static javax.ws.rs.core.Response.Status.FORBIDDEN;
public class NotificationResourceImpl extends WebResource implements NotificationResource {
@@ -70,7 +70,7 @@ public class NotificationResourceImpl extends WebResource implements Notificatio
}
@Override
- public void storeAlertNotification(RequestParams requestParams, AlertNotification alertNotification) {
+ public void storeNotificationForCurrentUser(RequestParams requestParams, AlertNotification alertNotification) {
if (alertNotification == null) {
throw new WebApplicationException("Missing alertNotification", BAD_REQUEST);
}
@@ -78,7 +78,7 @@ public class NotificationResourceImpl extends WebResource implements Notificatio
}
@Override
- public void storeAlertNotificationForUser(RequestParams requestParams, String userId, AlertNotification alertNotification) {
+ public void storeNotificationForUser(RequestParams requestParams, String userId, AlertNotification alertNotification) {
if (alertNotification == null) {
throw new WebApplicationException("Missing alertNotification", BAD_REQUEST);
}
@@ -86,15 +86,29 @@ public class NotificationResourceImpl extends WebResource implements Notificatio
}
@Override
- public List<AlertNotification> getAlertNotification() {
+ public List<AlertNotification> getNotificationsOfUser(RequestParams requestParams, String userId) {
+ return null;
+ // TODO
+ }
+
+ @Override
+ public void removeNotificationOfUser(RequestParams requestParams, String userId) {
+ // TODO
+ }
+
+ @Override
+ public List<AlertNotification> getPendingNotificationsOfCurrentUser(RequestParams requestParams) {
return notificationService.getPendingAlertForUserId(getUserId());
}
@Override
- public void removeAlertNotification(Long id) {
+ public void ackPendingNotificationOfCurrentUser(RequestParams requestParams, Long id) {
if (id == null) {
throw new WebApplicationException("Missing alert id", BAD_REQUEST);
}
+ if (!notificationService.isPendingAlertForUserId(id, getUserId())) {
+ throw new WebApplicationException(FORBIDDEN);
+ }
notificationService.removeAlertNotification(id);
}
}
diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java
index 29fb76d1f..3694392dc 100644
--- a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java
+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java
@@ -19,7 +19,6 @@
*/
package org.openremote.manager.server.notification;
-import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
@@ -27,7 +26,6 @@ import org.openremote.container.Container;
import org.openremote.container.ContainerService;
import org.openremote.container.persistence.PersistenceService;
import org.openremote.container.timer.TimerService;
-import org.openremote.container.util.Util;
import org.openremote.container.web.WebService;
import org.openremote.manager.shared.notification.DeviceNotificationToken;
import org.openremote.model.notification.AlertNotification;
@@ -36,6 +34,7 @@ import org.openremote.model.user.UserQuery;
import javax.persistence.EntityManager;
import javax.persistence.Query;
+import javax.persistence.TypedQuery;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
@@ -170,6 +169,18 @@ public class NotificationService implements ContainerService {
});
}
+ public boolean isPendingAlertForUserId(Long alertId, String userId) {
+ return persistenceService.doReturningTransaction(entityManager -> {
+ TypedQuery<Long> query = entityManager.createQuery(
+ "SELECT count(an) FROM AlertNotification an WHERE an.id = :alertId and an.userId =:userId and an.deliveryStatus = :deliveryStatus"
+ , Long.class);
+ query.setParameter("alertId", alertId);
+ query.setParameter("userId", userId);
+ query.setParameter("deliveryStatus", DeliveryStatus.PENDING);
+ return query.getSingleResult() > 0;
+ });
+ }
+
public void removeAlertNotification(Long id) {
persistenceService.doTransaction(entityManager -> {
Query query = entityManager.createQuery("UPDATE AlertNotification SET deliveryStatus=:status WHERE id =:id");
diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
index c03b82d52..99af82425 100644
--- a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java
@@ -74,30 +74,52 @@ public interface NotificationResource {
@SuccessStatusCode(204)
@Consumes(APPLICATION_JSON)
@RolesAllowed({"write:user"})
- void storeAlertNotification(@BeanParam RequestParams requestParams,
- AlertNotification alertNotification);
+ void storeNotificationForCurrentUser(@BeanParam RequestParams requestParams,
+ AlertNotification alertNotification);
/**
* Only the superuser can call this operation.
*/
@POST
- @Path("alert/{userId}")
+ @Path("alert/user/{userId}")
@SuccessStatusCode(204)
@Consumes(APPLICATION_JSON)
@RolesAllowed({"write:admin"})
- void storeAlertNotificationForUser(@BeanParam RequestParams requestParams,
- @PathParam("userId") String userId,
- AlertNotification alertNotification);
+ void storeNotificationForUser(@BeanParam RequestParams requestParams,
+ @PathParam("userId") String userId,
+ AlertNotification alertNotification);
+
+ /**
+ * Only the superuser can call this operation.
+ */
+ @GET
+ @Path("alert/user/{userId}")
+ @SuccessStatusCode(200)
+ @Produces(APPLICATION_JSON)
+ @RolesAllowed({"read:admin"})
+ List<AlertNotification> getNotificationsOfUser(@BeanParam RequestParams requestParams,
+ @PathParam("userId") String userId);
+
+ /**
+ * Only the superuser can call this operation.
+ */
+ @DELETE
+ @Path("alert/user/{userId}")
+ @SuccessStatusCode(204)
+ @RolesAllowed({"write:admin"})
+ void removeNotificationOfUser(@BeanParam RequestParams requestParams,
+ @PathParam("userId") String userId);
@GET
@Path("alert")
@Produces(APPLICATION_JSON)
@RolesAllowed({"write:user"})
- List<AlertNotification> getAlertNotification();
+ List<AlertNotification> getPendingNotificationsOfCurrentUser(@BeanParam RequestParams requestParams);
@DELETE
@Path("alert/{alertId}")
@SuccessStatusCode(204)
@RolesAllowed({"write:user"})
- void removeAlertNotification(@PathParam("alertId") Long id);
+ void ackPendingNotificationOfCurrentUser(@BeanParam RequestParams requestParams,
+ @PathParam("alertId") Long id);
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy
index 32a182172..3ccbbed8e 100644
--- a/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/rules/apartment/ApartmentNotifyAlarmTriggerTest.groovy
@@ -109,7 +109,7 @@ class ApartmentNotifyAlarmTriggerTest extends Specification implements ManagerCo
and: "the user should be notified"
conditions.eventually {
- def alerts = notificationResource.getAlertNotification()
+ def alerts = notificationResource.getPendingNotificationsOfCurrentUser()
assert alerts.size() == 1
assert alerts[0].title == "Apartment Alarm"
assert alerts[0].message.startsWith("Aanwezigheid in Living Room")
@@ -129,7 +129,7 @@ class ApartmentNotifyAlarmTriggerTest extends Specification implements ManagerCo
conditions.eventually {
def asset = assetStorageService.find(managerDemoSetup.apartment1LivingroomId, true)
assert asset.getAttribute("co2Level").get().valueAsInteger.orElse(null) == 444
- def alerts = notificationResource.getAlertNotification()
+ def alerts = notificationResource.getPendingNotificationsOfCurrentUser()
assert alerts.size() == 1
}
@@ -138,7 +138,7 @@ class ApartmentNotifyAlarmTriggerTest extends Specification implements ManagerCo
then: "we notify again and now have two notifications pending"
conditions.eventually {
- def alerts = notificationResource.getAlertNotification()
+ def alerts = notificationResource.getPendingNotificationsOfCurrentUser()
assert alerts.size() == 2
}
@@ -159,7 +159,7 @@ class ApartmentNotifyAlarmTriggerTest extends Specification implements ManagerCo
then: "we have still only two notifications pending"
conditions.eventually {
- def alerts = notificationResource.getAlertNotification()
+ def alerts = notificationResource.getPendingNotificationsOfCurrentUser()
assert alerts.size() == 2
}
diff --git a/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy b/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy
index a27d5a9ba..f544624dc 100644
--- a/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy
+++ b/manager/test/src/test/groovy/org/openremote/test/user/NotificationServiceTest.groovy
@@ -13,6 +13,8 @@ import org.openremote.model.user.UserQuery
import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
+import javax.ws.rs.WebApplicationException
+
import static org.openremote.container.util.MapAccess.getString
import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD
import static org.openremote.manager.server.setup.AbstractKeycloakSetup.SETUP_KEYCLOAK_ADMIN_PASSWORD_DEFAULT
@@ -33,13 +35,20 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
and: "an authenticated test user"
def realm = "customerA"
- def accessToken = authenticate(
+ def testuser3AccessToken = authenticate(
container,
realm,
KEYCLOAK_CLIENT_ID,
"testuser3",
"testuser3"
).token
+ def testuser2AccessToken = authenticate(
+ container,
+ realm,
+ KEYCLOAK_CLIENT_ID,
+ "testuser2",
+ "testuser2"
+ ).token
and: "an authenticated superuser"
def adminAccessToken = authenticate(
@@ -64,7 +73,9 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
and: "the notification resource"
// For user operations
- def notificationResource = getClientTarget(serverUri(serverPort), realm, accessToken).proxy(NotificationResource.class)
+ def testuser3NotificationResource = getClientTarget(serverUri(serverPort), realm, testuser3AccessToken).proxy(NotificationResource.class)
+ def testuser2NotificationResource = getClientTarget(serverUri(serverPort), realm, testuser2AccessToken).proxy(NotificationResource.class)
+
// For superuser operations
def adminNotificationResource = getClientTarget(serverUri(serverPort), MASTER_REALM, adminAccessToken).proxy(NotificationResource.class)
@@ -72,8 +83,8 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
notificationService.findAllUsersWithToken().isEmpty()
when: "the notification tokens of some devices are stored"
- notificationResource.storeDeviceToken(null, "device123", "token123", "ANDROID")
- notificationResource.storeDeviceToken(null, "device456", "token456", "ANDROID")
+ testuser3NotificationResource.storeDeviceToken(null, "device123", "token123", "ANDROID")
+ testuser3NotificationResource.storeDeviceToken(null, "device456", "token456", "ANDROID")
then: "the tokens should be in the database"
def tokens = adminNotificationResource.getDeviceTokens(null, keycloakDemoSetup.testuser3Id)
@@ -109,7 +120,7 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
notificationService.findAllUsersWithToken(new UserQuery().asset(new UserQuery.AssetPredicate().id(managerDemoSetup.apartment3Id))).isEmpty()
when: "the notification token of some device is updated"
- notificationResource.storeDeviceToken(null, "device456", "token789", "ANDROID")
+ testuser3NotificationResource.storeDeviceToken(null, "device456", "token789", "ANDROID")
then: "the updated token should be in the database"
def thirdToken = notificationService.findDeviceToken("device456", keycloakDemoSetup.testuser3Id)
@@ -117,17 +128,24 @@ class NotificationServiceTest extends Specification implements ManagerContainerT
notificationService.findAllTokenForUser(keycloakDemoSetup.testuser3Id).size() == 2
when: "the Alert Notification is stored"
- notificationResource.storeAlertNotification(null, notificationAlert)
- def alerts = notificationResource.getAlertNotification()
+ testuser3NotificationResource.storeNotificationForCurrentUser(null, notificationAlert)
+ def alerts = testuser3NotificationResource.getPendingNotificationsOfCurrentUser(null)
then: "it should be one alert notification pending"
alerts.size() == 1
+ when: "a user tries to acknowledge an alert notification she doesn't own"
+ testuser2NotificationResource.ackPendingNotificationOfCurrentUser(null, alerts.get(0).id)
+
+ then: "an exception should be thrown"
+ WebApplicationException ex = thrown()
+ ex.response.status == 403
+
when: "the alert Notification is acknowledged"
- notificationResource.removeAlertNotification(alerts.get(0).id)
+ testuser3NotificationResource.ackPendingNotificationOfCurrentUser(null, alerts.get(0).id)
then: "there should be no alert"
- notificationResource.getAlertNotification().size() == 0
+ testuser3NotificationResource.getPendingNotificationsOfCurrentUser(null).size() == 0
when: "a device registration is removed"
adminNotificationResource.deleteDeviceToken(null, keycloakDemoSetup.testuser3Id, "device456")