Merge branch 'master' into 'master'

Retrieve enrollment config from general config and support serilnumber based config with user based

See merge request entgra/carbon-device-mgt!578
revert-70ac1926
Dharmakeerthi Lasantha 4 years ago
commit b3f395b284

@ -231,8 +231,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
} }
return false; return false;
} }
EnrollmentConfiguration enrollmentConfiguration = DeviceManagerUtil.getEnrollmentConfigurationEntry( EnrollmentConfiguration enrollmentConfiguration = DeviceManagerUtil.getEnrollmentConfigurationEntry();
this.getConfiguration(device.getType()));
String deviceSerialNumber = null; String deviceSerialNumber = null;
if (enrollmentConfiguration != null) { if (enrollmentConfiguration != null) {
deviceSerialNumber = DeviceManagerUtil.getPropertyString(device.getProperties(), deviceSerialNumber = DeviceManagerUtil.getPropertyString(device.getProperties(),

@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.core.util;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
@ -887,21 +886,15 @@ public final class DeviceManagerUtil {
/** /**
* Retrieve the Enrollment Configuration entry added to the Platform Configuration * Retrieve the Enrollment Configuration entry added to the Platform Configuration
* @param platformConfiguration which has all the platform configurations added to the tenant
* @return enrollment configuration * @return enrollment configuration
*/ */
public static EnrollmentConfiguration getEnrollmentConfigurationEntry(PlatformConfiguration platformConfiguration) { public static EnrollmentConfiguration getEnrollmentConfigurationEntry() {
if (platformConfiguration != null) { Object enrollmentConfigEntry = DeviceManagerUtil.getConfiguration(
String enrollmentConfigEntry = platformConfiguration.getConfiguration().stream() DeviceManagementConstants.Common.ENROLLMENT_CONFIGURATION);
.filter(configurationEntry -> DeviceManagementConstants.Common.ENROLLMENT_CONFIGURATION if (enrollmentConfigEntry != null) {
.equals(configurationEntry.getName()))
.findFirst()
.map(configurationEntry -> configurationEntry.getValue().toString()).orElse(null);
if (!StringUtils.isBlank(enrollmentConfigEntry)) {
Gson gson = new Gson(); Gson gson = new Gson();
return gson.fromJson(enrollmentConfigEntry, EnrollmentConfiguration.class); return gson.fromJson(enrollmentConfigEntry.toString(), EnrollmentConfiguration.class);
} }
}
return null; return null;
} }
@ -911,8 +904,9 @@ public final class DeviceManagerUtil {
* Validation happens in two ways, * Validation happens in two ways,
* 1. List of Serial Numbers - If this is available checks if the device to be enrolled serial number is * 1. List of Serial Numbers - If this is available checks if the device to be enrolled serial number is
* in the given list of serial numbers * in the given list of serial numbers
* 2. List of Serial Numbers against a User - If [1] is missing and this is available checks if the device * 2. List of Serial Numbers against a User - If [1] is not configured or the device serial number is not
* to be enrolled serial number is in the list of serial numbers which are against a User * in the [1] then this checks if the device to be enrolled serial number is in the list of serial numbers
* which are against a User
* @param enrollmentConfiguration which has the enrollment configurations of a tenant * @param enrollmentConfiguration which has the enrollment configurations of a tenant
* @param deviceSerialNumber device serial number to be validated * @param deviceSerialNumber device serial number to be validated
* @return a boolean value if the device can be enrolled * @return a boolean value if the device can be enrolled
@ -928,36 +922,44 @@ public final class DeviceManagerUtil {
return true; return true;
} else { } else {
List<String> enrollmentConfigSerialNumbers = enrollmentConfiguration.getSerialNumbers(); List<String> enrollmentConfigSerialNumbers = enrollmentConfiguration.getSerialNumbers();
boolean isDeviceEnrollable = false;
if (enrollmentConfigSerialNumbers != null && !enrollmentConfigSerialNumbers.isEmpty()) { if (enrollmentConfigSerialNumbers != null && !enrollmentConfigSerialNumbers.isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("List of serial numbers '" + enrollmentConfigSerialNumbers.toString() + "' has been" log.debug("List of serial numbers '" + enrollmentConfigSerialNumbers.toString() + "' has been"
+ " added for enrollment configuration under platform configuration to validate " + " added for enrollment configuration under platform configuration to validate "
+ "the serial number '" + deviceSerialNumber + "'."); + "the serial number '" + deviceSerialNumber + "'.");
} }
return enrollmentConfigSerialNumbers.stream().anyMatch(deviceSerialNumber::equals); isDeviceEnrollable = enrollmentConfigSerialNumbers.stream().anyMatch(deviceSerialNumber::equals);
}
if (isDeviceEnrollable) {
return true;
}
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
List<EnrollmentConfiguration.UserConfiguration> userConfigurations = enrollmentConfiguration
.getUserConfigurations();
if (userConfigurations != null && !userConfigurations.isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("List of serial numbers against users has been added for enrollment "
+ "configuration under platform configuration.");
}
return userConfigurations.stream()
.filter(userConfiguration -> username.equals(userConfiguration.getUsername())).findFirst()
.filter(userConfiguration -> userConfiguration.getSerialNumbers().stream()
.anyMatch(deviceSerialNumber::equals)).isPresent();
} else { } else {
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (enrollmentConfigSerialNumbers != null && !enrollmentConfigSerialNumbers.isEmpty()) {
List<EnrollmentConfiguration.UserConfiguration> userConfigurations = enrollmentConfiguration // serial number of the device is not in the serial number based configuration nor in the serial
.getUserConfigurations(); // numbers against user based configuration
if (userConfigurations != null && !userConfigurations.isEmpty()) { return false;
if (log.isDebugEnabled()) { }
log.debug("List of serial numbers against users has been added for enrollment " if (log.isDebugEnabled()) {
+ "configuration under platform configuration."); log.debug("Enrollment configuration has been but configuration does not contain any "
} + "serial number based validation. It may be having the configuration to push "
return userConfigurations.stream() + "devices to a specific group after a successful enrollment.");
.filter(userConfiguration -> username.equals(userConfiguration.getUsername())).findFirst()
.filter(userConfiguration -> userConfiguration.getSerialNumbers().stream()
.anyMatch(deviceSerialNumber::equals)).isPresent();
} else {
if (log.isDebugEnabled()) {
log.debug("Enrollment configuration has been but configuration does not contain any "
+ "serial number based validation. It may be having the configuration to push "
+ "devices to a specific group after a successful enrollment.");
}
// enrollment configuration has been set only to add device to a specific group and not to
// validate device against serial number
return true;
} }
// enrollment configuration has been set only to add device to a specific group and not to
// validate device against serial number
return true;
} }
} }
} }

Loading…
Cancel
Save