Skip to content

Commit

Permalink
fix(hosts) : retrieve live version if a host has live and working ver…
Browse files Browse the repository at this point in the history
…sions (#30358)

#Closes #28362

### Proposed Changes
* For the sites portlet, if a site has live and working versions, the
live version is shown.
* When resolving a site by name, id or alias, the live version of the
site is retrieved for front-end users.

### Checklist
- [x] Tests

---------

Co-authored-by: Kevin Davila <[email protected]>
Co-authored-by: Jalinson Diaz <[email protected]>
Co-authored-by: Jonathan <[email protected]>
Co-authored-by: Will Ezell <[email protected]>
Co-authored-by: erickgonzalez <[email protected]>
Co-authored-by: Humberto Morera <[email protected]>
Co-authored-by: Jonathan Gamba <[email protected]>
Co-authored-by: Nicolas Molina Monroy <[email protected]>
Co-authored-by: Rafael Velazco <[email protected]>
Co-authored-by: freddyDOTCMS <[email protected]>
Co-authored-by: Fabrizzio Araya <[email protected]>
Co-authored-by: Daniel Enrique Colina Rodríguez <[email protected]>
Co-authored-by: Jose Castro <[email protected]>
  • Loading branch information
14 people authored Nov 12, 2024
1 parent b852b6c commit 4c5d2c5
Show file tree
Hide file tree
Showing 17 changed files with 1,111 additions and 496 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.containers.business.ContainerStructureFinderStrategyResolver;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.folders.business.ApplicationContainerFolderListener;
import com.dotmarketing.portlets.folders.business.ApplicationTemplateFolderListener;
import com.dotmarketing.portlets.folders.model.Folder;
Expand Down Expand Up @@ -85,7 +86,8 @@ public void initApplicationContainerFolderListener() {
try {

final User user = APILocator.systemUser();
final List<Host> hosts = APILocator.getHostAPI().findAllFromDB(user, false);
final List<Host> hosts = APILocator.getHostAPI().findAllFromDB(user,
HostAPI.SearchType.INCLUDE_SYSTEM_HOST);
final ApplicationContainerFolderListener listener = new ApplicationContainerFolderListener();
for (final Host host : hosts) {

Expand All @@ -107,7 +109,8 @@ public void initApplicationTemplateFolderListener() {
try {

final User user = APILocator.systemUser();
final List<Host> hosts = APILocator.getHostAPI().findAllFromDB(user, false);
final List<Host> hosts = APILocator.getHostAPI().findAllFromDB(user,
HostAPI.SearchType.INCLUDE_SYSTEM_HOST);
final ApplicationTemplateFolderListener listener = new ApplicationTemplateFolderListener();
for (final Host host : hosts) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.dotmarketing.exception.DotRuntimeException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.DotContentletValidationException;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.Contentlet;
import com.dotmarketing.portlets.languagesmanager.business.LanguageAPI;
import com.dotmarketing.portlets.languagesmanager.model.Language;
Expand Down Expand Up @@ -115,7 +116,8 @@ private void populateVanityURLsCacheBySite(final Host site) {

@Override
public void populateAllVanityURLsCache() throws DotDataException {
for (final Host site : Try.of(() -> APILocator.getHostAPI().findAllFromDB(APILocator.systemUser(), false)).getOrElse(List.of())) {
for (final Host site : Try.of(() -> APILocator.getHostAPI().findAllFromDB(APILocator.systemUser(),
HostAPI.SearchType.INCLUDE_SYSTEM_HOST)).getOrElse(List.of())) {
populateVanityURLsCacheBySite(site);
}
populateVanityURLsCacheBySite(APILocator.getHostAPI().findSystemHost());
Expand Down
20 changes: 17 additions & 3 deletions dotCMS/src/main/java/com/dotmarketing/business/ajax/HostAjax.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.dotcms.repackage.org.directwebremoting.WebContext;
import com.dotcms.repackage.org.directwebremoting.WebContextFactory;
import com.dotcms.util.CollectionsUtils;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
Expand All @@ -16,6 +15,7 @@
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.portlets.contentlet.business.ContentletAPI;
import com.dotmarketing.portlets.contentlet.business.HostAPI;
import com.dotmarketing.portlets.contentlet.model.ContentletVersionInfo;
import com.dotmarketing.portlets.structure.model.Field;
import com.dotmarketing.portlets.structure.model.Structure;
import com.dotmarketing.quartz.QuartzUtils;
Expand All @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -123,7 +124,11 @@ public Map<String, Object> findHostsForDataStore(String filter, boolean showArch
public Map<String, Object> findHostsPaginated(final String filter, final boolean showArchived, int offset, int count) throws DotDataException, DotSecurityException, PortalException, SystemException {
final User user = this.getLoggedInUser();
final boolean respectFrontend = !this.userWebAPI.isLoggedToBackend(this.getHttpRequest());
final List<Host> sitesFromDb = this.hostAPI.findAllFromDB(user, false, respectFrontend);
final List<HostAPI.SearchType> searchTypes = respectFrontend ?
List.of(HostAPI.SearchType.RESPECT_FRONT_END_ROLES, HostAPI.SearchType.LIVE_ONLY) :
List.of(HostAPI.SearchType.LIVE_ONLY);
final List<Host> sitesFromDb = this.hostAPI.findAllFromDB(user,
searchTypes.toArray(new HostAPI.SearchType[0]));
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());
Expand Down Expand Up @@ -153,12 +158,21 @@ public Map<String, Object> findHostsPaginated(final String filter, final boolean
Logger.warn(HostAjax.class, String.format("An error occurred when reviewing the creation status for " +
"Site '%s': %s", site.getIdentifier(), e.getMessage()), e);
}
String workingInode = site.getInode();
if (site.isLive() && !site.isWorking()) {
final Optional<ContentletVersionInfo> versionInfo = APILocator.getVersionableAPI()
.getContentletVersionInfo(site.getIdentifier(), site.getLanguageId());
if (versionInfo.isPresent()) {
workingInode = versionInfo.get().getWorkingInode();
}
}
final Map<String, Object> siteInfoMap = site.getMap();
siteInfoMap.putAll(Map.of(
"userPermissions", this.permissionAPI.getPermissionIdsFromUser(site, user),
"hostInSetup", siteInSetup,
"archived", site.isArchived(),
"live", site.isLive()));
"live", site.isLive(),
"workingInode", workingInode));
siteList.add(siteInfoMap);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void remove(final String key){
Logger.debug(this, "Cache not able to be removed", e);
}

final Host host = CacheLocator.getHostCache().getById(key);
final Host host = CacheLocator.getHostCache().getById(key, false);
if(host != null){
CacheLocator.getHostCache().remove(host);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
public interface HostAPI {

/**
* Will return a List of a host's aliases If no host aliases will return empty list
* @param host Host
* @return List
* This class is used to define the options for the search of a host.
*/
enum SearchType {
LIVE_ONLY, RESPECT_FRONT_END_ROLES, INCLUDE_SYSTEM_HOST
}

/**
* Returns the aliases of a given Site in the form of a list. In the Site's properties, aliases can be separated by:
Expand Down Expand Up @@ -184,7 +185,7 @@ Host find(Contentlet contentlet,
/**
* @deprecated This method is basically duplicated code. Use one of the following methods instead:
* <ul>
* <li>{@link #findAllFromDB(User, boolean)}</li>
* <li>{@link #findAllFromDB(User, SearchType...)}</li>
* <li>{@link #findAllFromCache(User, boolean)}</li>
* <li>{@link #search(String, boolean, boolean, int, int, User, boolean)}</li>
* </ul>
Expand Down Expand Up @@ -218,38 +219,19 @@ Host find(Contentlet contentlet,
*/
List<Host> findAll(User user, int limit, int offset, String sortBy, boolean respectFrontendRoles) throws DotDataException, DotSecurityException;

/**
* Returns the complete list of Sites in your dotCMS repository retrieved <b>directly from the data source</b>,
* including the System Host.
*
* @param user The {@link User} that is calling this method.
* @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 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}.
* method allows you to <b>EXCLUDE</b> the System Host from the result list. Additionally, you can specify whether
* you want to retrieve the live version of the Site over its working version if available.
*
* @param user The {@link User} that is calling this method.
* @param searchTypes The search options to be used when retrieving the list of Sites.
* @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;
List<Host> findAllFromDB(final User user, final SearchType... searchTypes) throws DotDataException, DotSecurityException;

/**
* Returns the complete list of Sites in your dotCMS repository retrieved from the cache. If no data is currently
Expand Down Expand Up @@ -415,7 +397,7 @@ List<Host> getHostsWithPermission(final int permissionType, final boolean includ
*
* @param host
*/
public void updateCache(Host host);
public void updateCache(Host host) throws DotDataException, DotSecurityException;

/**
* Updates the MenuLinks of the host with the new hostname
Expand Down
Loading

0 comments on commit 4c5d2c5

Please sign in to comment.