From 6c3a0fbc84202be0cfafcf9a1af7901a974eebf0 Mon Sep 17 00:00:00 2001 From: LakshikaAthapaththu Date: Wed, 16 Aug 2023 13:02:18 +0530 Subject: [PATCH] Refactoring --- .../scim2/common/impl/SCIMUserManager.java | 41 +++---------------- .../common/listener/SCIMGroupResolver.java | 25 ++++++----- 2 files changed, 20 insertions(+), 46 deletions(-) diff --git a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/impl/SCIMUserManager.java b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/impl/SCIMUserManager.java index e90a87be7..fd6a9ea24 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/impl/SCIMUserManager.java +++ b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/impl/SCIMUserManager.java @@ -2626,26 +2626,15 @@ public Group createGroup(Group group, Map requiredAttributes) } } } - // Add other scim attributes in the identity DB since user store doesn't support some attributes. - // Commented by Lakshi as we don't need to save this in the identity DB if there is ID support - // enabled in user store level. -// SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(carbonUM.getTenantId()); -// scimGroupHandler.createSCIMAttributes(group); -// carbonUM.addRoleWithID(group.getDisplayName(), members.toArray(new String[0]), null, false); - coreGroup = carbonUM.addGroupWithID(group.getDisplayName(), members.toArray(new String[0]), group.getDisplayName(), - group.getId(), group.getCreatedDate(), group.getLastModified(), group.getLocation()); + coreGroup = carbonUM.addGroupWithID(group.getDisplayName(), group.getId(), + members.toArray(new String[0]), group.getCreatedDateTime(), group.getLastModifiedDateTime(), + group.getLocation()); if (log.isDebugEnabled()) { log.debug("Group: " + group.getDisplayName() + " is created through SCIM."); } } else { - // Add other scim attributes in the identity DB since user store doesn't support some attributes. - // Commented by Lakshi as we don't need to save this in the identity DB if there is ID support - // enabled in user store level. -// SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(carbonUM.getTenantId()); -// scimGroupHandler.createSCIMAttributes(group); -// carbonUM.addRoleWithID(group.getDisplayName(), null, null, false); - coreGroup = carbonUM.addGroupWithID(group.getDisplayName(), null, group.getDisplayName(), - group.getId(), group.getCreatedDate(), group.getLastModified(), group.getLocation()); + coreGroup = carbonUM.addGroupWithID(group.getDisplayName(), group.getId(), null, + group.getCreatedDateTime(), group.getLastModifiedDateTime(), group.getLocation()); if (log.isDebugEnabled()) { log.debug("Group: " + group.getDisplayName() + " is created through SCIM."); @@ -2656,15 +2645,6 @@ public Group createGroup(Group group, Map requiredAttributes) group.setId(coreGroup.getGroupID()); } } catch (UserStoreException e) { - // Commented by Lakshi as we don't need to save this in the identity DB if there is ID support - // enabled in user store level. -// try { -// SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(carbonUM.getTenantId()); -// scimGroupHandler.deleteGroupAttributes(group.getDisplayName()); -// } catch (UserStoreException | IdentitySCIMException ex) { -// throw resolveError(e, "Error occurred while doing rollback operation of the SCIM " + -// "table entry for role: " + group.getDisplayName()); -// } handleErrorsOnRoleNamePolicy(e); throw resolveError(e, "Error occurred while adding role : " + group.getDisplayName()); } catch (IdentitySCIMException | BadRequestException e) { @@ -2787,11 +2767,6 @@ public void deleteGroup(String groupId) throws NotFoundException, CharonExceptio // Set thread local property to signal the downstream SCIMUserOperationListener // about the provisioning route. SCIMCommonUtils.setThreadLocalIsManagedThroughSCIMEP(true); - - // Get group name by id. -// SCIMGroupHandler groupHandler = new SCIMGroupHandler(carbonUM.getTenantId()); -// String groupName = groupHandler.getGroupName(groupId); - String groupName = carbonUM.getGroupNameByGroupId(groupId); if (groupName != null) { @@ -2816,11 +2791,9 @@ public void deleteGroup(String groupId) throws NotFoundException, CharonExceptio } //delete group in carbon UM -// carbonUM.deleteRole(groupName); carbonUM.deleteGroupWithID(groupId); carbonUM.removeGroupRoleMappingByGroupName(groupName); - //we do not update Identity_SCIM DB here since it is updated in SCIMUserOperationListener's methods. if (log.isDebugEnabled()) { log.debug("Group: " + groupName + " is deleted through SCIM."); } @@ -2834,10 +2807,6 @@ public void deleteGroup(String groupId) throws NotFoundException, CharonExceptio } catch (UserStoreException e) { throw resolveError(e, "Error occurred while deleting group " + groupId); } -// catch (IdentitySCIMException e) { -// throw new CharonException("Error occurred while deleting group " + groupId, e); -// } - } @Override diff --git a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/listener/SCIMGroupResolver.java b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/listener/SCIMGroupResolver.java index 1311b3c15..e6af23a0c 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/listener/SCIMGroupResolver.java +++ b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/listener/SCIMGroupResolver.java @@ -48,6 +48,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -628,27 +629,30 @@ private String createSearchValueForEwOperation(String attributeName, String filt } @Override - public void addGroup(String groupID, Date createdDate, Date lastModifiedDate, String location, - String displayName, int tenantId) throws UserStoreException { + public boolean addGroup(String displayName, String groupID, LocalDateTime createdDate, + LocalDateTime lastModifiedDate, String location, int tenantId) throws UserStoreException { + + Map attributes = new HashMap<>(); + attributes.put(SCIMConstants.CommonSchemaConstants.ID_URI, groupID); + attributes.put(SCIMConstants.CommonSchemaConstants.CREATED_URI, AttributeUtil.formatDateTime( + createdDate.toInstant(ZoneOffset.UTC))); + attributes.put(SCIMConstants.CommonSchemaConstants.LAST_MODIFIED_URI, AttributeUtil.formatDateTime( + lastModifiedDate.toInstant(ZoneOffset.UTC))); + attributes.put(SCIMConstants.CommonSchemaConstants.LOCATION_URI, location); try { - Map attributes = new HashMap<>(); - attributes.put(SCIMConstants.CommonSchemaConstants.ID_URI, groupID); - attributes.put(SCIMConstants.CommonSchemaConstants.CREATED_URI, AttributeUtil.formatDateTime( - createdDate.toInstant())); - attributes.put(SCIMConstants.CommonSchemaConstants.LAST_MODIFIED_URI, AttributeUtil.formatDateTime( - lastModifiedDate.toInstant())); - attributes.put(SCIMConstants.CommonSchemaConstants.LOCATION_URI, location); GroupDAO groupDAO = new GroupDAO(); groupDAO.addSCIMGroupAttributes(tenantId, displayName, attributes); } catch (IdentitySCIMException e) { throw new UserStoreException(String.format("Error occurred while saving the " + "group: %s in tenant: %s", displayName, tenantId), e); } + + return true; } @Override - public void deleteGroup(String groupName, int tenantId) throws UserStoreException { + public boolean deleteGroup(String groupName, int tenantId) throws UserStoreException { try { GroupDAO groupDAO = new GroupDAO(); @@ -664,5 +668,6 @@ public void deleteGroup(String groupName, int tenantId) throws UserStoreExceptio throw new UserStoreException(String.format("Error occurred while deleting the " + "group: %s in tenant: %s", groupName, tenantId), e); } + return true; } }