Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial commit for group ID supported create group operation #479

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,7 @@ public Group createGroup(Group group, Map<String, Boolean> 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);
}
Expand All @@ -2588,6 +2588,7 @@ public Group createGroup(Group group, Map<String, Boolean> requiredAttributes)
// If members are sent when creating the group, check whether users already exist in the user store.
List<Object> userIds = group.getMembers();
List<String> userDisplayNames = group.getMembersWithDisplayName();
org.wso2.carbon.user.core.common.Group coreGroup = null;
if (isNotEmpty(userIds)) {
List<String> members = new ArrayList<>();
for (Object userId : userIds) {
Expand Down Expand Up @@ -2625,30 +2626,25 @@ public Group createGroup(Group group, Map<String, Boolean> 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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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.");
}
Expand All @@ -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);
}

}
Expand Down Expand Up @@ -3410,7 +3400,7 @@ private void doPatchGroup(String groupId, String currentGroupName, Map<String, L
Update the group with added members and deleted members.
*/
if (isNotEmpty(addedMembers) || isNotEmpty(deletedMembers)) {
carbonUM.updateUserListOfRoleWithID(newGroupName,
carbonUM.updateUserIDListOfGroup(newGroupName,
deletedMemberIdsFromUserstore.toArray(new String[0]),
addedMemberIdsFromUserstore.toArray(new String[0]));
}
Expand Down Expand Up @@ -3508,7 +3498,7 @@ private void setGroupDisplayName(String oldGroupName, String newGroupName)

if (!StringUtils.equals(oldGroupName, newGroupName)) {
// Update group name in carbon UM.
carbonUM.updateRoleName(oldGroupName, newGroupName);
carbonUM.updateGroupDisplayName(oldGroupName, newGroupName);
}
}

Expand Down Expand Up @@ -3593,13 +3583,13 @@ public boolean doUpdateGroup(Group oldGroup, Group newGroup) throws CharonExcept
boolean updated = false;
if (isGroupDisplayNameChanged(oldGroupDisplayName, newGroupDisplayName)) {
// Update group name in carbon UM
carbonUM.updateRoleName(oldGroupDisplayName, newGroupDisplayName);
carbonUM.updateGroupDisplayName(oldGroupDisplayName, newGroupDisplayName);
updated = true;
}

// Update the group with added members and deleted members.
if (isNotEmpty(addedMembers) || isNotEmpty(deletedMembers)) {
carbonUM.updateUserListOfRoleWithID(newGroupDisplayName,
carbonUM.updateUserIDListOfGroup(newGroupDisplayName,
deletedMemberIdsFromUserstore.toArray(new String[0]),
addedMemberIdsFromUserstore.toArray(new String[0]));
updated = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
import org.wso2.carbon.user.core.model.OperationalCondition;
import org.wso2.carbon.user.core.util.UserCoreUtil;
import org.wso2.charon3.core.schema.SCIMConstants;
import org.wso2.charon3.core.utils.AttributeUtil;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -620,4 +624,64 @@ private String createSearchValueForEwOperation(String attributeName, String filt
return delimiter + attributeValue;
}
}

@Override
public boolean addGroup(String displayName, String groupID, LocalDateTime createdDate,
LocalDateTime lastModifiedDate, String location, int tenantId) throws UserStoreException {

Map<String, String> 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;
}
}
Loading