Search in sources :

Example 26 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method deleteApplicationByResourceId.

@Override
public void deleteApplicationByResourceId(String resourceId, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Deleting Application with resourceId: " + resourceId + " in tenantDomain: " + tenantDomain);
    }
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(true)) {
        ServiceProvider application = getApplicationByResourceId(resourceId, tenantDomain);
        if (application != null) {
            // Delete the application certificate if there is any
            deleteApplicationCertificate(connection, application);
            try (NamedPreparedStatement deleteAppStatement = new NamedPreparedStatement(connection, REMOVE_APP_FROM_SP_APP_WITH_UUID)) {
                deleteAppStatement.setString(ApplicationTableColumns.UUID, resourceId);
                int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
                deleteAppStatement.setInt(ApplicationTableColumns.TENANT_ID, tenantId);
                deleteAppStatement.execute();
                IdentityDatabaseUtil.commitTransaction(connection);
            } catch (SQLException ex) {
                IdentityDatabaseUtil.rollbackTransaction(connection);
                String msg = "Error occurred while deleting application with resourceId: %s in tenantDomain: %s.";
                throw new IdentityApplicationManagementException(String.format(msg, resourceId, tenantDomain), ex);
            }
        } else {
            if (log.isDebugEnabled()) {
                String msg = "Trying to delete a non-existing application with resourceId: %s in " + "tenantDomain: %s.";
                log.debug(String.format(msg, resourceId, tenantDomain));
            }
        }
    } catch (SQLException e) {
        String msg = "Error occurred while deleting application with resourceId: %s in tenantDomain: %s.";
        throw new IdentityApplicationManagementException(String.format(msg, resourceId, tenantDomain), e);
    }
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) ServiceProvider(org.wso2.carbon.identity.application.common.model.ServiceProvider) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Connection(java.sql.Connection)

Example 27 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getCountOfDiscoverableApplications.

@Override
public int getCountOfDiscoverableApplications(String filter, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Getting count of discoverable applications matching filter: " + filter + " in tenantDomain: " + tenantDomain);
    }
    if (StringUtils.isBlank(filter) || "*".equals(filter)) {
        return getCountOfDiscoverableApplications(tenantDomain);
    }
    int count = 0;
    String filterResolvedForSQL = resolveSQLFilter(filter);
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, LOAD_DISCOVERABLE_APP_COUNT_BY_APP_NAME_AND_TENANT)) {
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            statement.setString(ApplicationTableColumns.APP_NAME, filterResolvedForSQL);
            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    count = resultSet.getInt(1);
                }
            }
        }
    } catch (SQLException e) {
        throw new IdentityApplicationManagementServerException("Error while getting count of discoverable " + "applications matching filter:" + filter + " in tenantDomain: " + tenantDomain, e);
    }
    return count;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) IdentityApplicationManagementServerException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)

Example 28 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class ApplicationDAOImpl method getApplicationBasicInfoByResourceId.

@Override
public ApplicationBasicInfo getApplicationBasicInfoByResourceId(String resourceId, String tenantDomain) throws IdentityApplicationManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Getting application basic information for resourceId: " + resourceId + " in tenantDomain: " + tenantDomain);
    }
    ApplicationBasicInfo applicationBasicInfo = null;
    try (Connection connection = IdentityDatabaseUtil.getDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, LOAD_APP_BY_TENANT_AND_UUID)) {
            statement.setInt(ApplicationTableColumns.TENANT_ID, IdentityTenantUtil.getTenantId(tenantDomain));
            statement.setString(ApplicationTableColumns.UUID, resourceId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    applicationBasicInfo = buildApplicationBasicInfo(resultSet);
                }
            }
        }
    } catch (SQLException e) {
        String message = "Error while getting application basic information for resourceId: %s in " + "tenantDomain: %s";
        throw new IdentityApplicationManagementException(String.format(message, resourceId, tenantDomain), e);
    }
    return applicationBasicInfo;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) SQLException(java.sql.SQLException) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ApplicationBasicInfo(org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)

Example 29 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class RoleDAOImpl method getUserListOfRole.

