diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java index b909ff609b..93edca0451 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/UserManagementAdminService.java @@ -18,18 +18,11 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; -import io.swagger.annotations.SwaggerDefinition; -import io.swagger.annotations.Info; -import io.swagger.annotations.ExtensionProperty; -import io.swagger.annotations.Extension; -import io.swagger.annotations.Tag; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; -import io.swagger.annotations.ApiResponse; -import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.*; +import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; @@ -61,6 +54,12 @@ import javax.ws.rs.core.Response; description = "View Users", key = "perm:admin-users:view", permissions = {"/device-mgt/users/manage"} + ), + @Scope( + name = "Delete Users Device Information", + description = "Delete users device details", + key = "perm:admin-users:remove", + permissions = {"/device-mgt/users/manage"} ) } ) @@ -127,4 +126,131 @@ public interface UserManagementAdminService { value = "Credential.", required = true) PasswordResetWrapper credentials); + + + @Path("/{username}/devices") + @DELETE + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_DELETE, + value = "Delete a users associated devices.", + notes = "If you wish to remove an device details to comply with the privacy requirements, can be done with " + + "this resource.", + tags = "Device details remove", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin-users:remove") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Users devices and details has been deleted successfully.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body."), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 404, + message = "Group not found.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while removing the group.", + response = ErrorResponse.class) + }) + Response deleteDeviceOfUser(@ApiParam( + name = "username", + value = "Username of the users devices to be deleted.", + required = true) + @PathParam("username") String username); + + + + //device remove request would looks like follows + //DELETE devices/type/virtual_firealarm/id/us06ww93auzp + @DELETE + @Path("/type/{device-type}/id/{device-id}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + consumes = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Remove the information of device specified by device id", + notes = "Returns the status of the deleted device information.", + tags = "Device details remove privacy compliance", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin-users:remove") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully deleted the device information.", + response = Device.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest " + + "version of the requested resource."), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found. \n No device is found under the provided type and id.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving information requested device.", + response = ErrorResponse.class) + }) + Response deleteDevice( + @ApiParam( + name = "device-type", + value = "The device type, such as ios, android or windows.", + required = true) + @PathParam("device-type") + @Size(max = 45) + String deviceType, + @ApiParam( + name = "device-id", + value = "The device identifier of the device.", + required = true) + @PathParam("device-id") + @Size(max = 45) + String deviceId); + + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java index d268eff282..d695619b0b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/UserManagementAdminServiceImpl.java @@ -18,9 +18,17 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; +import io.swagger.annotations.ApiParam; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.netbeans.lib.cvsclient.commandLine.command.log; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.PrivacyComplianceException; import org.wso2.carbon.device.mgt.jaxrs.beans.PasswordResetWrapper; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.UserManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.util.CredentialManagementResponseBuilder; +import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.validation.constraints.Size; import javax.ws.rs.*; @@ -32,16 +40,45 @@ import javax.ws.rs.core.Response; @Consumes(MediaType.APPLICATION_JSON) public class UserManagementAdminServiceImpl implements UserManagementAdminService { + private static final Log log = LogFactory.getLog(UserManagementAdminServiceImpl.class); + @POST @Path("/{username}/credentials") @Override public Response resetUserPassword(@PathParam("username") @Size(max = 45) - String user, @QueryParam("domain") String domain, PasswordResetWrapper credentials) { + String user, @QueryParam("domain") String domain, PasswordResetWrapper credentials) { if (domain != null && !domain.isEmpty()) { user = domain + '/' + user; } return CredentialManagementResponseBuilder.buildResetPasswordResponse(user, credentials); } + @Override + public Response deleteDeviceOfUser(@PathParam("username") String username) { + try { + DeviceMgtAPIUtils.getPrivacyComplianceProvider().deleteDevicesOfUser(username); + return Response.status(Response.Status.OK).build(); + } catch (PrivacyComplianceException e) { + String msg = "Error occurred while deleting the devices belongs to the user."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + + @Override + public Response deleteDevice(@PathParam("device-type") @Size(max = 45) String deviceType, + @PathParam("device-id") @Size(max = 45) String deviceId) { + + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, deviceType); + DeviceMgtAPIUtils.getPrivacyComplianceProvider().deleteDeviceDetails(deviceIdentifier); + return Response.status(Response.Status.OK).build(); + } catch (PrivacyComplianceException e) { + String msg = "Error occurred while deleting the devices information."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 02f8ce62a9..a795fca1a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -49,6 +49,7 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; +import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; @@ -297,6 +298,19 @@ public class DeviceMgtAPIUtils { return realmService; } + public static PrivacyComplianceProvider getPrivacyComplianceProvider() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + PrivacyComplianceProvider privacyComplianceProvider = + (PrivacyComplianceProvider) ctx.getOSGiService(PrivacyComplianceProvider.class, null); + if (privacyComplianceProvider == null) { + String msg = "PrivacyComplianceProvider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return privacyComplianceProvider; + } + + public static IntegrationClientService getIntegrationClientService() { if (integrationClientService == null) { synchronized (DeviceMgtAPIUtils.class) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PrivacyComplianceException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PrivacyComplianceException.java new file mode 100644 index 0000000000..a9bf316de0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PrivacyComplianceException.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.common; + +public class PrivacyComplianceException extends Exception { + + private static final long serialVersionUID = -3127931192903344297L; + + public PrivacyComplianceException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public PrivacyComplianceException(String message, Throwable cause) { + super(message, cause); + } + + public PrivacyComplianceException(String msg) { + super(msg); + } + + public PrivacyComplianceException() { + super(); + } + + public PrivacyComplianceException(Throwable cause) { + super(cause); + } + +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 57338e8e19..53f7da75af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -222,7 +222,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } } DeviceManagementDAOFactory.beginTransaction(); - applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId); + applicationMappingDAO.removeApplicationMapping(device.getId(), device.getEnrolmentInfo().getId(), + appIdsToRemove, tenantId); Application installedApp; List applicationIds = new ArrayList<>(); List applicationsToMap = new ArrayList<>(); @@ -263,7 +264,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem if (log.isDebugEnabled()) { log.debug("num of app Ids:" + applicationIds.size()); } - applicationMappingDAO.addApplicationMappingsWithApps(device.getId(), applicationsToMap, tenantId); + applicationMappingDAO.addApplicationMappingsWithApps(device.getId(), device.getEnrolmentInfo().getId(), + applicationsToMap, tenantId); if (log.isDebugEnabled()) { log.debug("num of remove app Ids:" + appIdsToRemove.size()); @@ -312,7 +314,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } try { DeviceManagementDAOFactory.openConnection(); - return applicationDAO.getInstalledApplications(device.getId()); + return applicationDAO.getInstalledApplications(device.getId(), device.getEnrolmentInfo().getId()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while fetching the Application List of device " + deviceId.toString(); log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java index 3f7265396e..1808d652da 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationDAO.java @@ -34,7 +34,8 @@ public interface ApplicationDAO { Application getApplication(String identifier, String version,int tenantId) throws DeviceManagementDAOException; - Application getApplication(String identifier, String version, int deviceId, int tenantId) throws DeviceManagementDAOException; + Application getApplication(String identifier, String version, int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException; - List getInstalledApplications(int deviceId) throws DeviceManagementDAOException; + List getInstalledApplications(int deviceId, int enrolmentId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java index 10d60537b4..eb96a6185a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java @@ -29,9 +29,9 @@ public interface ApplicationMappingDAO { void addApplicationMappings(int deviceId, List applicationIds, int tenantId) throws DeviceManagementDAOException; - void addApplicationMappingsWithApps(int deviceId, List applications, int tenantId) + void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List applications, int tenantId) throws DeviceManagementDAOException; - void removeApplicationMapping(int deviceId, List appIdList, int tenantId) + void removeApplicationMapping(int deviceId, int enrolmentId, List appIdList, int tenantId) throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index bd0d7cc86a..9a76db22ac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -39,6 +39,8 @@ import org.wso2.carbon.device.mgt.core.dao.impl.device.SQLServerDeviceDAOImpl; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.impl.DeviceDetailsDAOImpl; +import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAO; +import org.wso2.carbon.device.mgt.core.privacy.dao.impl.PrivacyComplianceDAOImpl; import javax.sql.DataSource; import java.sql.Connection; @@ -151,6 +153,10 @@ public class DeviceManagementDAOFactory { return new DeviceDetailsDAOImpl(); } + public static PrivacyComplianceDAO getPrivacyComplianceDAO() { + return new PrivacyComplianceDAOImpl(); + } + public static void init(DataSourceConfig config) { dataSource = resolveDataSource(config); try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java index 3594cb2b31..ef37c22236 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractApplicationDAOImpl.java @@ -152,8 +152,8 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + - "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? " + - "AND TENANT_ID = ?"); + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM " + + "DM_APPLICATION WHERE APP_IDENTIFIER = ? AND TENANT_ID = ?"); stmt.setString(1, identifier); stmt.setInt(2, tenantId); rs = stmt.executeQuery(); @@ -171,7 +171,8 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { } @Override - public Application getApplication(String identifier, String version, int tenantId) throws DeviceManagementDAOException { + public Application getApplication(String identifier, String version, int tenantId) + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; @@ -179,8 +180,8 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + - "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM DM_APPLICATION WHERE APP_IDENTIFIER = ? " + - "AND VERSION = ? AND TENANT_ID = ?"); + "LOCATION_URL, IMAGE_URL, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID FROM " + + "DM_APPLICATION WHERE APP_IDENTIFIER = ? AND VERSION = ? AND TENANT_ID = ?"); stmt.setString(1, identifier); stmt.setString(2, version); stmt.setInt(3, tenantId); @@ -199,7 +200,8 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { } @Override - public Application getApplication(String identifier, String version, int deviceId, int tenantId) throws DeviceManagementDAOException { + public Application getApplication(String identifier, String version, int deviceId, int enrolmentId, int tenantId) + throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; @@ -208,14 +210,15 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { conn = this.getConnection(); stmt = conn.prepareStatement("SELECT ID, NAME, APP_IDENTIFIER, PLATFORM, CATEGORY, VERSION, TYPE, " + "LOCATION_URL, IMAGE_URL, appmap.APP_PROPERTIES, appmap.MEMORY_USAGE, appmap.IS_ACTIVE, TENANT_ID " + - "FROM DM_APPLICATION app INNER JOIN " + - "(SELECT APPLICATION_ID, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE FROM DM_DEVICE_APPLICATION_MAPPING W" + - "HERE DEVICE_ID = ?) appmap WHERE app.APP_IDENTIFIER = ? AND app.VERSION = ? AND " + + "FROM DM_APPLICATION app INNER JOIN (SELECT APPLICATION_ID, APP_PROPERTIES, MEMORY_USAGE, " + + "IS_ACTIVE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?) appmap " + + "WHERE app.APP_IDENTIFIER = ? AND app.VERSION = ? AND " + "appmap.APPLICATION_ID = app.id AND TENANT_ID = ?"); stmt.setInt(1, deviceId); - stmt.setString(2, identifier); - stmt.setString(3, version); - stmt.setInt(4, tenantId); + stmt.setInt(2, enrolmentId); + stmt.setString(3, identifier); + stmt.setString(4, version); + stmt.setInt(5, tenantId); rs = stmt.executeQuery(); if (rs.next()) { @@ -235,7 +238,7 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { } @Override - public List getInstalledApplications(int deviceId) throws DeviceManagementDAOException { + public List getInstalledApplications(int deviceId, int enrolmentId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; List applications = new ArrayList<>(); @@ -247,11 +250,12 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { "LOCATION_URL, IMAGE_URL, APPMAP.APP_PROPERTIES, APPMAP.MEMORY_USAGE, APPMAP.IS_ACTIVE, " + "TENANT_ID From DM_APPLICATION app INNER JOIN " + "(Select APPLICATION_ID, APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE" + - " From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=?) APPMAP " + + " From DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID=? AND ENROLMENT_ID = ?) APPMAP " + "ON " + "app.ID = APPMAP.APPLICATION_ID "); stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); rs = stmt.executeQuery(); while (rs.next()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index b3ae5f3d06..7358436ca4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -97,7 +97,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { } @Override - public void addApplicationMappingsWithApps(int deviceId, List applications, int tenantId) + public void addApplicationMappingsWithApps(int deviceId, int enrolmentId, List applications, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -108,25 +108,26 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { try { conn = this.getConnection(); - String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, APP_PROPERTIES, " + - "MEMORY_USAGE, IS_ACTIVE, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?)"; + String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, ENROLMENT_ID, APPLICATION_ID, " + + "APP_PROPERTIES, MEMORY_USAGE, IS_ACTIVE, TENANT_ID) VALUES (?, ?, ?, ?, ?, ?, ?)"; conn.setAutoCommit(false); stmt = conn.prepareStatement(sql); for (Application application : applications) { stmt.setInt(1, deviceId); - stmt.setInt(2, application.getId()); + stmt.setInt(2, enrolmentId); + stmt.setInt(3, application.getId()); bao = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bao); oos.writeObject(application.getAppProperties()); - stmt.setBytes(3, bao.toByteArray()); + stmt.setBytes(4, bao.toByteArray()); - stmt.setInt(4, application.getMemoryUsage()); - stmt.setBoolean(5, application.isActive()); + stmt.setInt(5, application.getMemoryUsage()); + stmt.setBoolean(6, application.isActive()); - stmt.setInt(6, tenantId); + stmt.setInt(7, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -155,13 +156,13 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { } @Override - public void removeApplicationMapping(int deviceId, List appIdList, + public void removeApplicationMapping(int deviceId, int enrolmentId, List appIdList, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; try { String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + - "APPLICATION_ID = ? AND TENANT_ID = ?"; + "APPLICATION_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; conn = this.getConnection(); for (int appId : appIdList) { @@ -169,6 +170,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { stmt.setInt(1, deviceId); stmt.setInt(2, appId); stmt.setInt(3, tenantId); + stmt.setInt(4, enrolmentId); stmt.execute(); } } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java index 7043e68a18..576941918f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java @@ -38,14 +38,15 @@ public interface DeviceDetailsDAO { * @param deviceInfo - Device information object. * @throws DeviceDetailsMgtDAOException */ - void addDeviceInformation(int deviceId, DeviceInfo deviceInfo) throws DeviceDetailsMgtDAOException; + void addDeviceInformation(int deviceId, int enrolmentId, DeviceInfo deviceInfo) throws DeviceDetailsMgtDAOException; /** * This method will add the device properties to the database. * @param propertyMap - device properties. * @throws DeviceDetailsMgtDAOException */ - void addDeviceProperties(Map propertyMap, int deviceId) throws DeviceDetailsMgtDAOException; + void addDeviceProperties(Map propertyMap, int deviceId, int enrolmentId) + throws DeviceDetailsMgtDAOException; /** * This method will return the device information when device id is provided. @@ -53,7 +54,7 @@ public interface DeviceDetailsDAO { * @return DeviceInfo * @throws DeviceDetailsMgtDAOException */ - DeviceInfo getDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException; + DeviceInfo getDeviceInformation(int deviceId, int enrolmentId) throws DeviceDetailsMgtDAOException; /** * This method will return the device properties from database. @@ -61,28 +62,28 @@ public interface DeviceDetailsDAO { * @return - device properties map. * @throws DeviceDetailsMgtDAOException */ - Map getDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException; + Map getDeviceProperties(int deviceId, int enrolmentId) throws DeviceDetailsMgtDAOException; /** * This method will delete the device information from the database. * @param deviceId - Integer. * @throws DeviceDetailsMgtDAOException */ - void deleteDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException; + void deleteDeviceInformation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; /** * This method will delete the device properties from database. * @param deviceId - Integer. * @throws DeviceDetailsMgtDAOException */ - void deleteDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException; + void deleteDeviceProperties(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; /** * This method will add device location to database. * @param deviceLocation - Device location with latitude and longitude. * @throws DeviceDetailsMgtDAOException */ - void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtDAOException; + void addDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) throws DeviceDetailsMgtDAOException; /** * This method will return the device location object when the device id is provided. @@ -90,14 +91,14 @@ public interface DeviceDetailsDAO { * @return - Device location object. * @throws DeviceDetailsMgtDAOException */ - DeviceLocation getDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException; + DeviceLocation getDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; /** * This method will delete the device location from the database. * @param deviceId * @throws DeviceDetailsMgtDAOException */ - void deleteDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException; + void deleteDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; // /** // * This method will add device application to database. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index d18bac288f..54fc1a4738 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -41,7 +41,8 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { private static Log log = LogFactory.getLog(DeviceDetailsDAOImpl.class); @Override - public void addDeviceInformation(int deviceId, DeviceInfo deviceInfo) throws DeviceDetailsMgtDAOException { + public void addDeviceInformation(int deviceId, int enrolmentId, DeviceInfo deviceInfo) + throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; @@ -51,8 +52,8 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_DETAIL (DEVICE_ID, DEVICE_MODEL, " + "VENDOR, OS_VERSION, OS_BUILD_DATE, BATTERY_LEVEL, INTERNAL_TOTAL_MEMORY, INTERNAL_AVAILABLE_MEMORY, " + "EXTERNAL_TOTAL_MEMORY, EXTERNAL_AVAILABLE_MEMORY, CONNECTION_TYPE, " + - "SSID, CPU_USAGE, TOTAL_RAM_MEMORY, AVAILABLE_RAM_MEMORY, PLUGGED_IN, UPDATE_TIMESTAMP) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); + "SSID, CPU_USAGE, TOTAL_RAM_MEMORY, AVAILABLE_RAM_MEMORY, PLUGGED_IN, UPDATE_TIMESTAMP, ENROLMENT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); stmt.setInt(1, deviceId); stmt.setString(2, deviceInfo.getDeviceModel()); @@ -71,6 +72,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt.setDouble(15, deviceInfo.getAvailableRAMMemory()); stmt.setBoolean(16, deviceInfo.isPluggedIn()); stmt.setLong(17, System.currentTimeMillis()); + stmt.setInt(18, enrolmentId); stmt.execute(); @@ -83,7 +85,8 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public void addDeviceProperties(Map propertyMap, int deviceId) throws DeviceDetailsMgtDAOException { + public void addDeviceProperties(Map propertyMap, int deviceId, int enrolmentId) + throws DeviceDetailsMgtDAOException { if (propertyMap.isEmpty()) { if(log.isDebugEnabled()) { @@ -95,12 +98,14 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_INFO (DEVICE_ID, KEY_FIELD, VALUE_FIELD) VALUES (?, ?, ?)"); + stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_INFO (DEVICE_ID, KEY_FIELD, VALUE_FIELD, ENROLMENT_ID) " + + "VALUES (?, ?, ?, ?)"); for (Map.Entry entry : propertyMap.entrySet()) { stmt.setInt(1, deviceId); stmt.setString(2, entry.getKey()); stmt.setString(3, entry.getValue()); + stmt.setInt(4, enrolmentId); stmt.addBatch(); } stmt.executeBatch(); @@ -113,7 +118,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public DeviceInfo getDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException { + public DeviceInfo getDeviceInformation(int deviceId, int enrolmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; @@ -121,9 +126,10 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { try { conn = this.getConnection(); - String sql = "SELECT * FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; + String sql = "SELECT * FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); rs = stmt.executeQuery(); if (rs.next()) { @@ -159,7 +165,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public Map getDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException { + public Map getDeviceProperties(int deviceId, int enrolmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; @@ -167,9 +173,10 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { Map map = new HashMap<>(); try { conn = this.getConnection(); - String sql = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?"; + String sql = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); rs = stmt.executeQuery(); while (rs.next()) { @@ -184,15 +191,16 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public void deleteDeviceInformation(int deviceId) throws DeviceDetailsMgtDAOException { + public void deleteDeviceInformation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ?"; + String query = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, enrollmentId); stmt.executeUpdate(); } catch (SQLException e) { @@ -203,15 +211,16 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public void deleteDeviceProperties(int deviceId) throws DeviceDetailsMgtDAOException { + public void deleteDeviceProperties(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ?"; + String query = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, enrollmentId); stmt.executeUpdate(); } catch (SQLException e) { @@ -222,14 +231,15 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtDAOException { + public void addDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_LOCATION (DEVICE_ID, LATITUDE, LONGITUDE, STREET1, " + - "STREET2, CITY, ZIP, STATE, COUNTRY, GEO_HASH, UPDATE_TIMESTAMP) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?,?)"); + "STREET2, CITY, ZIP, STATE, COUNTRY, GEO_HASH, UPDATE_TIMESTAMP, ENROLMENT_ID) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); stmt.setInt(1, deviceLocation.getDeviceId()); stmt.setDouble(2, deviceLocation.getLatitude()); stmt.setDouble(3, deviceLocation.getLongitude()); @@ -241,6 +251,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt.setString(9, deviceLocation.getCountry()); stmt.setString(10, GeoHashGenerator.encodeGeohash(deviceLocation)); stmt.setLong(11, System.currentTimeMillis()); + stmt.setInt(12, enrollmentId); stmt.execute(); } catch (SQLException e) { throw new DeviceDetailsMgtDAOException("Error occurred while adding the device location to database.", e); @@ -250,7 +261,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public DeviceLocation getDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException { + public DeviceLocation getDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; @@ -258,9 +269,10 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { DeviceLocation location = new DeviceLocation(); try { conn = this.getConnection(); - String sql = "SELECT * FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?"; + String sql = "SELECT * FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); + stmt.setInt(2, enrollmentId); rs = stmt.executeQuery(); while (rs.next()) { @@ -286,15 +298,16 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } @Override - public void deleteDeviceLocation(int deviceId) throws DeviceDetailsMgtDAOException { + public void deleteDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ?"; + String query = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); + stmt.setInt(2, enrollmentId); stmt.executeUpdate(); } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 385ab18477..8c9f357b19 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -68,12 +68,14 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { DeviceManagementDAOFactory.beginTransaction(); deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceDetailsDAO.deleteDeviceInformation(device.getId()); - deviceDetailsDAO.deleteDeviceProperties(device.getId()); - deviceDetailsDAO.addDeviceInformation(device.getId(), deviceInfo); - deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId()); + deviceDetailsDAO.deleteDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); + deviceDetailsDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId()); + deviceDetailsDAO.addDeviceInformation(device.getId(), device.getEnrolmentInfo().getId(), deviceInfo); + deviceDetailsDAO.addDeviceProperties(deviceInfo.getDeviceDetailsMap(), device.getId(), + device.getEnrolmentInfo().getId()); DeviceManagementDAOFactory.commitTransaction(); + //TODO :: This has to be fixed by adding the enrollment ID. if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; Object[] payload = new Object[]{ @@ -131,8 +133,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } try { DeviceManagementDAOFactory.openConnection(); - DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId()); - deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(device.getId())); + DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), + device.getEnrolmentInfo().getId()); + deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(device.getId(), + device.getEnrolmentInfo().getId())); return deviceInfo; } catch (SQLException e) { @@ -154,19 +158,21 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { identifierMap.put(identifier.getId(), identifier); } try { - List deviceIds = new ArrayList<>(); + List deviceIds = new ArrayList<>(); List devices = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). getAllDevices(false); for (Device device : devices) { if (identifierMap.containsKey(device.getDeviceIdentifier()) && device.getType().equals(identifierMap.get(device.getDeviceIdentifier()).getType())) { - deviceIds.add(device.getId()); + deviceIds.add(device); } } DeviceManagementDAOFactory.openConnection(); - for (Integer id : deviceIds) { - DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id); - deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id)); + for (Device device : deviceIds) { + DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(device.getId(), + device.getEnrolmentInfo().getId()); + deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(device.getId(), + device.getEnrolmentInfo().getId())); deviceInfos.add(deviceInfo); } } catch (SQLException e) { @@ -190,8 +196,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceLocation.setDeviceId(device.getId()); DeviceManagementDAOFactory.beginTransaction(); deviceDAO.updateDevice(device, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId()); - deviceDetailsDAO.addDeviceLocation(deviceLocation); + deviceDetailsDAO.deleteDeviceLocation(deviceLocation.getDeviceId(), device.getEnrolmentInfo().getId()); + deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); + //TODO: This has to be fixed with enrollment id or username should include in the stream def. if (DeviceManagerUtil.isPublishLocationResponseEnabled()) { Object[] metaData = {device.getDeviceIdentifier(), device.getType()}; Object[] payload = new Object[]{ @@ -233,7 +240,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } try { DeviceManagementDAOFactory.openConnection(); - return deviceDetailsDAO.getDeviceLocation(device.getId()); + return deviceDetailsDAO.getDeviceLocation(device.getId(), device.getEnrolmentInfo().getId()); } catch (SQLException e) { throw new DeviceDetailsMgtException("SQL error occurred while retrieving device from database.", e); } catch (DeviceDetailsMgtDAOException e) { @@ -270,7 +277,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { List deviceLocations = new ArrayList<>(); DeviceManagementDAOFactory.openConnection(); for (Device device : devices) { - deviceLocations.add(deviceDetailsDAO.getDeviceLocation(device.getId())); + deviceLocations.add(deviceDetailsDAO.getDeviceLocation(device.getId(), + device.getEnrolmentInfo().getId())); } return deviceLocations; } catch (DeviceManagementException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index f6a3a3fc83..4f8cd365ab 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -29,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; +import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; @@ -67,6 +68,7 @@ public class DeviceManagementDataHolder { private DeviceTaskManagerService deviceTaskManagerService; private DeviceStatusTaskManagerService deviceStatusTaskManagerService; private DeviceTypeGeneratorService deviceTypeGeneratorService; + private PrivacyComplianceProvider privacyComplianceProvider; private Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap( new HashMap()); @@ -266,4 +268,12 @@ public class DeviceManagementDataHolder { DeviceTypeGeneratorService deviceTypeGeneratorService) { this.deviceTypeGeneratorService = deviceTypeGeneratorService; } + + public PrivacyComplianceProvider getPrivacyComplianceProvider() { + return privacyComplianceProvider; + } + + public void setPrivacyComplianceProvider(PrivacyComplianceProvider privacyComplianceProvider) { + this.privacyComplianceProvider = privacyComplianceProvider; + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 47d709ac4e..65decec77f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -51,6 +51,8 @@ import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManageme import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionManagerServiceImpl; +import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; +import org.wso2.carbon.device.mgt.core.privacy.impl.PrivacyComplianceProviderImpl; import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository; import org.wso2.carbon.device.mgt.core.push.notification.mgt.task.PushNotificationSchedulerTask; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -225,6 +227,12 @@ public class DeviceManagementServiceComponent { .getPushNotificationConfiguration().getSchedulerTaskInitialDelay(), config .getPushNotificationConfiguration().getSchedulerBatchDelayMills(), TimeUnit.MILLISECONDS); } + + PrivacyComplianceProvider privacyComplianceProvider = new PrivacyComplianceProviderImpl(); + DeviceManagementDataHolder.getInstance().setPrivacyComplianceProvider(privacyComplianceProvider); + componentContext.getBundleContext().registerService(PrivacyComplianceProvider.class.getName(), + privacyComplianceProvider, null); + if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/PrivacyComplianceProvider.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/PrivacyComplianceProvider.java new file mode 100644 index 0000000000..e661dbb212 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/PrivacyComplianceProvider.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.privacy; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.PrivacyComplianceException; + +public interface PrivacyComplianceProvider { + + void deleteDevicesOfUser(String username) throws PrivacyComplianceException; + + void deleteDeviceDetails(DeviceIdentifier deviceIdentifier) throws PrivacyComplianceException; + +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/PrivacyComplianceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/PrivacyComplianceDAO.java new file mode 100644 index 0000000000..d1b3862743 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/PrivacyComplianceDAO.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.privacy.dao; + +import org.wso2.carbon.device.mgt.core.privacy.impl.DeviceEnrollmentMapping; + +import java.util.List; + +public interface PrivacyComplianceDAO { + + List getDevicesOfUser(String username, int tenantId) throws PrivacyComplianceDAOException; + + void deleteDevice(int deviceId, int tenantId) throws PrivacyComplianceDAOException; + + void deleteDeviceEnrollments(int deviceId, int tenantId) throws PrivacyComplianceDAOException; + + void deleteDeviceEnrollments(int deviceId, int enrolmentId, int tenantId) throws PrivacyComplianceDAOException; + + void deleteDeviceDetails(int deviceId, int enrolmentId) throws PrivacyComplianceDAOException; + + void deleteDeviceApplications(int deviceId, int enrolmentId, int tenantId) throws PrivacyComplianceDAOException; + + void deleteDeviceProperties(int deviceId, int enrolmentId, int tenantId) throws PrivacyComplianceDAOException; + + void deleteDeviceLocation(int deviceId, int enrolmentId) throws PrivacyComplianceDAOException; + + void updateDeviceOperationResponses(int enrolmentId) throws PrivacyComplianceDAOException; + + void deleteDeviceOperationDetails(int enrolmentId) throws PrivacyComplianceDAOException; + + void deleteOperationEnrolmentMappings(int enrolmentId) throws PrivacyComplianceDAOException; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/PrivacyComplianceDAOException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/PrivacyComplianceDAOException.java new file mode 100644 index 0000000000..152a696b7b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/PrivacyComplianceDAOException.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.privacy.dao; + +public class PrivacyComplianceDAOException extends Exception { + + private static final long serialVersionUID = -3127931192903344297L; + + public PrivacyComplianceDAOException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public PrivacyComplianceDAOException(String message, Throwable cause) { + super(message, cause); + } + + public PrivacyComplianceDAOException(String msg) { + super(msg); + } + + public PrivacyComplianceDAOException() { + super(); + } + + public PrivacyComplianceDAOException(Throwable cause) { + super(cause); + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java new file mode 100644 index 0000000000..7c2b7d25b9 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/dao/impl/PrivacyComplianceDAOImpl.java @@ -0,0 +1,280 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.privacy.dao.impl; + +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; +import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAO; +import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAOException; +import org.wso2.carbon.device.mgt.core.privacy.impl.DeviceEnrollmentMapping; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class PrivacyComplianceDAOImpl implements PrivacyComplianceDAO { + + @Override + public List getDevicesOfUser(String username, int tenantId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List deviceIds = new ArrayList<>(); + try { + conn = this.getConnection(); + String sql = "SELECT * FROM DM_ENROLMENT WHERE OWNER = ? AND TENANT_ID = ? ORDER BY DEVICE_ID"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, username); + stmt.setInt(2, tenantId); + stmt.executeUpdate(); + + while (rs.next()) { + DeviceEnrollmentMapping mapping = new DeviceEnrollmentMapping(); + mapping.setDeviceId(rs.getInt("DEVICE_ID")); + mapping.setEnrolmentId(rs.getInt("ENROLMENT_ID")); + deviceIds.add(mapping); + } + if (deviceIds.isEmpty()) { + return null; + } + return deviceIds; + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while retrieving device ids " + + "related to the given user.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + @Override + public void deleteDevice(int deviceId, int tenantId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_DEVICE WHERE ID = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the devices", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteDeviceEnrollments(int deviceId, int tenantId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device enrolments", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteDeviceEnrollments(int deviceId, int enrolmentId, int tenantId) + throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_ENROLMENT WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + stmt.setInt(3, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device enrolments", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteDeviceDetails(int deviceId, int enrolmentId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_DEVICE_DETAIL WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device details.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteDeviceApplications(int deviceId, int enrolmentId, int tenantId) + throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? " + + "AND ENROLMENT_ID = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); + stmt.setInt(3, tenantId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device applications.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteDeviceProperties(int deviceId, int enrolmentId, int tenantId) + throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_DEVICE_INFO WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteDeviceLocation(int deviceId, int enrolmentId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device location.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void updateDeviceOperationResponses(int enrolmentId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "UPDATE DM_DEVICE_OPERATION_RESPONSE SET OPERATION_RESPONSE = ? WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setNull(1, java.sql.Types.BLOB); + stmt.setInt(2, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + + @Override + public void deleteDeviceOperationDetails(int enrolmentId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_DEVICE_OPERATION_RESPONSE WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void deleteOperationEnrolmentMappings(int enrolmentId) throws PrivacyComplianceDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, enrolmentId); + stmt.executeUpdate(); + + } catch (SQLException e) { + throw new PrivacyComplianceDAOException("Error occurred while deleting the device information.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + private Connection getConnection() throws SQLException { + return DeviceManagementDAOFactory.getConnection(); + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/DeviceEnrollmentMapping.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/DeviceEnrollmentMapping.java new file mode 100644 index 0000000000..1acd1aa46e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/DeviceEnrollmentMapping.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.privacy.impl; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + +public class DeviceEnrollmentMapping { + + private int deviceId; + private int enrolmentId; + private DeviceIdentifier deviceIdentifier; + private int tenantId; + + public int getDeviceId() { + return deviceId; + } + + public void setDeviceId(int deviceId) { + this.deviceId = deviceId; + } + + public int getEnrolmentId() { + return enrolmentId; + } + + public void setEnrolmentId(int enrolmentId) { + this.enrolmentId = enrolmentId; + } + + public DeviceIdentifier getDeviceIdentifier() { + return deviceIdentifier; + } + + public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java new file mode 100644 index 0000000000..072bf19c17 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/privacy/impl/PrivacyComplianceProviderImpl.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.core.privacy.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.PrivacyComplianceException; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; +import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; +import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAO; +import org.wso2.carbon.device.mgt.core.privacy.dao.PrivacyComplianceDAOException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class PrivacyComplianceProviderImpl implements PrivacyComplianceProvider { + + private static final Log log = LogFactory.getLog(PrivacyComplianceProviderImpl.class); + + PrivacyComplianceDAO complianceDAO; + + public PrivacyComplianceProviderImpl() { + complianceDAO = DeviceManagementDAOFactory.getPrivacyComplianceDAO(); + } + + @Override + public void deleteDevicesOfUser(String username) throws PrivacyComplianceException { + + if (log.isDebugEnabled()) { + log.debug("Deleting the requested users."); + } + try { + DeviceManagementDAOFactory.beginTransaction(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + List enrollmentMappings = complianceDAO.getDevicesOfUser(username, tenantId); + Map> deviceMap = new HashMap<>(); + int x = -1; + for (DeviceEnrollmentMapping m : enrollmentMappings) { + if (m.getDeviceId() != x) { + x = m.getDeviceId(); + List enrolments = new ArrayList<>(); + enrolments.add(m.getEnrolmentId()); + deviceMap.put(m.getDeviceId(), enrolments); + } else { + deviceMap.get(m.getDeviceId()).add(m.getEnrolmentId()); + } + } + for (int deviceId : deviceMap.keySet()) { + List enrollmentIds = deviceMap.get(deviceId); + for (Integer enrolmentId : enrollmentIds) { + complianceDAO.deleteDeviceOperationDetails(enrolmentId); + complianceDAO.deleteOperationEnrolmentMappings(enrolmentId); + complianceDAO.deleteDeviceApplications(deviceId, enrolmentId, tenantId); + complianceDAO.deleteDeviceDetails(deviceId, enrolmentId); + complianceDAO.deleteDeviceProperties(deviceId, enrolmentId, tenantId); + complianceDAO.deleteDeviceLocation(deviceId, enrolmentId); + complianceDAO.deleteDeviceEnrollments(deviceId, enrolmentId); + } + complianceDAO.deleteDevice(deviceId, tenantId); + } + DeviceManagementDAOFactory.commitTransaction(); + } catch (PrivacyComplianceDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while deleting the devices and details of the given user"; + log.error(msg, e); + throw new PrivacyComplianceException(msg, e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Database error occurred while deleting the devices and details of the given user"; + log.error(msg, e); + throw new PrivacyComplianceException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + log.info("Requested users device has been successfully removed..!"); + } + + @Override + public void deleteDeviceDetails(DeviceIdentifier deviceIdentifier) throws PrivacyComplianceException { + + if (log.isDebugEnabled()) { + log.debug("Deleting the requested device details."); + } + try { + Device device = this.getDevice(deviceIdentifier); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementDAOFactory.beginTransaction(); + complianceDAO.deleteDeviceOperationDetails(device.getEnrolmentInfo().getId()); + complianceDAO.deleteOperationEnrolmentMappings(device.getEnrolmentInfo().getId()); + complianceDAO.deleteDeviceApplications(device.getId(), device.getEnrolmentInfo().getId(), tenantId); + complianceDAO.deleteDeviceDetails(device.getId(), device.getEnrolmentInfo().getId()); + complianceDAO.deleteDeviceProperties(device.getId(), device.getEnrolmentInfo().getId(), tenantId); + complianceDAO.deleteDeviceLocation(device.getId(), device.getEnrolmentInfo().getId()); + DeviceManagementDAOFactory.commitTransaction(); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Database error occurred while deleting the device details."; + log.error(msg, e); + throw new PrivacyComplianceException(msg, e); + } catch (PrivacyComplianceDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while deleting the device details."; + log.error(msg, e); + throw new PrivacyComplianceException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + + } + + private Device getDevice(DeviceIdentifier deviceId) throws PrivacyComplianceException { + try { + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false); + } catch (DeviceManagementException e) { + throw new PrivacyComplianceException( + "Error occurred while retrieving device info.", e); + } + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java index f96e2e4048..e77f700fa8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/search/mgt/impl/ProcessorImpl.java @@ -236,7 +236,8 @@ public class ProcessorImpl implements Processor { try { DeviceManagementDAOFactory.openConnection(); for (Device device : devices) { - device.setApplications(applicationDAO.getInstalledApplications(device.getId())); + device.setApplications(applicationDAO.getInstalledApplications(device.getId(), + device.getEnrolmentInfo().getId())); } } catch (DeviceManagementDAOException e) { throw new SearchMgtException("Error occurred while fetching the Application List of devices ", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 2b5c88b036..798b9b74de 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -2475,8 +2475,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceInfo info = null; try { DeviceManagementDAOFactory.openConnection(); - info = deviceInfoDAO.getDeviceInformation(device.getId()); - DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); + info = deviceInfoDAO.getDeviceInformation(device.getId(), device.getEnrolmentInfo().getId()); + DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId(), + device.getEnrolmentInfo().getId()); if (location != null) { //There are some cases where the device-info is not updated properly. Hence returning a null value. if (info != null) { @@ -2507,23 +2508,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv /** * Returns all the installed apps of the given device. */ - private List getInstalledApplications(Device device) { + private List getInstalledApplications(Device device) throws DeviceManagementException { if (log.isDebugEnabled()) { log.debug("Get installed applications of device: " + device.getId() + " of type '" + device.getType() + "'"); } List applications = new ArrayList<>(); try { DeviceManagementDAOFactory.openConnection(); - applications = applicationDAO.getInstalledApplications(device.getId()); + applications = applicationDAO.getInstalledApplications(device.getId(), device.getEnrolmentInfo().getId()); device.setApplications(applications); } catch (DeviceManagementDAOException e) { - log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " + - "which carries the id '" + device.getId() + "'", e); + String msg = "Error occurred while retrieving the application list of '" + device.getType() + "', " + + "which carries the id '" + device.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred in getInstalledApplications"; log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index badedd8d56..22d7098c74 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -506,7 +506,8 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes //Device details table will be reffered when looking for last updated time //This dao entry is to mimic a device info operation - deviceDetailsDAO.addDeviceInformation(initialDevice.getId(), TestDataHolder + deviceDetailsDAO.addDeviceInformation(initialDevice.getId(), initialDevice.getEnrolmentInfo().getId(), + TestDataHolder .generateDummyDeviceInfo()); DeviceManagementDAOFactory.closeConnection(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index e7893e242b..845b0d0e36 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -301,7 +301,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( POLICY_CRITERION_ID INT NOT NULL, PROP_KEY VARCHAR(45) NULL, PROP_VALUE VARCHAR(100) NULL, - CONTENT BLOB NULL COMMENT 'This is used to ', + CONTENT BLOB NULL, PRIMARY KEY (ID), CONSTRAINT FK_POLICY_CRITERIA_PROPERTIES FOREIGN KEY (POLICY_CRITERION_ID) @@ -363,19 +363,32 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( PRIMARY KEY (ID) ); + CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, APPLICATION_ID INTEGER NOT NULL, APP_PROPERTIES BLOB NULL, MEMORY_USAGE INTEGER(10) NULL, IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), - CONSTRAINT fk_dm_device FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_dm_application FOREIGN KEY (APPLICATION_ID) REFERENCES - DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + CONSTRAINT fk_dm_device + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT fk_dm_application + FOREIGN KEY (APPLICATION_ID) + REFERENCES DM_APPLICATION (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_APP_MAP_DM_ENROL + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION ); -- POLICY RELATED TABLES FINISHED -- @@ -400,6 +413,7 @@ CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INT NULL, + ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, VALUE_FIELD VARCHAR(100) NULL, PRIMARY KEY (ID), @@ -407,12 +421,18 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT DM_DEVICE_INFO_DEVICE_ENROLLMENT + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION ON UPDATE NO ACTION ); CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INT NULL, + ENROLMENT_ID INT NOT NULL, LATITUDE DOUBLE NULL, LONGITUDE DOUBLE NULL, STREET1 VARCHAR(255) NULL, @@ -428,14 +448,19 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT DM_DEVICE_LOCATION_DM_ENROLLMENT + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION ON UPDATE NO ACTION ); - CREATE INDEX DM_DEVICE_LOCATION_GEO_hashx ON DM_DEVICE_LOCATION(GEO_HASH ASC); CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ID INT NOT NULL AUTO_INCREMENT, DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT NOT NULL, DEVICE_MODEL VARCHAR(45) NULL, VENDOR VARCHAR(45) NULL, OS_VERSION VARCHAR(45) NULL, @@ -457,6 +482,11 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_ENROLMENT_DEVICE_DETAILS + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION ON UPDATE NO ACTION ); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.register/register.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.register/register.hbs index ab12ae05fa..b9977f909b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.register/register.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.register/register.hbs @@ -59,6 +59,13 @@ class="form-control" placeholder="Confirm Password"/> +
+
+