From fb6c98b96999783554dd50faed5f222668e9c417 Mon Sep 17 00:00:00 2001 From: Ace Date: Mon, 18 Dec 2017 12:43:37 +0530 Subject: [PATCH] Restricting clear notifications to tenant domain only --- .../service/impl/NotificationManagementServiceImpl.java | 5 ++++- .../notification/mgt/NotificationManagementService.java | 2 +- .../notification/mgt/NotificationManagementServiceImpl.java | 5 +++-- .../mgt/core/notification/mgt/dao/NotificationDAO.java | 3 ++- .../mgt/dao/impl/AbstractNotificationDAOImpl.java | 5 +++-- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java index b39493d6a06..8bfbb0cd7d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/NotificationManagementServiceImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; @@ -106,7 +107,9 @@ public class NotificationManagementServiceImpl implements NotificationManagement public Response clearAllNotifications() { Notification.Status status = Notification.Status.CHECKED; try { - DeviceMgtAPIUtils.getNotificationManagementService().updateAllNotifications(status); + int loggedinUserTenantId = CarbonContext.getThreadLocalCarbonContext() + .getTenantId(); + DeviceMgtAPIUtils.getNotificationManagementService().updateAllNotifications(status, loggedinUserTenantId); return Response.status(Response.Status.OK).build(); } catch (NotificationManagementException e) { log.error("Error encountered while trying to clear all notifications.", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java index 429f9d30e30..2f4837a4a43 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/notification/mgt/NotificationManagementService.java @@ -70,7 +70,7 @@ public interface NotificationManagementService { * @throws NotificationManagementException * if something goes wrong while updating the Notification. */ - boolean updateAllNotifications(Notification.Status status) throws + boolean updateAllNotifications(Notification.Status status, int tenantID) throws NotificationManagementException; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java index 493fc21989a..2660ba757d5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java @@ -142,13 +142,14 @@ public class NotificationManagementServiceImpl implements NotificationManagement } @Override - public boolean updateAllNotifications(Notification.Status status) throws NotificationManagementException { + public boolean updateAllNotifications(Notification.Status status, int tenantID) throws + NotificationManagementException { if (log.isDebugEnabled()) { log.debug("Attempting to clear all notifications"); } try { NotificationManagementDAOFactory.beginTransaction(); - notificationDAO.updateAllNotifications(status); + notificationDAO.updateAllNotifications(status, tenantID); NotificationManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { NotificationManagementDAOFactory.rollbackTransaction(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java index 59034c159f6..4c47d7c90e8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/NotificationDAO.java @@ -67,7 +67,8 @@ public interface NotificationDAO { * @return returns the no of updated records. * @throws NotificationManagementException */ - int updateAllNotifications(Notification.Status status) throws NotificationManagementException; + int updateAllNotifications(Notification.Status status, int tenantID) throws + NotificationManagementException; /** * This method is used to get all notifications based on tenant-id. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java index 6b7d3252893..74d63ee1593 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/dao/impl/AbstractNotificationDAOImpl.java @@ -143,16 +143,17 @@ public abstract class AbstractNotificationDAOImpl implements NotificationDAO { } @Override - public int updateAllNotifications(Notification.Status status) + public int updateAllNotifications(Notification.Status status, int tenantID) throws NotificationManagementException { Connection conn; PreparedStatement stmt = null; int rows; try { conn = NotificationManagementDAOFactory.getConnection(); - String sql = "UPDATE DM_NOTIFICATION SET STATUS = ?"; + String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE TENANT_ID= ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, status.toString()); + stmt.setInt(2, tenantID); rows = stmt.executeUpdate(); } catch (Exception e) { throw new NotificationManagementException("Error while trying to clear all " +