|
92 | 92 |
|
93 | 93 | import javax.xml.namespace.QName; |
94 | 94 |
|
95 | | -import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants. |
96 | | - ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION_ID; |
| 95 | +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION_ID; |
97 | 96 | import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.APPLICATION; |
98 | 97 | import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.CONSOLE_ORG_SCOPE_PREFIX; |
99 | 98 | import static org.wso2.carbon.identity.role.v2.mgt.core.RoleConstants.CONSOLE_SCOPE_PREFIX; |
@@ -1024,52 +1023,55 @@ public void addMainRoleToSharedRoleRelationship(String mainRoleUUID, String shar |
1024 | 1023 | String mainRoleTenantDomain, String sharedRoleTenantDomain) |
1025 | 1024 | throws IdentityRoleManagementException { |
1026 | 1025 |
|
1027 | | - String mainRoleName = getRoleNameByID(mainRoleUUID, mainRoleTenantDomain); |
1028 | 1026 | int mainRoleTenantId = IdentityTenantUtil.getTenantId(mainRoleTenantDomain); |
1029 | 1027 |
|
1030 | 1028 | String sharedRoleName = getRoleNameByID(sharedRoleUUID, sharedRoleTenantDomain); |
1031 | 1029 | int sharedRoleTenantId = IdentityTenantUtil.getTenantId(sharedRoleTenantDomain); |
1032 | 1030 |
|
1033 | 1031 | int mainRoleUMId = 0; |
1034 | 1032 | int sharedRoleUMId = 0; |
1035 | | - try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) { |
1036 | | - try (NamedPreparedStatement stmt = new NamedPreparedStatement(connection, GET_ROLE_UM_ID_BY_UUID)) { |
1037 | | - stmt.setString(RoleConstants.RoleTableColumns.UM_UUID, mainRoleUUID); |
1038 | | - ResultSet resultSet = stmt.executeQuery(); |
1039 | | - while (resultSet.next()) { |
1040 | | - mainRoleUMId = resultSet.getInt(1); |
1041 | | - } |
1042 | | - } catch (SQLException e) { |
1043 | | - String message = "Error while resolving id of role name: %s in the tenantDomain: %s."; |
1044 | | - throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(), |
1045 | | - String.format(message, mainRoleName, mainRoleTenantDomain), e); |
1046 | | - } |
1047 | | - |
1048 | | - try (NamedPreparedStatement stmt = new NamedPreparedStatement(connection, GET_ROLE_UM_ID_BY_UUID)) { |
1049 | | - stmt.setString(RoleConstants.RoleTableColumns.UM_UUID, sharedRoleUUID); |
1050 | | - ResultSet resultSet = stmt.executeQuery(); |
1051 | | - while (resultSet.next()) { |
1052 | | - sharedRoleUMId = resultSet.getInt(1); |
1053 | | - } |
1054 | | - } catch (SQLException e) { |
1055 | | - String message = "Error while resolving id of role name: %s in the tenantDomain: %s."; |
1056 | | - throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(), |
1057 | | - String.format(message, sharedRoleName, sharedRoleTenantDomain), e); |
1058 | | - } |
| 1033 | + try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(true)) { |
1059 | 1034 |
|
1060 | | - if (mainRoleUMId == 0 || sharedRoleUMId == 0) { |
1061 | | - String message = "Error while resolving role id."; |
1062 | | - throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(), |
1063 | | - message); |
1064 | | - } |
1065 | | - try (NamedPreparedStatement preparedStatement = new NamedPreparedStatement(connection, |
1066 | | - INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP)) { |
1067 | | - preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_ID, sharedRoleUMId); |
1068 | | - preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_ID, mainRoleUMId); |
1069 | | - preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_TENANT_ID, sharedRoleTenantId); |
1070 | | - preparedStatement.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_TENANT_ID, mainRoleTenantId); |
1071 | | - preparedStatement.executeUpdate(); |
| 1035 | + try (NamedPreparedStatement statementForGetMainRoleId = new NamedPreparedStatement(connection, |
| 1036 | + GET_ROLE_UM_ID_BY_UUID); |
| 1037 | + NamedPreparedStatement statementForGetSharedRoleId = new NamedPreparedStatement(connection, |
| 1038 | + GET_ROLE_UM_ID_BY_UUID); |
| 1039 | + NamedPreparedStatement statementForAddRoleRelationship = new NamedPreparedStatement(connection, |
| 1040 | + INSERT_MAIN_TO_SHARED_ROLE_RELATIONSHIP)) { |
| 1041 | + |
| 1042 | + // Retrieve UM_ID for main role. |
| 1043 | + statementForGetMainRoleId.setString(RoleConstants.RoleTableColumns.UM_UUID, mainRoleUUID); |
| 1044 | + ResultSet resultSetForMainRole = statementForGetMainRoleId.executeQuery(); |
| 1045 | + while (resultSetForMainRole.next()) { |
| 1046 | + mainRoleUMId = resultSetForMainRole.getInt(1); |
| 1047 | + } |
| 1048 | + |
| 1049 | + // Retrieve UM_ID for shared role. |
| 1050 | + statementForGetSharedRoleId.setString(RoleConstants.RoleTableColumns.UM_UUID, sharedRoleUUID); |
| 1051 | + ResultSet resultSetForSharedRole = statementForGetSharedRoleId.executeQuery(); |
| 1052 | + while (resultSetForSharedRole.next()) { |
| 1053 | + sharedRoleUMId = resultSetForSharedRole.getInt(1); |
| 1054 | + } |
| 1055 | + |
| 1056 | + if (mainRoleUMId == 0 || sharedRoleUMId == 0) { |
| 1057 | + String message = "Error while resolving role id."; |
| 1058 | + throw new IdentityRoleManagementServerException( |
| 1059 | + RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(), |
| 1060 | + message); |
| 1061 | + } |
| 1062 | + |
| 1063 | + // Add main role to shared role relationship. |
| 1064 | + statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_ID, |
| 1065 | + sharedRoleUMId); |
| 1066 | + statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_ID, mainRoleUMId); |
| 1067 | + statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_SHARED_ROLE_TENANT_ID, |
| 1068 | + sharedRoleTenantId); |
| 1069 | + statementForAddRoleRelationship.setInt(RoleConstants.RoleTableColumns.UM_MAIN_ROLE_TENANT_ID, |
| 1070 | + mainRoleTenantId); |
| 1071 | + statementForAddRoleRelationship.executeUpdate(); |
| 1072 | + IdentityDatabaseUtil.commitUserDBTransaction(connection); |
1072 | 1073 | } catch (SQLException e) { |
| 1074 | + IdentityDatabaseUtil.rollbackUserDBTransaction(connection); |
1073 | 1075 | String message = "Error while adding the role relationship of role: %s."; |
1074 | 1076 | throw new IdentityRoleManagementServerException(RoleConstants.Error.UNEXPECTED_SERVER_ERROR.getCode(), |
1075 | 1077 | String.format(message, sharedRoleName), e); |
|
0 commit comments