@Override
public List<UserBasicInfo> getUserListOfRole(String roleID, String tenantDomain) throws IdentityRoleManagementException {
    if (!isExistingRoleID(roleID, tenantDomain)) {
        throw new IdentityRoleManagementClientException(ROLE_NOT_FOUND.getCode(), "Role id: " + roleID + " does not exist in the system.");
    }
    List<UserBasicInfo> userList = new ArrayList<>();
    String roleName = getRoleNameByID(roleID, tenantDomain);
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    try {
        UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm();
        if (UserCoreUtil.isEveryoneRole(roleName, userRealm.getRealmConfiguration())) {
            List<org.wso2.carbon.user.core.common.User> users = ((AbstractUserStoreManager) userRealm.getUserStoreManager()).listUsersWithID(RoleConstants.WILDCARD_CHARACTER, -1);
            for (org.wso2.carbon.user.core.common.User user : users) {
                userList.add(new UserBasicInfo(user.getUserID(), user.getDomainQualifiedUsername()));
            }
        }
    } catch (UserStoreException e) {
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), "Error while getting the realmConfiguration.", e);
    }
    List<String> disabledDomainName = getDisabledDomainNames();
    try (Connection connection = IdentityDatabaseUtil.getUserDBConnection(false)) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_USER_LIST_OF_ROLE_SQL, RoleTableColumns.UM_ID)) {
            statement.setString(RoleTableColumns.UM_ROLE_NAME, roleName);
            statement.setInt(RoleTableColumns.UM_TENANT_ID, tenantId);
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    String name = resultSet.getString(1);
                    String domain = resultSet.getString(2);
                    if (!disabledDomainName.contains(domain)) {
                        if (StringUtils.isNotEmpty(domain)) {
                            name = UserCoreUtil.addDomainToName(name, domain);
                        }
                        userList.add(new UserBasicInfo(getUserIDByName(name, tenantDomain), name));
                    }
                }
            }
        }
    } catch (SQLException e) {
        String errorMessage = "Error while while getting the user list of role for role name: %s in the " + "tenantDomain: %s";
        throw new IdentityRoleManagementServerException(UNEXPECTED_SERVER_ERROR.getCode(), String.format(errorMessage, roleName, tenantDomain), e);
    }
    return userList;
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) UserBasicInfo(org.wso2.carbon.identity.role.mgt.core.UserBasicInfo) UserRealm(org.wso2.carbon.user.api.UserRealm) IdentityRoleManagementServerException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException) UserStoreException(org.wso2.carbon.user.api.UserStoreException) ResultSet(java.sql.ResultSet) AbstractUserStoreManager(org.wso2.carbon.user.core.common.AbstractUserStoreManager) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)

Example 30 with NamedPreparedStatement

use of org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement in project carbon-identity-framework by wso2.

the class RoleDAOImpl method batchProcessRoleNames.

private Map<String, String> batchProcessRoleNames(List<String> roleNames, String tenantDomain, Connection connection) throws SQLException, IdentityRoleManagementException {
    Map<String, String> roleNamesToIDs = new HashMap<>();
    int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
    String roleID;
    for (String roleName : roleNames) {
        try (NamedPreparedStatement statement = new NamedPreparedStatement(connection, GET_ROLE_ID_BY_NAME_SQL)) {
            statement.setInt(RoleConstants.RoleTableColumns.TENANT_ID, tenantId);
            statement.setString(RoleConstants.RoleTableColumns.ROLE_NAME, roleName);
            statement.setString(RoleConstants.RoleTableColumns.ATTR_NAME, RoleConstants.ID_URI);
            int count = 0;
            try (ResultSet resultSet = statement.executeQuery()) {
                while (resultSet.next()) {
                    // Handle multiple matching roles.
                    count++;
                    if (count > 1) {
                        String errorMessage = "Invalid scenario. Multiple roles found for the given role name: " + roleName + " and tenantDomain: " + tenantDomain;
                        throw new IdentityRoleManagementClientException(INVALID_REQUEST.getCode(), errorMessage);
                    }
                    roleID = resultSet.getString(1);
                    roleNamesToIDs.put(roleName, roleID);
                }
            }
        }
    }
    return roleNamesToIDs;
}
Also used : NamedPreparedStatement(org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) IdentityRoleManagementClientException(org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)

Aggregations

NamedPreparedStatement (org.wso2.carbon.database.utils.jdbc.NamedPreparedStatement)45 SQLException (java.sql.SQLException)40 Connection (java.sql.Connection)39 ResultSet (java.sql.ResultSet)33 IdentityRoleManagementServerException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementServerException)17 ArrayList (java.util.ArrayList)12 IdentityRoleManagementClientException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementClientException)12 PreparedStatement (java.sql.PreparedStatement)7 CORSOrigin (org.wso2.carbon.identity.cors.mgt.core.model.CORSOrigin)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Test (org.testng.annotations.Test)6 IdentityApplicationManagementException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementException)6 HashMap (java.util.HashMap)5 IdentityApplicationManagementServerException (org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException)4 ApplicationBasicInfo (org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo)4 RoleBasicInfo (org.wso2.carbon.identity.role.mgt.core.RoleBasicInfo)4 NamedPreparedStatement (org.wso2.carbon.identity.oauth2.util.NamedPreparedStatement)3 IdentityRoleManagementException (org.wso2.carbon.identity.role.mgt.core.IdentityRoleManagementException)3 UserRealm (org.wso2.carbon.user.api.UserRealm)3 UserStoreException (org.wso2.carbon.user.api.UserStoreException)3