From 9c31b93937052610297cc747efb4074943351308 Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 29 Aug 2017 09:52:35 +0530 Subject: [PATCH] Fixing minor issues --- .../device/application/mgt/common/Filter.java | 11 +++++++- .../mgt/core/dao/ApplicationDAO.java | 2 +- .../GenericApplicationDAOImpl.java | 16 ++++++++++-- .../mgt/core/impl/ApplicationManagerImpl.java | 26 +++++++++++++++++-- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java index 1834d9c83fd..becd16fb21d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Filter.java @@ -46,6 +46,8 @@ public class Filter { private String sortBy; + private String userName; + public int getLimit() { return limit; } @@ -102,6 +104,14 @@ public class Filter { this.sortBy = sortBy; } + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + public boolean hasCondition() { if (filterProperties != null || searchQuery != null || filter != null) { return true; @@ -109,5 +119,4 @@ public class Filter { return false; } - } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 6c556757c38..f49a2d3f4bb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -37,7 +37,7 @@ public interface ApplicationDAO { ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException; - Application getApplication(String uuid, int tenantId) throws ApplicationManagementDAOException; + Application getApplication(String uuid, int tenantId, String userName) throws ApplicationManagementDAOException; int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index 6f921a104b2..6531ca6e3ca 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -140,12 +140,17 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE AS " + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE APP.TENANT_ID = ? "; + String userName = filter.getUserName(); + if (!userName.equals("ALL")) { + sql += " AND APP.CREATED_BY = ? "; + } if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) { sql += "AND APP.NAME LIKE ? "; } sql += "LIMIT ?,?;"; stmt = conn.prepareStatement(sql); + stmt.setString(++index, userName); stmt.setInt(++index, tenantId); if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) { stmt.setString(++index, "%" + filter.getSearchQuery() + "%"); @@ -244,7 +249,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public Application getApplication(String uuid, int tenantId) throws ApplicationManagementDAOException { + public Application getApplication(String uuid, int tenantId, String userName) throws + ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting application with the UUID(" + uuid + ") from the database"); } @@ -261,11 +267,17 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS " + "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON " + "APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE AS " - + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE UUID = ? AND APP.TENANT_ID = ?"; + + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE UUID = ? AND APP.TENANT_ID = ? "; + stmt = conn.prepareStatement(sql); stmt.setString(1, uuid); stmt.setInt(2, tenantId); + + if (!userName.equals("ALL")) { + sql += "AND APP.CREATED_BY = ?"; + stmt.setString(3, userName); + } rs = stmt.executeQuery(); if (log.isDebugEnabled()) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index f9f8dae6807..ee67fa29d56 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -174,6 +174,18 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + + try { + if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + userName = "ALL"; + } + } catch (UserStoreException e) { + throw new ApplicationManagementException("User-store exception while checking whether the user " + + userName + " of tenant " + tenantId + " has the publisher permission"); + } + filter.setUserName(userName); + try { ConnectionManagerUtil.openDBConnection(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); @@ -278,9 +290,19 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public Application getApplication(String uuid) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + + try { + if (isAuthorized(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + userName = "ALL"; + } + } catch (UserStoreException e) { + throw new ApplicationManagementException( + "User-store exception while getting application with the UUID " + uuid); + } try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationDAO().getApplication(uuid, tenantId); + return DAOFactory.getApplicationDAO().getApplication(uuid, tenantId, userName); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -304,7 +326,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { ConnectionManagerUtil.openDBConnection(); - Application application = DAOFactory.getApplicationDAO().getApplication(applicationUUID, tenantId); + Application application = DAOFactory.getApplicationDAO().getApplication(applicationUUID, tenantId,userName); return application.getUser().getUserName().equals(userName) && application.getUser().getTenantId() == tenantId; } finally {