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 {