Skip to content

Commit

Permalink
Set the last update date in the post-update role name listener
Browse files Browse the repository at this point in the history
  • Loading branch information
HasiniSama committed Feb 5, 2025
1 parent cf0a556 commit d29840b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2017-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand Down Expand Up @@ -289,16 +289,38 @@ public void addSCIMGroupAttributesToSCIMDisabledHybridRoles(int tenantId,
}
}

/**
* Update SCIM group attributes.
*
* @param tenantId Tenant id.
* @param roleName Group name.
* @param attributes Attributes to be updated.
* @throws IdentitySCIMException If an error occurred while updating the attributes.
*/
public void updateSCIMGroupAttributes(int tenantId, String roleName,
Map<String, String> attributes) throws IdentitySCIMException {

Connection connection = IdentityDatabaseUtil.getDBConnection();
doUpdateSCIMGroupAttributes(tenantId, roleName, attributes, SQLQueries.UPDATE_ATTRIBUTES_SQL);
}

/**
* Do update SCIM group attributes.
*
* @param tenantId Tenant id.
* @param roleName Group name.
* @param attributes Attributes to be updated.
* @param sqlQuery SQL query to update the attributes.
* @throws IdentitySCIMException If an error occurred while updating the attributes.
*/
private void doUpdateSCIMGroupAttributes(int tenantId, String roleName, Map<String, String> attributes,
String sqlQuery) throws IdentitySCIMException {

Connection connection = IdentityDatabaseUtil.getDBConnection(true);
PreparedStatement prepStmt = null;

if (isExistingGroup(SCIMCommonUtils.getGroupNameWithDomain(roleName), tenantId)) {
try {
prepStmt = connection.prepareStatement(SQLQueries.UPDATE_ATTRIBUTES_SQL);

prepStmt = connection.prepareStatement(sqlQuery);
prepStmt.setInt(2, tenantId);
prepStmt.setString(3, roleName);

Expand All @@ -308,19 +330,16 @@ public void updateSCIMGroupAttributes(int tenantId, String roleName,
prepStmt.setString(4, entry.getKey());
prepStmt.setString(1, entry.getValue());
prepStmt.addBatch();

} else {
throw new IdentitySCIMException("Error when adding SCIM Attribute: "
+ entry.getKey()
+ " An attribute with the same name doesn't exists.");
+ entry.getKey() + " An attribute with the same name doesn't exists.");
}
}
int[] return_count = prepStmt.executeBatch();
int[] returnCount = prepStmt.executeBatch();
if (log.isDebugEnabled()) {
log.debug("No. of records updated for updating SCIM Group : " + return_count.length);
log.debug("No. of records updated for updating SCIM Group : " + returnCount.length);
}
connection.commit();

} catch (SQLException e) {
throw new IdentitySCIMException("Error updating the SCIM Group Attributes.", e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2017-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand All @@ -15,7 +15,9 @@
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.scim2.common.DAO;

/**
* SQL Queries for SCIM_IDENTITY_TABLE which persists SCIM_GROUP info.
*/
Expand All @@ -42,7 +44,7 @@ public class SQLQueries {
"SELECT TENANT_ID, ROLE_NAME, ATTR_NAME FROM IDN_SCIM_GROUP WHERE IDN_SCIM_GROUP.TENANT_ID=? AND " +
"IDN_SCIM_GROUP.ROLE_NAME=? AND IDN_SCIM_GROUP.ATTR_NAME=? AND IDN_SCIM_GROUP.AUDIENCE_REF_ID=?";
public static final String UPDATE_ATTRIBUTES_SQL =
"UPDATE IDN_SCIM_GROUP SET UM_ATTR_VALUE=? WHERE TENANT_ID=? AND ROLE_NAME=? AND ATTR_NAME=?";
"UPDATE IDN_SCIM_GROUP SET ATTR_VALUE=? WHERE TENANT_ID=? AND ROLE_NAME=? AND ATTR_NAME=?";
public static final String UPDATE_GROUP_NAME_SQL =
"UPDATE IDN_SCIM_GROUP SET ROLE_NAME=? WHERE TENANT_ID=? AND ROLE_NAME=?";
public static final String DELETE_GROUP_SQL =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2017-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand Down Expand Up @@ -44,7 +44,6 @@
import java.util.Set;
import java.util.UUID;


/**
* This is for managing SCIM specific attributes related to Group resource in Identity_SCIM_GROUP
* Table. This should be managed per tenant.
Expand Down Expand Up @@ -335,4 +334,17 @@ public String[] getGroupListFromAttributeName(String attributeName, String searc
GroupDAO groupDAO = new GroupDAO();
return groupDAO.getGroupNameList(attributeName, searchAttribute, this.tenantId, domainName);
}

/**
* Update SCIM attributes of the group.
*
* @param groupName The display name of the group.
* @param attributes The attributes to be updated.
* @throws IdentitySCIMException IdentitySCIMException when updating the SCIM Group information.
*/
public void updateSCIMAttributes(String groupName, Map<String, String> attributes) throws IdentitySCIMException {

GroupDAO groupDAO = new GroupDAO();
groupDAO.updateSCIMGroupAttributes(tenantId, groupName, attributes);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* Copyright (c) 2017-2025, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 Inc. licenses this file to you under the Apache License,
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
Expand Down Expand Up @@ -51,6 +51,7 @@

import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -698,7 +699,6 @@ public boolean doPostUpdateRoleName(String roleName, String newRoleName, UserSto
}

try {
//TODO:set last update date
SCIMGroupHandler scimGroupHandler = new SCIMGroupHandler(userStoreManager.getTenantId());

String domainName = UserCoreUtil.getDomainName(userStoreManager.getRealmConfiguration());
Expand All @@ -712,6 +712,17 @@ public boolean doPostUpdateRoleName(String roleName, String newRoleName, UserSto
} catch (IdentitySCIMException e) {
throw new UserStoreException("Error updating group information in SCIM Tables.", e);
}

// Update the last modified time of the group.
Date groupLastUpdatedTime = new Date();
Map<String, String> attributes = new HashMap<>();
attributes.put(SCIMConstants.CommonSchemaConstants.LAST_MODIFIED_URI,
AttributeUtil.formatDateTime(groupLastUpdatedTime.toInstant()));
try {
scimGroupHandler.updateSCIMAttributes(newRoleNameWithDomain, attributes);
} catch (IdentitySCIMException e) {
throw new UserStoreException("Failed to update group's last modified date in SCIM tables.", e);
}
return true;
} catch (org.wso2.carbon.user.api.UserStoreException e) {
throw new UserStoreException(e);
Expand Down

0 comments on commit d29840b

Please sign in to comment.