From 3d159c0111d1502a4c81ffecab7e1956bc4f6b8a Mon Sep 17 00:00:00 2001 From: "amalka.subasinghe" Date: Tue, 24 Oct 2023 16:24:04 +0530 Subject: [PATCH] user store role mapping feature --- .../role/mapper/UserStoreRoleMapper.java | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/userstore/role/mapper/UserStoreRoleMapper.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/userstore/role/mapper/UserStoreRoleMapper.java index 9419631b5b..9636fa442e 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/userstore/role/mapper/UserStoreRoleMapper.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.userstore.role.mapper/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/userstore/role/mapper/UserStoreRoleMapper.java @@ -75,38 +75,40 @@ public class UserStoreRoleMapper implements ServerStartupObserver { .getTenantUserRealm(MultitenantConstants.SUPER_TENANT_ID).getUserStoreManager(); for (RoleMapping mapping : roleMappings) { - String[] users = userStoreManager.getUserListOfRole(mapping.getSecondaryRole()); - if (users != null && users.length > 0) { - List primaryRoles = mapping.getInternalRoles(); - for (String role : primaryRoles) { - if (userStoreManager.isExistingRole(role)) { - String[] existingUsers = userStoreManager.getUserListOfRole(role); - List existingUserList = new ArrayList<>(Arrays.asList(existingUsers)); - List newUserList = new ArrayList<>(); - for (String user: users) { - if (existingUserList.contains(user)) { - // if contains, remove from existing list - existingUserList.remove(user); - } else { - // new user - newUserList.add(user); + if (userStoreManager.isExistingRole(mapping.getSecondaryRole())) { + String[] users = userStoreManager.getUserListOfRole(mapping.getSecondaryRole()); + if (users != null && users.length > 0) { + List primaryRoles = mapping.getInternalRoles(); + for (String role : primaryRoles) { + if (userStoreManager.isExistingRole(role)) { + String[] existingUsers = userStoreManager.getUserListOfRole(role); + List existingUserList = new ArrayList<>(Arrays.asList(existingUsers)); + List newUserList = new ArrayList<>(); + for (String user : users) { + if (existingUserList.contains(user)) { + // if contains, remove from existing list + existingUserList.remove(user); + } else { + // new user + newUserList.add(user); + } } - } - List deleteUserList = new ArrayList<>(); - if (!existingUserList.isEmpty()) { - String domain = mapping.getSecondaryRole().substring(0,mapping.getSecondaryRole().indexOf("/")); - for (String user : existingUserList) { - if (user.startsWith(domain.toUpperCase())) { - deleteUserList.add(user); + List deleteUserList = new ArrayList<>(); + if (!existingUserList.isEmpty()) { + String domain = mapping.getSecondaryRole().substring(0, mapping.getSecondaryRole().indexOf("/")); + for (String user : existingUserList) { + if (user.startsWith(domain.toUpperCase())) { + deleteUserList.add(user); + } } } - } - // update user list of given role - if (!newUserList.isEmpty() || !deleteUserList.isEmpty()) { - userStoreManager.updateUserListOfRole(role, deleteUserList.toArray(new String[0]), newUserList.toArray(new String[0])); - log.info("update user role mapping executed....."); + // update user list of given role + if (!newUserList.isEmpty() || !deleteUserList.isEmpty()) { + userStoreManager.updateUserListOfRole(role, deleteUserList.toArray(new String[0]), newUserList.toArray(new String[0])); + log.info("update user role mapping executed....."); + } } } }