diff --git a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/DAO/GroupDAO.java b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/DAO/GroupDAO.java index 9ba867766..2829b3e6b 100644 --- a/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/DAO/GroupDAO.java +++ b/components/org.wso2.carbon.identity.scim2.common/src/main/java/org/wso2/carbon/identity/scim2/common/DAO/GroupDAO.java @@ -548,4 +548,32 @@ private String removePrimaryDomainName(String roleName) { return roleName; } } + + public void updateGroupName(int tenantId, String oldGroupName, String newGroupName) + throws IdentitySCIMException { + Connection connection = IdentityDatabaseUtil.getDBConnection(); + PreparedStatement prepStmt = null; + + if (isExistingGroup(SCIMCommonUtils.getGroupNameWithDomain(oldGroupName), tenantId)) { + try { + prepStmt = connection.prepareStatement(SQLQueries.UPDATE_GROUP_NAME_SQL); + + prepStmt.setString(1, SCIMCommonUtils.getGroupNameWithDomain(newGroupName)); + prepStmt.setInt(2, tenantId); + prepStmt.setString(3, SCIMCommonUtils.getGroupNameWithDomain(oldGroupName)); + + int count = prepStmt.executeUpdate(); + if (log.isDebugEnabled()) { + log.debug("No. of records updated for updating SCIM Group : " + count); + } + connection.commit(); + } catch (SQLException e) { + throw new IdentitySCIMException("Error updating the SCIM Group Attributes", e); + } finally { + IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt); + } + } else { + throw new IdentitySCIMException("Error when updating group name of the group: " + oldGroupName); + } + } } 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 8148eec97..c5a4f3008 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 @@ -2576,7 +2576,7 @@ public Group createGroup(Group group, Map requiredAttributes) } group.setDisplayName(roleNameWithDomain); //check if the group already exists - if (carbonUM.isExistingRole(group.getDisplayName(), false)) { + if (carbonUM.isExistingGroup(group.getDisplayName())) { String error = "Group with name: " + group.getDisplayName() + " already exists in the system."; throw new ConflictException(error); } @@ -2588,6 +2588,7 @@ public Group createGroup(Group group, Map requiredAttributes) // If members are sent when creating the group, check whether users already exist in the user store. List userIds = group.getMembers(); List userDisplayNames = group.getMembersWithDisplayName(); + org.wso2.carbon.user.core.common.Group coreGroup = null; if (isNotEmpty(userIds)) { List members = new ArrayList<>(); for (Object userId : userIds) { @@ -2625,30 +2626,25 @@ public Group createGroup(Group group, Map requiredAttributes) } } } - // Add other scim attributes in the identity DB since user store doesn't support some attributes. - 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(), 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. - SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(carbonUM.getTenantId()); - scimGroupHandler.createSCIMAttributes(group); - carbonUM.addRoleWithID(group.getDisplayName(), null, null, false); + 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."); } } - } catch (UserStoreException e) { - 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()); + group.getAttributeList().remove(SCIMConstants.CommonSchemaConstants.ID); + if (coreGroup != null) { + group.setId(coreGroup.getGroupID()); } + } catch (UserStoreException e) { handleErrorsOnRoleNamePolicy(e); throw resolveError(e, "Error occurred while adding role : " + group.getDisplayName()); } catch (IdentitySCIMException | BadRequestException e) { @@ -2771,10 +2767,7 @@ 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) { String userStoreDomainFromSP = null; @@ -2798,10 +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."); } @@ -2814,8 +2806,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); } } @@ -3410,7 +3400,7 @@ private void doPatchGroup(String groupId, String currentGroupName, 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 { + 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 boolean deleteGroup(String groupName, int tenantId) throws UserStoreException { + + try { + GroupDAO groupDAO = new GroupDAO(); + if (groupDAO.isExistingGroup(groupName, tenantId)) { + groupDAO.removeSCIMGroup(tenantId, groupName); + } else { + if (log.isDebugEnabled()) { + log.debug("Information for the group: " + groupName + + " doesn't contain in the identity scim table."); + } + } + } catch (IdentitySCIMException e) { + throw new UserStoreException(String.format("Error occurred while deleting the " + + "group: %s in tenant: %s", groupName, tenantId), e); + } + return true; + } + + @Override + public boolean updateGroupName(String oldGroupName, String newGroupName, int tenantID) throws UserStoreException { + + try { + GroupDAO groupDAO = new GroupDAO(); + if (groupDAO.isExistingGroup(oldGroupName, tenantID)) { + groupDAO.updateGroupName(tenantID, oldGroupName, newGroupName); + } else { + log.warn("Non-existent group: " + oldGroupName + " is trying to be updated.."); + } + + } catch (IdentitySCIMException e) { + throw new UserStoreException(String.format("Error occurred while updating the " + + "group: %s in tenant: %s", oldGroupName, tenantID), e); + } + return true; + } }