Search in sources :

Example 76 with Group

use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.

the class RegistrySearchUtilTestCase method testAnonymousUserQueryInDevPortal.

@Test
public void testAnonymousUserQueryInDevPortal() throws APIPersistenceException {
    // Normal dev portal api listing
    String inputQuery = "";
    UserContext ctx = new UserContext("wso2.anonymous.user", organization, null, anonymousRoles);
    String searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    String expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&name=*&enableStore=(true OR null)" + "&group=true&group.field=name&group.ngroups=true&group.sort=versionTimestamp desc&lcState=(PUBLISHED OR PROTOTYPED)";
    Assert.assertEquals("Generated query mismatched. ", expected, searchQuery);
    // search for 'test' in description
    inputQuery = "description:test";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&" + "description=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for description search. ", expected, searchQuery);
    // search for provider 'pubuser'
    inputQuery = "provider:pubuser";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)&" + "provider=*pubuser*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for provider search. ", expected, searchQuery);
    // search for propertyname 'test'
    inputQuery = "property_name:test";
    expected = "store_view_roles=(null OR system\\/wso2.anonymous.role)" + "&api_meta.property_name__display=*test*&lcState=(PUBLISHED OR PROTOTYPED)";
    searchQuery = RegistrySearchUtil.getDevPortalSearchQuery(inputQuery, ctx, false, false);
    Assert.assertEquals("Generated query mismatched for property search. ", expected, searchQuery);
}
Also used : UserContext(org.wso2.carbon.apimgt.persistence.dto.UserContext) Test(org.junit.Test)

Example 77 with Group

use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.

the class ApiMgtDAO method getApplicationByName.

/**
 * Fetches an Application by name.
 *
 * @param applicationName Name of the Application
 * @param userId          Name of the User.
 * @param groupId         Group ID
 * @throws APIManagementException
 */
