diff --git a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/APIResourceManagementConstants.java b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/APIResourceManagementConstants.java index 1cae1164c087..e18918aa580f 100644 --- a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/APIResourceManagementConstants.java +++ b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/APIResourceManagementConstants.java @@ -142,7 +142,8 @@ public enum ErrorMessages { "properties.", "Error while adding API resource properties to the database."), ERROR_CODE_ERROR_WHILE_UPDATING_SCOPE_METADATA("65015", "Error while updating scope metadata.", "Error while updating scope metadata in the database."), - ; + ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION("65016", "Error while resolving tenant", + "Error while resolving tenant domain for organization."),; private final String code; private final String message; diff --git a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/SQLConstants.java b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/SQLConstants.java index 19458f21a30f..df3d7f56211a 100644 --- a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/SQLConstants.java +++ b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/constant/SQLConstants.java @@ -65,10 +65,18 @@ public class SQLConstants { "DESCRIPTION, TENANT_ID, TYPE, REQUIRES_AUTHORIZATION FROM API_RESOURCE WHERE "; public static final String GET_API_RESOURCES_TAIL = " (TENANT_ID = %d OR TENANT_ID IS NULL) ORDER BY CURSOR_KEY %s LIMIT %d"; + public static final String GET_API_RESOURCES_TAIL_FOR_ORGANIZATIONS = + " (TENANT_ID = %d OR TENANT_ID IS NULL) AND TYPE != 'TENANT' AND TYPE != 'SYSTEM' " + + "ORDER BY CURSOR_KEY %s LIMIT %d"; public static final String GET_API_RESOURCES_TAIL_MSSQL = " (TENANT_ID = %d OR TENANT_ID IS NULL) ORDER BY CURSOR_KEY %s"; + public static final String GET_API_RESOURCES_TAIL_FOR_ORGANIZATIONS_MSSQL = + " (TENANT_ID = %d OR TENANT_ID IS NULL) AND TYPE != 'TENANT' AND TYPE != 'SYSTEM' ORDER BY CURSOR_KEY %s"; public static final String GET_API_RESOURCES_TAIL_ORACLE = " (TENANT_ID = %d OR TENANT_ID IS NULL) ORDER BY CURSOR_KEY %s FETCH FIRST %d ROWS ONLY"; + public static final String GET_API_RESOURCES_TAIL_FOR_ORGANIZATIONS_ORACLE = + " (TENANT_ID = %d OR TENANT_ID IS NULL) AND TYPE != 'TENANT' AND TYPE != 'SYSTEM' " + + "ORDER BY CURSOR_KEY %s FETCH FIRST %d ROWS ONLY"; public static final String GET_API_RESOURCES_WITH_PROPERTIES_SELECTION = "SELECT" + " AR.ID AS API_RESOURCE_ID," + " AR.CURSOR_KEY AS CURSOR_KEY," + @@ -99,6 +107,8 @@ public class SQLConstants { " LEFT JOIN API_RESOURCE_PROPERTY ARP ON AR.ID = ARP.API_ID ORDER BY CURSOR_KEY %s"; public static final String GET_API_RESOURCES_COUNT = "SELECT COUNT(DISTINCT(ID)) FROM API_RESOURCE WHERE "; public static final String GET_API_RESOURCES_COUNT_TAIL = " (TENANT_ID = ? OR TENANT_ID IS NULL)"; + public static final String GET_API_RESOURCES_COUNT_FOR_ORGANIZATIONS_TAIL = + " (TENANT_ID = ? OR TENANT_ID IS NULL) AND TYPE != 'TENANT' AND TYPE != 'SYSTEM'"; public static final String GET_API_RESOURCE_BY_ID = "SELECT" + " AR.ID AS API_RESOURCE_ID," + " AR.NAME AS API_RESOURCE_NAME," + @@ -113,6 +123,20 @@ public class SQLConstants { " S.DESCRIPTION AS SCOPE_DESCRIPTION" + " FROM API_RESOURCE AR LEFT JOIN SCOPE S ON AR.ID = S.API_ID WHERE AR.ID = ? AND (AR.TENANT_ID = ?" + " OR AR.TENANT_ID IS NULL)"; + public static final String GET_API_RESOURCE_BY_ID_FOR_ORGANIZATIONS = "SELECT" + + " AR.ID AS API_RESOURCE_ID," + + " AR.NAME AS API_RESOURCE_NAME," + + " AR.IDENTIFIER AS API_RESOURCE_IDENTIFIER," + + " AR.DESCRIPTION AS API_RESOURCE_DESCRIPTION," + + " AR.TENANT_ID AS API_RESOURCE_TENANT_ID," + + " AR.TYPE AS API_RESOURCE_TYPE," + + " AR.REQUIRES_AUTHORIZATION AS REQUIRES_AUTHORIZATION," + + " S.ID AS SCOPE_ID," + + " S.NAME AS SCOPE_QUALIFIED_NAME," + + " S.DISPLAY_NAME AS SCOPE_DISPLAY_NAME," + + " S.DESCRIPTION AS SCOPE_DESCRIPTION" + + " FROM API_RESOURCE AR LEFT JOIN SCOPE S ON AR.ID = S.API_ID WHERE AR.ID = ? AND (AR.TENANT_ID = ?" + + " OR AR.TENANT_ID IS NULL) AND AR.TYPE != 'TENANT' AND AR.TYPE != 'SYSTEM'"; public static final String GET_SCOPES_BY_API_ID = "SELECT ID, NAME, DISPLAY_NAME, DESCRIPTION, API_ID, TENANT_ID " + "FROM SCOPE WHERE API_ID = ? AND (TENANT_ID = ? OR TENANT_ID IS NULL)"; public static final String GET_API_RESOURCE_BY_IDENTIFIER = "SELECT" + @@ -151,6 +175,10 @@ public class SQLConstants { public static final String GET_SCOPES_BY_TENANT_ID = "SELECT ID, NAME, DISPLAY_NAME, DESCRIPTION, API_ID, " + "TENANT_ID FROM SCOPE WHERE "; public static final String GET_SCOPES_BY_TENANT_ID_TAIL = " (TENANT_ID = ? OR TENANT_ID IS NULL)"; + public static final String GET_SCOPES_BY_TENANT_ID_FOR_ORGANIZATIONS = + "SELECT SC.ID, SC.NAME, SC.DISPLAY_NAME, SC.DESCRIPTION, SC.API_ID, SC.TENANT_ID FROM SCOPE SC" + + " JOIN API_RESOURCE AR ON AR.ID = SC.API_ID" + + " WHERE (AR.TENANT_ID = ? OR AR.TENANT_ID IS NULL) AND TYPE != 'TENANT' AND TYPE != 'SYSTEM'"; public static final String DELETE_SCOPE_BY_NAME = "DELETE FROM SCOPE WHERE NAME = ? AND TENANT_ID = ?"; public static final String GET_API_RESOURCE_PROPERTIES_BY_API_ID = "SELECT ID, NAME, VALUE FROM " + "API_RESOURCE_PROPERTY WHERE API_ID = ?"; diff --git a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/dao/impl/APIResourceManagementDAOImpl.java b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/dao/impl/APIResourceManagementDAOImpl.java index 30756faaf818..1e60791d23c7 100644 --- a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/dao/impl/APIResourceManagementDAOImpl.java +++ b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/main/java/org/wso2/carbon/identity/api/resource/mgt/dao/impl/APIResourceManagementDAOImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.identity.api.resource.mgt.constant.APIResourceManagementConstants; import org.wso2.carbon.identity.api.resource.mgt.constant.SQLConstants; import org.wso2.carbon.identity.api.resource.mgt.dao.APIResourceManagementDAO; +import org.wso2.carbon.identity.api.resource.mgt.internal.APIResourceManagementServiceComponentHolder; import org.wso2.carbon.identity.api.resource.mgt.model.FilterQueryBuilder; import org.wso2.carbon.identity.api.resource.mgt.util.APIResourceManagementUtil; import org.wso2.carbon.identity.application.common.model.APIResource; @@ -35,6 +36,9 @@ import org.wso2.carbon.identity.core.model.ExpressionNode; import org.wso2.carbon.identity.core.util.IdentityCoreConstants; import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; +import org.wso2.carbon.identity.core.util.IdentityTenantUtil; +import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException; +import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; import java.sql.Connection; import java.sql.PreparedStatement; @@ -94,6 +98,19 @@ public Integer getAPIResourcesCount(Integer tenantId, List expre Map filterAttributeValue = filterQueryBuilder.getFilterAttributeValue(); String getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_TAIL; + try { + if (OrganizationManagementUtil.isOrganization(tenantId)) { + tenantId = getRootOrganizationTenantId(tenantId); + getAPIResourcesCountSqlStmtTail = SQLConstants.GET_API_RESOURCES_COUNT_FOR_ORGANIZATIONS_TAIL; + } + } catch (OrganizationManagementException e) { + throw new APIResourceMgtException( + APIResourceManagementConstants.ErrorMessages + .ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION.getCode(), + APIResourceManagementConstants.ErrorMessages + .ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION.getMessage(), e); + } + String sqlStmt = SQLConstants.GET_API_RESOURCES_COUNT + filterQueryBuilder.getFilterQuery() + getAPIResourcesCountSqlStmtTail; @@ -228,8 +245,21 @@ public boolean isAPIResourceExistById(String apiId, Integer tenantId) throws API @Override public APIResource getAPIResourceById(String apiId, Integer tenantId) throws APIResourceMgtException { + String query = SQLConstants.GET_API_RESOURCE_BY_ID; + try { + if (OrganizationManagementUtil.isOrganization(tenantId)) { + tenantId = getRootOrganizationTenantId(tenantId); + query = SQLConstants.GET_API_RESOURCE_BY_ID_FOR_ORGANIZATIONS; + } + } catch (OrganizationManagementException e) { + throw new APIResourceMgtException( + APIResourceManagementConstants.ErrorMessages + .ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION.getCode(), + APIResourceManagementConstants.ErrorMessages + .ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION.getMessage(), e); + } try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false); - PreparedStatement preparedStatement = dbConnection.prepareStatement(SQLConstants.GET_API_RESOURCE_BY_ID)) { + PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) { preparedStatement.setString(1, apiId); preparedStatement.setInt(2, tenantId); ResultSet resultSet = preparedStatement.executeQuery(); @@ -445,6 +475,18 @@ public List getScopesByTenantId(Integer tenantId, List ex appendFilterQuery(expressionNodes, filterQueryBuilder, true); String query = SQLConstants.GET_SCOPES_BY_TENANT_ID + filterQueryBuilder.getFilterQuery() + SQLConstants.GET_SCOPES_BY_TENANT_ID_TAIL; + try { + if (OrganizationManagementUtil.isOrganization(tenantId) && !isInternalFiltering(filterQueryBuilder)) { + tenantId = getRootOrganizationTenantId(tenantId); + query = SQLConstants.GET_SCOPES_BY_TENANT_ID_FOR_ORGANIZATIONS; + } + } catch (OrganizationManagementException e) { + throw new APIResourceMgtException( + APIResourceManagementConstants.ErrorMessages + .ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION.getCode(), + APIResourceManagementConstants.ErrorMessages + .ERROR_CODE_ERROR_WHILE_RESOLVING_TENANT_FOR_ORGANIZATION.getMessage(), e); + } PreparedStatement preparedStatement = dbConnection.prepareStatement(query); preparedStatement.setInt(1, tenantId); int filterAttrSize = 0; @@ -778,15 +820,40 @@ private static APIResource getApiResource(ResultSet resultSet, List expressionNodes, int expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())) + .thenReturn(false); Assert.assertEquals(daoImpl.getAPIResourcesCount(tenantId, expressionNodes).intValue(), expected); } } @@ -106,9 +112,13 @@ public Object[][] getAPIResourcesData() { public void testGetAPIResources(Integer limit, Integer tenantId, String sortOrder, List expressionNodes, int count) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())) + .thenReturn(false); Assert.assertEquals(daoImpl.getAPIResources(limit, tenantId, sortOrder, expressionNodes).size(), count); } } @@ -124,9 +134,13 @@ public Object[][] addAPIResourceData() { @Test(dataProvider = "addAPIResourceData", priority = 2) public void testAddAPIResource(String postfix, int tenantId) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())) + .thenReturn(false); APIResource apiResource = createAPIResource(postfix); APIResource createdAPIResource = daoImpl.addAPIResource(apiResource, tenantId); Assert.assertNotNull(createdAPIResource); @@ -147,8 +161,11 @@ public Object[][] getScopesByAPIData() { @Test(dataProvider = "getScopesByAPIData", priority = 3) public void testGetScopesByAPI(String name, Integer tenantId, int expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - String apiId = addAPIResourceToDB(name, getConnection(), tenantId, identityDatabaseUtil).getId(); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + String apiId = addAPIResourceToDB(name, getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil).getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); Assert.assertEquals(daoImpl.getScopesByAPI(apiId, TENANT_ID).size(), expected); @@ -166,8 +183,11 @@ public Object[][] isAPIResourceExistData() { @Test(dataProvider = "isAPIResourceExistData", priority = 4) public void testIsAPIResourceExist(String identifierPostFix, Integer tenantId, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - addAPIResourceToDB(identifierPostFix, getConnection(), tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + addAPIResourceToDB(identifierPostFix, getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); Assert.assertEquals(daoImpl.isAPIResourceExist(APIRESOURCE_IDENTIFIER + identifierPostFix, TENANT_ID), @@ -186,10 +206,12 @@ public Object[][] isAPIResourceExistByIdData() { @Test(dataProvider = "isAPIResourceExistByIdData", priority = 5) public void testIsAPIResourceExistById(Integer tenantId, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource createdAPIResource = - addAPIResourceToDB("testIsAPIResourceExistById", getConnection(), tenantId, identityDatabaseUtil); + addAPIResourceToDB("testIsAPIResourceExistById", getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil); String apiId = createdAPIResource.getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); @@ -208,9 +230,12 @@ public Object[][] getAPIResourceByIdData() { @Test(dataProvider = "getAPIResourceByIdData", priority = 6) public void testGetAPIResourceById(Integer tenantId, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource createdAPIResource = - addAPIResourceToDB("testGetAPIResourceById", getConnection(), tenantId, identityDatabaseUtil); + addAPIResourceToDB("testGetAPIResourceById", getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil); String apiId = createdAPIResource.getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); @@ -229,9 +254,11 @@ public Object[][] isScopeExistByIdData() { @Test(dataProvider = "isScopeExistByIdData", priority = 7) public void testIsScopeExistById(Integer tenantId, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource createdAPIResource = addAPIResourceToDB("testIsScopeExistById", getConnection(), - tenantId, identityDatabaseUtil); + tenantId, identityDatabaseUtil, organizationManagementUtil); String scopeId = createdAPIResource.getScopes().get(0).getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); @@ -250,9 +277,11 @@ public Object[][] deleteAPIResourceByIdData() { @Test(dataProvider = "deleteAPIResourceByIdData", priority = 8) public void testDeleteAPIResourceById(Integer tenantId, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { String apiId = addAPIResourceToDB("testDeleteAPIResourceById", getConnection(), tenantId, - identityDatabaseUtil).getId(); + identityDatabaseUtil, organizationManagementUtil).getId(); Connection connection = getConnection(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); @@ -279,9 +308,10 @@ public Object[][] isScopeExistByNameData() { @Test(dataProvider = "isScopeExistByNameData", priority = 9) public void testIsScopeExistByName(Integer tenantId, String scopeName, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - - addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil, organizationManagementUtil); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); @@ -301,9 +331,10 @@ public Object[][] getScopeByNameAndTenantIdData() { public void testGetScopeByNameAndTenantId(Integer tenantId, String scopeName, String expectedName) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - - addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil, organizationManagementUtil); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); Scope scope = daoImpl.getScopeByNameAndTenantId(TEST_SCOPE_1 + scopeName, tenantId); @@ -315,10 +346,13 @@ public void testGetScopeByNameAndTenantId(Integer tenantId, String scopeName, St public void testAddScopes() throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource apiResource = - addAPIResourceToDB("testAddScopes", getConnection(), TENANT_ID, identityDatabaseUtil); + addAPIResourceToDB("testAddScopes", getConnection(), TENANT_ID, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiResource.getId(); List scopes = Arrays.asList(createScope("scope1"), createScope("scope2")); @@ -342,10 +376,13 @@ public void testAddScopes() throws Exception { @Test(priority = 12) public void testDeleteAllScopes() throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource apiResource = - addAPIResourceToDB("testDeleteAllScopes", getConnection(), TENANT_ID, identityDatabaseUtil); + addAPIResourceToDB("testDeleteAllScopes", getConnection(), TENANT_ID, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiResource.getId(); Connection connection = getConnection(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); @@ -367,10 +404,13 @@ public void testDeleteAllScopes() throws Exception { @Test(priority = 13) public void testDeleteScope() throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource apiResource = - addAPIResourceToDB("testDeleteScope", getConnection(), TENANT_ID, identityDatabaseUtil); + addAPIResourceToDB("testDeleteScope", getConnection(), TENANT_ID, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiResource.getId(); String scopeName = apiResource.getScopes().get(0).getName(); // Assuming there's at least one scope @@ -442,8 +482,11 @@ private static APIResource createAPIResource(String postFix) { */ private APIResource addAPIResourceToDB(String namePostFix, Connection connection, int tenantId) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - return addAPIResourceToDB(namePostFix, connection, tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + return addAPIResourceToDB(namePostFix, connection, tenantId, identityDatabaseUtil, + organizationManagementUtil); } } @@ -458,7 +501,9 @@ private APIResource addAPIResourceToDB(String namePostFix, Connection connection * @throws Exception Error when adding API resource. */ private APIResource addAPIResourceToDB(String namePostFix, Connection connection, int tenantId, - MockedStatic identityDatabaseUtil) throws Exception { + MockedStatic identityDatabaseUtil, + MockedStatic organizationManagementUtil) + throws Exception { APIResource apiResource = createAPIResource(namePostFix); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); @@ -467,6 +512,7 @@ private APIResource addAPIResourceToDB(String namePostFix, Connection connection connection.commit(); return null; }); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())).thenReturn(false); return daoImpl.addAPIResource(apiResource, tenantId); } diff --git a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/test/java/org/wso2/carbon/identity/api/resource/mgt/dao/CacheBackedAPIResourceManagementDAOTest.java b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/test/java/org/wso2/carbon/identity/api/resource/mgt/dao/CacheBackedAPIResourceManagementDAOTest.java index a6ea03d36fb8..e24909888807 100644 --- a/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/test/java/org/wso2/carbon/identity/api/resource/mgt/dao/CacheBackedAPIResourceManagementDAOTest.java +++ b/components/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt/src/test/java/org/wso2/carbon/identity/api/resource/mgt/dao/CacheBackedAPIResourceManagementDAOTest.java @@ -35,6 +35,7 @@ import org.wso2.carbon.identity.core.model.ExpressionNode; import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; +import org.wso2.carbon.identity.organization.management.service.util.OrganizationManagementUtil; import java.nio.file.Paths; import java.sql.Connection; @@ -47,6 +48,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.mockStatic; public class CacheBackedAPIResourceManagementDAOTest { @@ -101,9 +103,13 @@ public Object[][] getAPIResourcesCountData() { public void testGetAPIResourcesCount(Integer tenantId, List expressionNodes, int expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())) + .thenReturn(false); Assert.assertEquals(daoImpl.getAPIResourcesCount(tenantId, expressionNodes).intValue(), expected); } } @@ -121,9 +127,13 @@ public Object[][] getAPIResourcesData() { public void testGetAPIResources(Integer limit, Integer tenantId, String sortOrder, List expressionNodes, int count) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())) + .thenReturn(false); Assert.assertEquals(daoImpl.getAPIResources(limit, tenantId, sortOrder, expressionNodes).size(), count); } } @@ -139,9 +149,13 @@ public Object[][] addAPIResourceData() { @Test(dataProvider = "addAPIResourceData", priority = 2) public void testAddAPIResource(String postfix, int tenantId) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())) + .thenReturn(false); APIResource apiResource = createAPIResource(postfix); APIResource createdAPIResource = daoImpl.addAPIResource(apiResource, tenantId); Assert.assertNotNull(createdAPIResource); @@ -163,8 +177,11 @@ public Object[][] getScopesByAPIData() { public void testGetScopesByAPI(String name, Integer tenantId, int expected) throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { - String apiId = addAPIResourceToDB(name, getConnection(), tenantId, identityDatabaseUtil).getId(); + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + String apiId = addAPIResourceToDB(name, getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil).getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(true)).thenReturn(getConnection()); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(false)).thenReturn(getConnection()); identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(TENANT_ID)) @@ -185,8 +202,11 @@ public Object[][] isAPIResourceExistData() { public void testIsAPIResourceExist(String identifierPostFix, Integer tenantId, boolean expected) throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { - addAPIResourceToDB(identifierPostFix, getConnection(), tenantId, identityDatabaseUtil); + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + addAPIResourceToDB(identifierPostFix, getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(true)).thenReturn(getConnection()); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(false)).thenReturn(getConnection()); identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(TENANT_ID)) @@ -208,9 +228,12 @@ public Object[][] isAPIResourceExistByIdData() { public void testIsAPIResourceExistById(Integer tenantId, boolean expected) throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { APIResource apiresource = - addAPIResourceToDB("testIsAPIResourceExistById", getConnection(), tenantId, identityDatabaseUtil); + addAPIResourceToDB("testIsAPIResourceExistById", getConnection(), tenantId, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiresource.getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(true)).thenReturn(getConnection()); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(false)).thenReturn(getConnection()); @@ -232,13 +255,15 @@ public Object[][] getAPIResourceByIdData() { public void testGetAPIResourceById(Integer tenantId, boolean expected) throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(tenantId)) .thenReturn(getTenantDomain(tenantId)); String apiId = addAPIResourceToDB("testGetAPIResourceById", getConnection(), tenantId, - identityDatabaseUtil).getId(); + identityDatabaseUtil, organizationManagementUtil).getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); @@ -260,9 +285,11 @@ public Object[][] isScopeExistByIdData() { @Test(dataProvider = "isScopeExistByIdData", priority = 7) public void testIsScopeExistById(Integer tenantId, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { String scopeId = addAPIResourceToDB("testIsScopeExistById", getConnection(), tenantId, - identityDatabaseUtil).getScopes().get(0).getId(); + identityDatabaseUtil, organizationManagementUtil).getScopes().get(0).getId(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); Assert.assertEquals(daoImpl.isScopeExistById(scopeId, TENANT_ID), expected); @@ -281,13 +308,15 @@ public Object[][] deleteAPIResourceByIdData() { public void testDeleteAPIResourceById(Integer tenantId, boolean expected) throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(tenantId)) .thenReturn(getTenantDomain(tenantId)); String apiId = addAPIResourceToDB("testDeleteAPIResourceById", getConnection(), tenantId, - identityDatabaseUtil).getId(); + identityDatabaseUtil, organizationManagementUtil).getId(); Connection connection = getConnection(); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(true)).thenReturn(connection); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(false)).thenReturn(getConnection()); @@ -318,8 +347,10 @@ public Object[][] isScopeExistByNameData() { @Test(dataProvider = "isScopeExistByNameData", priority = 9) public void testIsScopeExistByName(Integer tenantId, String scopeName, boolean expected) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil, organizationManagementUtil); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); @@ -339,8 +370,10 @@ public Object[][] getScopeByNameAndTenantIdData() { public void testGetScopeByNameAndTenantId(Integer tenantId, String scopeName, String expectedName) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + addAPIResourceToDB(scopeName, getConnection(), tenantId, identityDatabaseUtil, organizationManagementUtil); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())) .thenReturn(getConnection()); Scope scope = daoImpl.getScopeByNameAndTenantId(TEST_SCOPE_1 + scopeName, tenantId); @@ -352,13 +385,16 @@ public void testGetScopeByNameAndTenantId(Integer tenantId, String scopeName, St public void testAddScopes() throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(TENANT_ID)) .thenReturn(getTenantDomain(TENANT_ID)); APIResource apiResource = - addAPIResourceToDB("testAddScopes", getConnection(), TENANT_ID, identityDatabaseUtil); + addAPIResourceToDB("testAddScopes", getConnection(), TENANT_ID, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiResource.getId(); List scopes = Arrays.asList(createScope("scope1"), createScope("scope2")); @@ -388,13 +424,16 @@ public void testAddScopes() throws Exception { public void testDeleteAllScopes() throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(TENANT_ID)) .thenReturn(getTenantDomain(TENANT_ID)); APIResource apiResource = - addAPIResourceToDB("testDeleteAllScopes", getConnection(), TENANT_ID, identityDatabaseUtil); + addAPIResourceToDB("testDeleteAllScopes", getConnection(), TENANT_ID, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiResource.getId(); Connection connection = getConnection(); @@ -422,13 +461,16 @@ public void testDeleteAllScopes() throws Exception { public void testDeleteScope() throws Exception { try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); - MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class)) { + MockedStatic identityTenantUtil = mockStatic(IdentityTenantUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { identityTenantUtil.when(() -> IdentityTenantUtil.getTenantDomain(TENANT_ID)) .thenReturn(getTenantDomain(TENANT_ID)); APIResource apiResource = - addAPIResourceToDB("testDeleteScope", getConnection(), TENANT_ID, identityDatabaseUtil); + addAPIResourceToDB("testDeleteScope", getConnection(), TENANT_ID, identityDatabaseUtil, + organizationManagementUtil); String apiId = apiResource.getId(); String scopeName = apiResource.getScopes().get(0).getName(); @@ -503,8 +545,11 @@ private static APIResource createAPIResource(String postFix) { */ private APIResource addAPIResourceToDB(String namePostFix, Connection connection, int tenantId) throws Exception { - try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class)) { - return addAPIResourceToDB(namePostFix, connection, tenantId, identityDatabaseUtil); + try (MockedStatic identityDatabaseUtil = mockStatic(IdentityDatabaseUtil.class); + MockedStatic organizationManagementUtil = + mockStatic(OrganizationManagementUtil.class)) { + return addAPIResourceToDB(namePostFix, connection, tenantId, identityDatabaseUtil, + organizationManagementUtil); } } @@ -519,7 +564,9 @@ private APIResource addAPIResourceToDB(String namePostFix, Connection connection * @throws Exception Error when adding API resource. */ private APIResource addAPIResourceToDB(String namePostFix, Connection connection, int tenantId, - MockedStatic identityDatabaseUtil) throws Exception { + MockedStatic identityDatabaseUtil, + MockedStatic organizationManagementUtil) + throws Exception { APIResource apiResource = createAPIResource(namePostFix); identityDatabaseUtil.when(() -> IdentityDatabaseUtil.getDBConnection(anyBoolean())).thenReturn(connection); @@ -529,6 +576,7 @@ private APIResource addAPIResourceToDB(String namePostFix, Connection connection connection.commit(); return null; }); + organizationManagementUtil.when(() -> OrganizationManagementUtil.isOrganization(anyInt())).thenReturn(false); return daoImpl.addAPIResource(apiResource, tenantId); } diff --git a/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 b/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 index 111e3b6e52a1..1953a56401e5 100644 --- a/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 +++ b/features/api-resource-mgt/org.wso2.carbon.identity.api.resource.mgt.server.feature/resources/system-api-resource.xml.j2 @@ -49,6 +49,14 @@ description="Delete API resources"/> + + + + + diff --git a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/resource-access-control-v2.xml.j2 b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/resource-access-control-v2.xml.j2 index f867bfc26056..cc70f54d5e61 100644 --- a/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/resource-access-control-v2.xml.j2 +++ b/features/identity-core/org.wso2.carbon.identity.core.server.feature/resources/resource-access-control-v2.xml.j2 @@ -1045,6 +1045,11 @@ + + + internal_org_api_resource_view + + internal_api_resource_create