From b66e80409157671b293dbafb205bff6247467547 Mon Sep 17 00:00:00 2001
From: Saad Sahibjan <saadsahibjan@gmail.com>
Date: Fri, 19 Jun 2020 15:22:32 +0530
Subject: [PATCH] Retrieve enrollment config from general config and support
 serilnumber based config with user based

---
 .../DeviceManagementProviderServiceImpl.java  |  3 +-
 .../mgt/core/util/DeviceManagerUtil.java      | 74 ++++++++++---------
 2 files changed, 39 insertions(+), 38 deletions(-)

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 f89dbce3adf..97125cf386b 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
@@ -231,8 +231,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
             }
             return false;
         }
-        EnrollmentConfiguration enrollmentConfiguration = DeviceManagerUtil.getEnrollmentConfigurationEntry(
-                this.getConfiguration(device.getType()));
+        EnrollmentConfiguration enrollmentConfiguration = DeviceManagerUtil.getEnrollmentConfigurationEntry();
         String deviceSerialNumber = null;
         if (enrollmentConfiguration != null) {
             deviceSerialNumber = DeviceManagerUtil.getPropertyString(device.getProperties(),
diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java
index 92fac25875c..cb5d305aeff 100644
--- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java
+++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java
@@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.core.util;
 
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.google.gson.Gson;
-import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.http.HttpResponse;
@@ -887,21 +886,15 @@ public final class DeviceManagerUtil {
 
     /**
      * 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
      */
-    public static EnrollmentConfiguration getEnrollmentConfigurationEntry(PlatformConfiguration platformConfiguration) {
-        if (platformConfiguration != null) {
-            String enrollmentConfigEntry  = platformConfiguration.getConfiguration().stream()
-                    .filter(configurationEntry -> DeviceManagementConstants.Common.ENROLLMENT_CONFIGURATION
-                            .equals(configurationEntry.getName()))
-                    .findFirst()
-                    .map(configurationEntry -> configurationEntry.getValue().toString()).orElse(null);
-            if (!StringUtils.isBlank(enrollmentConfigEntry)) {
+    public static EnrollmentConfiguration getEnrollmentConfigurationEntry() {
+            Object enrollmentConfigEntry = DeviceManagerUtil.getConfiguration(
+                    DeviceManagementConstants.Common.ENROLLMENT_CONFIGURATION);
+            if (enrollmentConfigEntry != null) {
                 Gson gson = new Gson();
-                return gson.fromJson(enrollmentConfigEntry, EnrollmentConfiguration.class);
+                return gson.fromJson(enrollmentConfigEntry.toString(), EnrollmentConfiguration.class);
             }
-        }
         return null;
     }
 
@@ -911,8 +904,9 @@ public final class DeviceManagerUtil {
      * Validation happens in two ways,
      * 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
-     * 2. List of Serial Numbers against a User - If [1] is missing and this is available checks if the device
-     * to be enrolled serial number is in the list of serial numbers which are against a User
+     * 2. List of Serial Numbers against a User - If [1] is not configured or the device serial number is not
+     * 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 deviceSerialNumber device serial number to be validated
      * @return a boolean value if the device can be enrolled
@@ -928,36 +922,44 @@ public final class DeviceManagerUtil {
             return true;
         } else {
             List<String> enrollmentConfigSerialNumbers = enrollmentConfiguration.getSerialNumbers();
+            boolean isDeviceEnrollable = false;
             if (enrollmentConfigSerialNumbers != null && !enrollmentConfigSerialNumbers.isEmpty()) {
                 if (log.isDebugEnabled()) {
                     log.debug("List of serial numbers '" + enrollmentConfigSerialNumbers.toString() + "' has been"
                               + " added for enrollment configuration under platform configuration to validate "
                               + "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 {
-                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 {
-                    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;
+                if (enrollmentConfigSerialNumbers != null && !enrollmentConfigSerialNumbers.isEmpty()) {
+                    // serial number of the device is not in the serial number based configuration nor in the serial
+                    // numbers against user based configuration
+                    return false;
+                }
+                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;
             }
         }
     }