Fix user,role removed devices

pull/447/head
prathabanKavin 5 months ago
parent 9f8f25d310
commit f9cea050ae

@ -101,11 +101,12 @@ public interface EnrollmentDAO {
* Retrieves owners and the list of device IDs related to an owner. * Retrieves owners and the list of device IDs related to an owner.
* *
* @param owner the owner whose device IDs need to be retrieved * @param owner the owner whose device IDs need to be retrieved
* @param allowingDeviceStatuses statuses of devices need to be retrieved
* @param tenantId the ID of the tenant * @param tenantId the ID of the tenant
* @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user * @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user
* @throws DeviceManagementDAOException if an error occurs while fetching the data * @throws DeviceManagementDAOException if an error occurs while fetching the data
*/ */
OwnerWithDeviceDTO getOwnersWithDevices(String owner, int tenantId) throws DeviceManagementDAOException; OwnerWithDeviceDTO getOwnersWithDevices(String owner, List<String> allowingDeviceStatuses, int tenantId) throws DeviceManagementDAOException;
/** /**
* Retrieves a list of device IDs with owners and device status. * Retrieves a list of device IDs with owners and device status.

@ -564,23 +564,34 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
} }
@Override @Override
public OwnerWithDeviceDTO getOwnersWithDevices(String owner, int tenantId) public OwnerWithDeviceDTO getOwnersWithDevices(String owner, List<String> allowingDeviceStatuses, int tenantId)
throws DeviceManagementDAOException { throws DeviceManagementDAOException {
Connection conn = null; Connection conn = null;
OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO(); OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO();
List<Integer> deviceIds = new ArrayList<>(); List<Integer> deviceIds = new ArrayList<>();
int deviceCount = 0; int deviceCount = 0;
StringBuilder deviceFilters = new StringBuilder();
for (int i = 0; i < allowingDeviceStatuses.size(); i++) {
deviceFilters.append("?");
if (i < allowingDeviceStatuses.size() - 1) {
deviceFilters.append(",");
}
}
String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS AS DEVICE_STATUS, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE AS DEVICE_TYPE, e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " + String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS AS DEVICE_STATUS, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE AS DEVICE_TYPE, e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " +
"FROM DM_ENROLMENT e " + "FROM DM_ENROLMENT e " +
"JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " + "JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " +
"WHERE e.OWNER = ? AND e.TENANT_ID = ?"; "WHERE e.OWNER = ? AND e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ")";
try { try {
conn = this.getConnection(); conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, owner); stmt.setString(1, owner);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
for (int i = 0; i < allowingDeviceStatuses.size(); i++) {
stmt.setString(3 + i, allowingDeviceStatuses.get(i));
}
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) { while (rs.next()) {
if (ownerDetails.getUserName() == null) { if (ownerDetails.getUserName() == null) {

@ -5358,9 +5358,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
OwnerWithDeviceDTO ownerWithDeviceDTO; OwnerWithDeviceDTO ownerWithDeviceDTO;
List<String> allowingDeviceStatuses = new ArrayList<>();
allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.INACTIVE.toString());
allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString());
try { try {
DeviceManagementDAOFactory.openConnection(); DeviceManagementDAOFactory.openConnection();
ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, tenantId); ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId);
if (ownerWithDeviceDTO == null) { if (ownerWithDeviceDTO == null) {
String msg = "No data found for owner: " + owner; String msg = "No data found for owner: " + owner;
log.error(msg); log.error(msg);

Loading…
Cancel
Save