public Application getApplicationByName(String applicationName, String userId, String groupId) throws APIManagementException {
    // mysql> select APP.APPLICATION_ID, APP.NAME, APP.SUBSCRIBER_ID,APP.APPLICATION_TIER,APP.CALLBACK_URL,APP
    // .DESCRIPTION,
    // APP.APPLICATION_STATUS from AM_SUBSCRIBER as SUB,AM_APPLICATION as APP
    // where SUB.user_id='admin' AND APP.name='DefaultApplication' AND SUB.SUBSCRIBER_ID=APP.SUBSCRIBER_ID;
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    int applicationId = 0;
    Application application = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        String query = SQLConstants.GET_APPLICATION_BY_NAME_PREFIX;
        String whereClause = "  WHERE SUB.USER_ID =? AND APP.NAME=? AND SUB.SUBSCRIBER_ID=APP.SUBSCRIBER_ID";
        String whereClauseCaseInSensitive = "  WHERE LOWER(SUB.USER_ID) =LOWER(?) AND APP.NAME=? AND SUB" + "" + ".SUBSCRIBER_ID=APP.SUBSCRIBER_ID";
        String whereClauseWithGroupId = "  WHERE  (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND SUB.USER_ID = ?)) AND " + "APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        String whereClauseWithGroupIdCaseInSensitive = "  WHERE  (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND LOWER(SUB.USER_ID) = LOWER(?))) AND " + "APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        String whereClauseWithMultiGroupId = "  WHERE  ((APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  OR   SUB.USER_ID = ? " + "OR (APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM AM_APPLICATION WHERE GROUP_ID = ?))) " + "AND APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        String whereClauseWithMultiGroupIdCaseInSensitive = "  WHERE  ((APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  " + "OR   LOWER(SUB.USER_ID) = LOWER(?)  " + "OR (APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM AM_APPLICATION WHERE GROUP_ID = " + "?))) " + "AND APP.NAME = ? AND SUB.SUBSCRIBER_ID = APP.SUBSCRIBER_ID";
        if (groupId != null && !"null".equals(groupId) && !groupId.isEmpty()) {
            if (multiGroupAppSharingEnabled) {
                Subscriber subscriber = getSubscriber(userId);
                String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
                if (forceCaseInsensitiveComparisons) {
                    query = query + whereClauseWithMultiGroupIdCaseInSensitive;
                } else {
                    query = query + whereClauseWithMultiGroupId;
                }
                String[] groupIds = groupId.split(",");
                int parameterIndex = groupIds.length;
                prepStmt = fillQueryParams(connection, query, groupIds, 1);
                prepStmt.setString(++parameterIndex, tenantDomain);
                prepStmt.setString(++parameterIndex, userId);
                prepStmt.setString(++parameterIndex, tenantDomain + '/' + groupId);
                prepStmt.setString(++parameterIndex, applicationName);
            } else {
                if (forceCaseInsensitiveComparisons) {
                    query = query + whereClauseWithGroupIdCaseInSensitive;
                } else {
                    query = query + whereClauseWithGroupId;
                }
                prepStmt = connection.prepareStatement(query);
                prepStmt.setString(1, groupId);
                prepStmt.setString(2, userId);
                prepStmt.setString(3, applicationName);
            }
        } else {
            if (forceCaseInsensitiveComparisons) {
                query = query + whereClauseCaseInSensitive;
            } else {
                query = query + whereClause;
            }
            prepStmt = connection.prepareStatement(query);
            prepStmt.setString(1, userId);
            prepStmt.setString(2, applicationName);
        }
        rs = prepStmt.executeQuery();
        while (rs.next()) {
            String subscriberId = rs.getString("SUBSCRIBER_ID");
            String subscriberName = rs.getString("USER_ID");
            Subscriber subscriber = new Subscriber(subscriberName);
            subscriber.setId(Integer.parseInt(subscriberId));
            application = new Application(applicationName, subscriber);
            application.setOwner(rs.getString("CREATED_BY"));
            application.setDescription(rs.getString("DESCRIPTION"));
            application.setStatus(rs.getString("APPLICATION_STATUS"));
            application.setCallbackUrl(rs.getString("CALLBACK_URL"));
            applicationId = rs.getInt("APPLICATION_ID");
            application.setId(applicationId);
            application.setTier(rs.getString("APPLICATION_TIER"));
            application.setUUID(rs.getString("UUID"));
            application.setGroupId(rs.getString("GROUP_ID"));
            application.setOwner(rs.getString("CREATED_BY"));
            application.setTokenType(rs.getString("TOKEN_TYPE"));
            if (multiGroupAppSharingEnabled) {
                setGroupIdInApplication(connection, application);
            }
            if (application != null) {
                Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
                application.setApplicationAttributes(applicationAttributes);
            }
        }
    } catch (SQLException e) {
        handleException("Error while obtaining details of the Application : " + applicationName, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return application;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 78 with Group

use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.

the class ApiMgtDAO method isApplicationExist.

/**
 * Check whether given application name is available under current subscriber or group
 *
 * @param appName  application name
 * @param username subscriber
 * @param groupId  group of the subscriber
 * @param organization identifier of the organization
 * @return true if application is available for the subscriber
 * @throws APIManagementException if failed to get applications for given subscriber
 */
public boolean isApplicationExist(String appName, String username, String groupId, String organization) throws APIManagementException {
    if (username == null) {
        return false;
    }
    Subscriber subscriber = getSubscriber(username);
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    int appId = 0;
    String sqlQuery = SQLConstants.GET_APPLICATION_ID_PREFIX;
    String whereClauseWithGroupId = " AND (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND SUB.USER_ID = ?))";
    String whereClauseWithGroupIdCaseInsensitive = " AND (APP.GROUP_ID = ? " + "OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL) AND LOWER(SUB.USER_ID) = LOWER(?)))";
    String whereClauseWithMultiGroupId = " AND  ( (APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  OR  ( SUB.USER_ID = ? ) " + "OR (APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM AM_APPLICATION WHERE GROUP_ID = ?)))";
    String whereClauseWithMultiGroupIdCaseInsensitive = " AND  ( (APP.APPLICATION_ID IN  (SELECT APPLICATION_ID " + "FROM AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?)) " + "OR (LOWER(SUB.USER_ID) = LOWER(?))" + "OR (APP.APPLICATION_ID IN (SELECT APPLICATION_ID FROM AM_APPLICATION WHERE GROUP_ID = ?)))";
    String whereClause = " AND SUB.USER_ID = ? ";
    String whereClauseCaseInsensitive = " AND LOWER(SUB.USER_ID) = LOWER(?) ";
    try {
        connection = APIMgtDBUtil.getConnection();
        if (!StringUtils.isEmpty(groupId)) {
            if (multiGroupAppSharingEnabled) {
                if (forceCaseInsensitiveComparisons) {
                    sqlQuery += whereClauseWithMultiGroupIdCaseInsensitive;
                } else {
                    sqlQuery += whereClauseWithMultiGroupId;
                }
                String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
                String[] grpIdArray = groupId.split(",");
                int noOfParams = grpIdArray.length;
                preparedStatement = fillQueryParams(connection, sqlQuery, grpIdArray, 3);
                preparedStatement.setString(1, appName);
                preparedStatement.setString(2, organization);
                int paramIndex = noOfParams + 2;
                preparedStatement.setString(++paramIndex, tenantDomain);
                preparedStatement.setString(++paramIndex, subscriber.getName());
                preparedStatement.setString(++paramIndex, tenantDomain + '/' + groupId);
            } else {
                if (forceCaseInsensitiveComparisons) {
                    sqlQuery += whereClauseWithGroupIdCaseInsensitive;
                } else {
                    sqlQuery += whereClauseWithGroupId;
                }
                preparedStatement = connection.prepareStatement(sqlQuery);
                preparedStatement.setString(1, appName);
                preparedStatement.setString(2, organization);
                preparedStatement.setString(3, groupId);
                preparedStatement.setString(4, subscriber.getName());
            }
        } else {
            if (forceCaseInsensitiveComparisons) {
                sqlQuery += whereClauseCaseInsensitive;
            } else {
                sqlQuery += whereClause;
            }
            preparedStatement = connection.prepareStatement(sqlQuery);
            preparedStatement.setString(1, appName);
            preparedStatement.setString(2, organization);
            preparedStatement.setString(3, subscriber.getName());
        }
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            appId = resultSet.getInt("APPLICATION_ID");
        }
        if (appId > 0) {
            return true;
        }
    } catch (SQLException e) {
        handleException("Error while getting the id  of " + appName + " from the persistence store.", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(preparedStatement, connection, resultSet);
    }
    return false;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 79 with Group

use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.

the class ApiMgtDAO method isApplicationGroupCombinationExists.

/**
 * @param applicationName application name
 * @param username username
 * @param groupId group id
 * @return whether a certain application group combination exists or not
 * @throws APIManagementException if failed to assess whether a certain application group combination exists or not
 */
public boolean isApplicationGroupCombinationExists(String applicationName, String username, String groupId) throws APIManagementException {
    if (username == null) {
        return false;
    }
    Subscriber subscriber = getSubscriber(username);
    int appId = 0;
    String sqlQuery = SQLConstants.GET_APPLICATION_ID_PREFIX_FOR_GROUP_COMPARISON;
    String whereClauseWithGroupId = " AND APP.GROUP_ID = ?";
    String whereClauseWithMultiGroupId = " AND (APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))";
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        if (!StringUtils.isEmpty(groupId)) {
            if (multiGroupAppSharingEnabled) {
                sqlQuery += whereClauseWithMultiGroupId;
                String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
                String[] grpIdArray = groupId.split(",");
                int noOfParams = grpIdArray.length;
                try (PreparedStatement preparedStatement = fillQueryParams(connection, sqlQuery, grpIdArray, 2)) {
                    preparedStatement.setString(1, applicationName);
                    int paramIndex = noOfParams + 1;
                    preparedStatement.setString(++paramIndex, tenantDomain);
                    try (ResultSet resultSet = preparedStatement.executeQuery()) {
                        if (resultSet.next()) {
                            appId = resultSet.getInt("APPLICATION_ID");
                        }
                        if (appId > 0) {
                            return true;
                        }
                    }
                }
            } else {
                sqlQuery += whereClauseWithGroupId;
                try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
                    preparedStatement.setString(1, applicationName);
                    preparedStatement.setString(2, groupId);
                    try (ResultSet resultSet = preparedStatement.executeQuery()) {
                        if (resultSet.next()) {
                            appId = resultSet.getInt("APPLICATION_ID");
                        }
                        if (appId > 0) {
                            return true;
                        }
                    }
                }
            }
        }
    } catch (SQLException e) {
        handleException("Error while getting application group combination data for application: " + applicationName, e);
    }
    return false;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 80 with Group

use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.

the class ThrottleConditionEvaluatorTest method testRetrievingEmptyApplicableConditionsWhenDefaultAndConditionGroupsAreNotAvailable.

@Test
public void testRetrievingEmptyApplicableConditionsWhenDefaultAndConditionGroupsAreNotAvailable() {
    ConditionGroupDTO[] conditionGroupDTOS = new ConditionGroupDTO[0];
    List<ConditionGroupDTO> conditionGroupDTOList = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
    // Should return empty Condition group in the Condition group array
    Assert.assertNull(conditionGroupDTOList.get(0));
}
Also used : AuthenticationContext(org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext) ConditionGroupDTO(org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO) Test(org.junit.Test)

Aggregations

Test (org.testng.annotations.Test)155 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)99 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)94 Event (org.wso2.siddhi.core.event.Event)89 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)80 Group (org.wso2.charon3.core.objects.Group)57 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)53 CharonException (org.wso2.charon3.core.exceptions.CharonException)43 HashMap (java.util.HashMap)38 Connection (java.sql.Connection)34 SQLException (java.sql.SQLException)34 ArrayList (java.util.ArrayList)34 IdentitySCIMException (org.wso2.carbon.identity.scim2.common.exceptions.IdentitySCIMException)34 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)33 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)32 PreparedStatement (java.sql.PreparedStatement)29 ResultSet (java.sql.ResultSet)29 Operation (io.swagger.v3.oas.annotations.Operation)27 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)27 Response (javax.ws.rs.core.Response)27