Fixing https://github.com/wso2/product-iots/issues/685: When user removed, all associated devices removed

revert-70aa11f8
Rasika Perera 8 years ago
parent 6330164676
commit 186b49395e

@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.eclipse.wst.common.uriresolver.internal.util.URIEncoder; import org.eclipse.wst.common.uriresolver.internal.util.URIEncoder;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo; import org.wso2.carbon.device.mgt.core.service.EmailMetaInfo;
@ -306,13 +307,16 @@ public class UserManagementServiceImpl implements UserManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage("User '" + new ErrorResponse.ErrorResponseBuilder().setMessage("User '" +
username + "' does not exist for removal.").build()).build(); username + "' does not exist for removal.").build()).build();
} }
// Un-enroll all devices for the user
DeviceManagementProviderService deviceManagementService = DeviceMgtAPIUtils.getDeviceManagementService();
deviceManagementService.setStatus(username, EnrolmentInfo.Status.REMOVED);
userStoreManager.deleteUser(username); userStoreManager.deleteUser(username);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("User '" + username + "' was successfully removed."); log.debug("User '" + username + "' was successfully removed.");
} }
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} catch (UserStoreException e) { } catch (DeviceManagementException | UserStoreException e) {
String msg = "Exception in trying to remove user by username: " + username; String msg = "Exception in trying to remove user by username: " + username;
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(

@ -39,6 +39,8 @@ public interface EnrollmentDAO {
boolean setStatus(int enrolmentId, String currentOwner, Status status, boolean setStatus(int enrolmentId, String currentOwner, Status status,
int tenantId) throws DeviceManagementDAOException; int tenantId) throws DeviceManagementDAOException;
boolean setStatus(String currentOwner, Status status, int tenantId) throws DeviceManagementDAOException;
boolean setStatus(int enrolmentId, Status status, int tenantId) throws DeviceManagementDAOException; boolean setStatus(int enrolmentId, Status status, int tenantId) throws DeviceManagementDAOException;
Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException; Status getStatus(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException;

@ -203,6 +203,27 @@ public class EnrollmentDAOImpl implements EnrollmentDAO {
return true; return true;
} }
@Override
public boolean setStatus(String currentOwner, EnrolmentInfo.Status status,
int tenantId) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String sql = "UPDATE DM_ENROLMENT SET STATUS = ? WHERE OWNER = ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, status.toString());
stmt.setString(2, currentOwner);
stmt.setInt(3, tenantId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while setting the status of device enrolment", e);
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
return true;
}
@Override @Override
public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException { public boolean setStatus(int enrolmentID, EnrolmentInfo.Status status, int tenantId) throws DeviceManagementDAOException {
Connection conn; Connection conn;

@ -504,6 +504,8 @@ public interface DeviceManagementProviderService {
boolean setStatus(DeviceIdentifier deviceId, String currentOwner, boolean setStatus(DeviceIdentifier deviceId, String currentOwner,
EnrolmentInfo.Status status) throws DeviceManagementException; EnrolmentInfo.Status status) throws DeviceManagementException;
boolean setStatus(String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException;
void notifyOperationToDevices(Operation operation, void notifyOperationToDevices(Operation operation,
List<DeviceIdentifier> deviceIds) throws DeviceManagementException; List<DeviceIdentifier> deviceIds) throws DeviceManagementException;

@ -886,6 +886,26 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
} }
@Override
public boolean setStatus(String currentOwner,
EnrolmentInfo.Status status) throws DeviceManagementException {
try {
boolean success = false;
int tenantId = this.getTenantId();
DeviceManagementDAOFactory.beginTransaction();
success = enrollmentDAO.setStatus(currentOwner, status, tenantId);
DeviceManagementDAOFactory.commitTransaction();
return success;
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceManagementException("Error occurred while setting enrollment status", e);
} catch (TransactionManagementException e) {
throw new DeviceManagementException("Error occurred while initiating transaction", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Override @Override
@Deprecated @Deprecated
public void notifyOperationToDevices(Operation operation, List<DeviceIdentifier> deviceIds) public void notifyOperationToDevices(Operation operation, List<DeviceIdentifier> deviceIds)

Loading…
Cancel
Save