Skip to content

Commit

Permalink
#24908 include in 23.01.18 LTS
Browse files Browse the repository at this point in the history
  • Loading branch information
erickgonzalez committed Jun 11, 2024
1 parent dfb74e8 commit 723e7f3
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ private void unpublishHost(final Host host, final User user) throws DotHibernate

try {
HibernateUtil.startTransaction();
host.setIndexPolicy(IndexPolicy.FORCE);
host.setIndexPolicy(IndexPolicy.WAIT_FOR);
APILocator.getHostAPI().unpublish(host, user, false);
HibernateUtil.closeAndCommitTransaction();
} catch (Exception e) {
Expand All @@ -520,10 +520,13 @@ private void unpublishHost(final Host host, final User user) throws DotHibernate
* Archives a given host
*/
private void archiveHost(final Host host, final User user) throws DotHibernateException {
if (null == host || null == user) {
return;
}

try {
HibernateUtil.startTransaction();
host.setIndexPolicy(IndexPolicy.FORCE);
host.setIndexPolicy(IndexPolicy.WAIT_FOR);
APILocator.getHostAPI().archive(host, user, false);
HibernateUtil.closeAndCommitTransaction();
} catch (Exception e) {
Expand All @@ -539,11 +542,14 @@ private void archiveHost(final Host host, final User user) throws DotHibernateEx
*/
private void deleteHost(final Host host, final User user)
throws DotHibernateException, InterruptedException, ExecutionException {
if (null == host || null == user) {
return;
}

Optional<Future<Boolean>> hostDeleteResult = Optional.empty();
try {
HibernateUtil.startTransaction();
host.setIndexPolicy(IndexPolicy.FORCE);
host.setIndexPolicy(IndexPolicy.WAIT_FOR);
hostDeleteResult = APILocator.getHostAPI().delete(host, user, false, true);
HibernateUtil.closeAndCommitTransaction();
} catch (Exception e) {
Expand Down Expand Up @@ -1219,14 +1225,15 @@ public void retrieveHostsPerTagStorage() throws DotHibernateException, Execution
}

/**
* Method to test: {@link HostAPI#searchByStopped(String, boolean, boolean, int, int, User, boolean)}
*
* Given Scenario: Create a test Site and stop it. Then, create another Site, then stop it and archive it. Finally,
* compare the total count of stopped Sites.
*
* Expected Result: When compared to the initial stopped Sites count, after stopping the new Site, the count must
* increase by one. After stopping and archivnig the second Site, the count must be increased by two because
* archived Sites are also considered "stopped Sites".
* <ul>
* <li><b>Method to test:
* </b>{@link HostAPI#searchByStopped(String, boolean, boolean, int, int, User, boolean)}</li>
* <li><b>Given Scenario: </b>Create a test Site and stop it. Then, create another Site, then stop it and
* archive it. Finally, compare the total count of stopped Sites.</li>
* <li><b>Expected Result: </b>When compared to the initial stopped Sites count, after stopping the new Site,
* the count must be increased by one. After stopping AND archiving the second Site, the total count difference
* must be 2 because archived Sites are also considered "stopped Sites" as well.</li>
* </ul>
*/
@Test
public void searchByStopped() throws DotHibernateException, ExecutionException, InterruptedException {
Expand All @@ -1242,7 +1249,7 @@ public void searchByStopped() throws DotHibernateException, ExecutionException,
final PaginatedArrayList<Host> stoppedSites = hostAPI.searchByStopped(null, true, false, 0, 0, systemUser,
false);
testSite = siteDataGen.nextPersisted();
unpublishHost(testSite, systemUser);
this.unpublishHost(testSite, systemUser);
final PaginatedArrayList<Host> updatedStoppedSites = hostAPI.searchByStopped(null, true, false, 0, 0,
systemUser, false);

Expand All @@ -1253,8 +1260,8 @@ public void searchByStopped() throws DotHibernateException, ExecutionException,
// Test data generation #2
siteDataGen = new SiteDataGen();
testSiteTwo = siteDataGen.nextPersisted();
unpublishHost(testSiteTwo, systemUser);
archiveHost(testSiteTwo, systemUser);
this.unpublishHost(testSiteTwo, systemUser);
this.archiveHost(testSiteTwo, systemUser);
final PaginatedArrayList<Host> updatedStoppedAndArchivedSites = hostAPI.searchByStopped(null, true,
false, 0, 0, systemUser, false);

Expand Down Expand Up @@ -1366,4 +1373,28 @@ public void count() throws DotHibernateException, ExecutionException, Interrupte
}
}

/**
* <ul>
* <li><b>Method to test: </b>{@link HostAPI#findAllFromDB(User, boolean, boolean)}</li>
* <li><b>Given Scenario: </b>Create a test Site and call the {@findAllFromDB} method that allows you to include
* or exclude the System Host.</li>
* <li><b>Expected Result: </b>When calling the method with the {@code includeSystemHost} parameter as
* {@code true}, the System Host must be included. Otherwise, it must be left out.</li>
* </ul>
*/
@Test
public void findAllFromDB() throws DotDataException, DotSecurityException {
// Initialization
final HostAPI hostAPI = APILocator.getHostAPI();
final User systemUser = APILocator.systemUser();

// Test data generation
final List<Host> siteList = hostAPI.findAllFromDB(systemUser, false, false);
final List<Host> siteListWithSystemHost = hostAPI.findAllFromDB(systemUser, true, false);

// Assertions
assertEquals("The size difference between both Site lists MUST be 1", 1,
siteListWithSystemHost.size() - siteList.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public Map<String, Object> findHostsPaginated(final String filter, final boolean

final User user = this.getLoggedInUser();
final boolean respectFrontend = !this.userWebAPI.isLoggedToBackend(this.getHttpRequest());
final List<Host> sitesFromDb = this.hostAPI.findAllFromDB(user, respectFrontend);
final List<Host> sitesFromDb = this.hostAPI.findAllFromDB(user, false,respectFrontend);
final List<Field> fields = FieldsCache.getFieldsByStructureVariableName(Host.HOST_VELOCITY_VAR_NAME);
final List<Field> searchableFields = fields.stream().filter(field -> field.isListed() && field
.getFieldType().startsWith("text")).collect(Collectors.toList());

List<Map<String, Object>> siteList = new ArrayList<>(sitesFromDb.size());
Collections.sort(sitesFromDb, new HostNameComparator());
sitesFromDb.sort(new HostNameComparator());
for (final Host site : sitesFromDb) {
boolean addToResultList = false;
if (showArchived || !site.isArchived()) {
Expand Down Expand Up @@ -174,7 +174,7 @@ public Map<String, Object> findHostsPaginated(final String filter, final boolean
}
}

final List<Map<String, Object>> fieldMapList = fields.stream().map(field -> field.getMap()).collect(Collectors.toList());
final List<Map<String, Object>> fieldMapList = fields.stream().map(Field::getMap).collect(Collectors.toList());
final Structure siteContentType = CacheLocator.getContentTypeCache().getStructureByVelocityVarName(Host.HOST_VELOCITY_VAR_NAME);
return CollectionsUtils.map(
"total", totalResults,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ Host find(Contentlet contentlet,
*/
List<Host> findAllFromDB(final User user, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException;

/**
* Returns the complete list of Sites in your dotCMS repository retrieved <b>directly from the data source</b>. This
* method allows you to <b>EXCLUDE</b> the System Host from the result list.
*
* @param user The {@link User} that is calling this method.
* @param includeSystemHost If the System Host must be included in the results, set to {@code true}.
* @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this
* operation, set to {@code true}. Otherwise, set to {@code false}.
*
* @return The list of {@link Host} objects.
*
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotSecurityException The specified User does not have the required permissions to perform this
* operation.
*/
List<Host> findAllFromDB(final User user, final boolean includeSystemHost, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException;

/**
* Returns the complete list of Sites in your dotCMS repository retrieved from the cache. If no data is currently
* available, it will be retrieved from the data source, and put into the respective cache region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
*/
public class HostAPIImpl implements HostAPI, Flushable<Host> {

private HostCache hostCache = CacheLocator.getHostCache();
private final HostCache hostCache = CacheLocator.getHostCache();
private Host systemHost;
private final SystemEventsAPI systemEventsAPI;
private HostFactory hostFactory;
Expand Down Expand Up @@ -320,7 +320,13 @@ public List<Host> findAll(final User user, final int limit, final int offset, fi
@Override
public List<Host> findAllFromDB(final User user, final boolean respectFrontendRoles) throws DotDataException,
DotSecurityException {
return this.findPaginatedSitesFromDB(user, 0, 0, null, respectFrontendRoles);
return this.findAllFromDB(user, true, respectFrontendRoles);
}

@Override
public List<Host> findAllFromDB(final User user, final boolean includeSystemHost,
final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
return this.findPaginatedSitesFromDB(user, 0, 0, null, includeSystemHost, respectFrontendRoles);
}

/**
Expand All @@ -345,7 +351,33 @@ public List<Host> findAllFromDB(final User user, final boolean respectFrontendRo
@CloseDBIfOpened
private List<Host> findPaginatedSitesFromDB(final User user, final int limit, final int offset, final String
sortBy, final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
final List<Host> siteList = this.getHostFactory().findAll(limit, offset, sortBy);
return this.findPaginatedSitesFromDB(user, limit, offset, sortBy, true, respectFrontendRoles);
}

/**
* Returns an optionally paginated list of all Sites in your dotCMS content repository. This method allows you to
* <b>EXCLUDE</b> the System Host from the result list.
*
* @param user The {@link User} performing this action.
* @param limit Limit of results returned in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param offset Expected offset of results in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param sortBy Optional sorting criterion, as specified by the available columns in: {@link
* com.dotmarketing.common.util.SQLUtil#ORDERBY_WHITELIST}
* @param includeSystemHost If the System Host should be included in the result list, set to {@code true}.
* @param respectFrontendRoles If the User's front-end roles need to be taken into account in order to perform this
* operation, set to {@code true}. Otherwise, set to {@code false}.
*
* @return The list of {@link Host} objects.
*
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotSecurityException The specified User does not have the required permissions to perform this
* operation.
*/
private List<Host> findPaginatedSitesFromDB(final User user, final int limit, final int offset,final String sortBy, final boolean includeSystemHost,
final boolean respectFrontendRoles) throws DotDataException, DotSecurityException {
final List<Host> siteList = this.getHostFactory().findAll(limit, offset, sortBy, includeSystemHost);
if (null != siteList && !siteList.isEmpty()) {
return siteList.stream().filter(site -> {
try {
Expand All @@ -354,11 +386,12 @@ private List<Host> findPaginatedSitesFromDB(final User user, final int limit, fi
.doesSystemHostHavePermissions(APILocator.systemHost(), user,
respectFrontendRoles, Host.class.getCanonicalName());
}
checkSitePermission(user, respectFrontendRoles, site);
this.checkSitePermission(user, respectFrontendRoles, site);
return true;
} catch (final DotDataException | DotSecurityException e) {
Logger.warn(this, String.format("An error occurred when checking permissions from User '%s' on " +
"Site '%s': %s", user.getUserId(), site.getInode(), e.getMessage()));
Logger.warn(this,
String.format("An error occurred when checking permissions from User '%s' on " + "Site " +
"'%s': %s", user.getUserId(), site.getInode(), e.getMessage()));
}
return false;
}).collect(Collectors.toList());
Expand Down Expand Up @@ -831,7 +864,7 @@ public PaginatedArrayList<Host> search(final String filter, final boolean showAr
}
}
if (showStopped && !showArchived) {
// Return stopped Sites
// Return stopped Sites, which include archived Sites as well
siteListOpt = this.getHostFactory()
.findLiveAndStopped(filter, limit, offset, showSystemHost, user, respectFrontendRoles);
if (siteListOpt.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ public interface HostFactory {
*/
List<Host> findAll(final int limit, final int offset, final String orderBy) throws DotDataException, DotSecurityException;

/**
* Returns the list of Sites in your dotCMS repository retrieved <b>directly from the data source</b> matching the
* specified search criteria.
*
* @param limit Limit of results returned in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param offset Expected offset of results in the response, for pagination purposes. If set equal or
* lower than zero, this parameter will be ignored.
* @param orderBy Optional sorting criterion
* @param includeSystemHost If the System Host should be included in the results, set to {@code true}.
*
* @return The list of {@link Host} objects.
*
* @throws DotDataException An error occurred when accessing the data source.
* @throws DotSecurityException The specified User does not have the required permissions to perform this
* operation.
*/
List<Host> findAll(final int limit, final int offset, final String orderBy, final boolean includeSystemHost)
throws DotDataException, DotSecurityException;

/**
* Retrieves the System Host object.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dotmarketing.portlets.contentlet.business;

import com.dotcms.business.CloseDBIfOpened;
import com.dotcms.business.WrapInTransaction;
import com.dotcms.concurrent.DotConcurrentFactory;
import com.dotcms.content.business.json.ContentletJsonAPI;
Expand Down Expand Up @@ -285,11 +286,21 @@ public List<Host> findAll() throws DotDataException, DotSecurityException {
}

@Override
public List<Host> findAll(int limit, int offset, String orderBy) throws DotDataException, DotSecurityException {
public List<Host> findAll(final int limit, final int offset, final String orderBy) throws DotDataException, DotSecurityException {
return findAll(limit,offset, orderBy, true);
}

@CloseDBIfOpened
@Override
public List<Host> findAll(final int limit, final int offset, final String orderBy, final boolean includeSystemHost) throws DotDataException, DotSecurityException {
final DotConnect dc = new DotConnect();
final StringBuffer sqlQuery = new StringBuffer().append(SELECT_SITE_INODE)
final StringBuilder sqlQuery = new StringBuilder().append(SELECT_SITE_INODE)
.append(WHERE)
.append(EXCLUDE_SYSTEM_HOST);
.append(" true ");
if (!includeSystemHost) {
sqlQuery.append(AND);
sqlQuery.append(EXCLUDE_SYSTEM_HOST);
}
final String sanitizedSortBy = SQLUtil.sanitizeSortBy(orderBy);
if (UtilMethods.isSet(sanitizedSortBy)) {
sqlQuery.append(ORDER_BY);
Expand All @@ -305,8 +316,7 @@ public List<Host> findAll(int limit, int offset, String orderBy) throws DotDataE
dc.setStartRow(offset);
}
final List<Map<String, String>> dbResults = dc.loadResults();
final List<Host> siteList = convertDbResultsToSites(dbResults);
return siteList;
return this.convertDbResultsToSites(dbResults);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions hotfix_tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,5 @@ This maintenance release includes the following code fixes:
194. https://github.com/dotCMS/core/issues/28360 : Move Async Email Actionlet to Core #28360

**Release-23.01.18**

195. https://github.com/dotCMS/core/issues/24908 : Lost ability to select system host #24908

0 comments on commit 723e7f3

Please sign in to comment.