From a044bbd385dea33a2b673a73b460c73f80db27a0 Mon Sep 17 00:00:00 2001 From: milanperera Date: Mon, 30 Sep 2019 15:44:54 +0200 Subject: [PATCH 1/2] fix entgra/product-iots#172 --- .../impl/UserManagementServiceImpl.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 090563af7b..791751eb41 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -115,6 +115,7 @@ public class UserManagementServiceImpl implements UserManagementService { private static final Log log = LogFactory.getLog(UserManagementServiceImpl.class); private static final String DEFAULT_DEVICE_USER = "Internal/devicemgt-user"; + private static final String DEFAULT_SUBSCRIBER = "Internal/subscriber"; // Permissions that are given for a normal device user. private static final Permission[] PERMISSIONS_FOR_DEVICE_USER = { @@ -159,6 +160,23 @@ public class UserManagementServiceImpl implements UserManagementService { String[] userInfoRoles = userInfo.getRoles(); tmpRoles.add(DEFAULT_DEVICE_USER); if (userInfoRoles != null) { + //check if subscriber role is coming in the payload + boolean subscriberFound = false; + for (String r : userInfoRoles) { + if (DEFAULT_SUBSCRIBER.equals(r)) { + subscriberFound = true; + break; + } + } + if (!subscriberFound) { + // Add Internal/subscriber role to new users + if (userStoreManager.isExistingRole(DEFAULT_SUBSCRIBER)) { + tmpRoles.add(DEFAULT_SUBSCRIBER); + } else { + log.warn("User: " + userInfo.getUsername() + " will not be able to enroll devices as '" + + DEFAULT_SUBSCRIBER + "' is missing in the system"); + } + } tmpRoles.addAll(Arrays.asList(userInfoRoles)); } String[] roles = new String[tmpRoles.size()]; From 576ccd2363e54a9d19d71d5ae8cb8f5c8ff8b222 Mon Sep 17 00:00:00 2001 From: milanperera Date: Mon, 30 Sep 2019 15:54:12 +0200 Subject: [PATCH 2/2] Fix issues in fix-172 --- .../impl/UserManagementServiceImpl.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java index 791751eb41..11260d9fb3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/UserManagementServiceImpl.java @@ -159,26 +159,31 @@ public class UserManagementServiceImpl implements UserManagementService { List tmpRoles = new ArrayList<>(); String[] userInfoRoles = userInfo.getRoles(); tmpRoles.add(DEFAULT_DEVICE_USER); + + boolean subscriberFound = false; + if (userInfoRoles != null) { + //check if subscriber role is coming in the payload - boolean subscriberFound = false; for (String r : userInfoRoles) { if (DEFAULT_SUBSCRIBER.equals(r)) { subscriberFound = true; break; } } - if (!subscriberFound) { - // Add Internal/subscriber role to new users - if (userStoreManager.isExistingRole(DEFAULT_SUBSCRIBER)) { - tmpRoles.add(DEFAULT_SUBSCRIBER); - } else { - log.warn("User: " + userInfo.getUsername() + " will not be able to enroll devices as '" + - DEFAULT_SUBSCRIBER + "' is missing in the system"); - } - } tmpRoles.addAll(Arrays.asList(userInfoRoles)); } + + if (!subscriberFound) { + // Add Internal/subscriber role to new users + if (userStoreManager.isExistingRole(DEFAULT_SUBSCRIBER)) { + tmpRoles.add(DEFAULT_SUBSCRIBER); + } else { + log.warn("User: " + userInfo.getUsername() + " will not be able to enroll devices as '" + + DEFAULT_SUBSCRIBER + "' is missing in the system"); + } + } + String[] roles = new String[tmpRoles.size()]; tmpRoles.toArray(roles);