Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update getSites rest service and builder with additional params - EXO-66028 - Meeds-io/MIPs#72 #2805

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -62,6 +63,7 @@
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.mop.SiteType;
import org.exoplatform.portal.mop.navigation.Scope;
import org.exoplatform.portal.mop.rest.model.UserNodeRestEntity;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.portal.mop.user.HttpUserPortalContext;
import org.exoplatform.portal.mop.user.UserNavigation;
Expand Down Expand Up @@ -1752,14 +1754,29 @@ public static RelationshipManager getRelationshipManager() {

public static List<SiteEntity> buildSiteEntities(List<PortalConfig> sites,
HttpServletRequest request,
boolean expandNavigations) {
return sites.stream().map(site -> buildSiteEntity(site, request, expandNavigations)).filter(Objects::nonNull).toList();
}

public static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest request, boolean expandNavigations) {
if (site == null) {
return null;
}
boolean expandNavigations,
boolean excludeEmptyNavigationSites,
boolean filterByPermission,
boolean sortByDisplayOrder) {
List<PortalConfig> filteredByPermissionSites = sites;
if (filterByPermission) {
filteredByPermissionSites = sites.stream().filter(getUserACL()::hasPermission).toList();
}
List<SiteEntity> siteEntities = filteredByPermissionSites.stream()
.map(site -> buildSiteEntity(site,
request,
expandNavigations,
excludeEmptyNavigationSites))
.filter(Objects::nonNull)
.toList();
return sortByDisplayOrder ? siteEntities
: siteEntities.stream().sorted(Comparator.comparing(SiteEntity::getDisplayName, String.CASE_INSENSITIVE_ORDER)).toList();
}

public static SiteEntity buildSiteEntity(PortalConfig site,
HttpServletRequest request,
boolean expandNavigations,
boolean excludeEmptyNavigationSites) {
SiteType siteType = SiteType.valueOf(site.getType().toUpperCase());
String displayName = site.getLabel();
org.exoplatform.services.security.Identity userIdentity = ConversationState.getCurrent().getIdentity();
Expand All @@ -1779,24 +1796,34 @@ public static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest r
Map<String, Object> editPermission = computePermission(site.getEditPermission());

UserNode rootNode = null;
if (expandNavigations) {
String currentUser = userIdentity.getUserId();
try {
HttpUserPortalContext userPortalContext = new HttpUserPortalContext(request);
UserPortalConfig userPortalConfig =
getUserPortalConfigService().getUserPortalConfig(siteType != SiteType.PORTAL ? getUserPortalConfigService().getDefaultPortal()
: site.getName(),
currentUser,
userPortalContext);

UserPortal userPortal = userPortalConfig.getUserPortal();
UserNavigation navigation = userPortal.getNavigation(new SiteKey(siteType.getName(), site.getName()));
if (navigation != null) {
rootNode = userPortal.getNode(navigation, Scope.ALL, UserNodeFilterConfig.builder().withReadWriteCheck().build(), null);
}
} catch (Exception e) {
LOG.error("Error while getting site {} navigations for user {}", site.getName(), currentUser, e);
String currentUser = userIdentity.getUserId();
try {
HttpUserPortalContext userPortalContext = new HttpUserPortalContext(request);
UserPortalConfig userPortalConfig =
getUserPortalConfigService().getUserPortalConfig(siteType != SiteType.PORTAL ? getUserPortalConfigService().getDefaultPortal()
: site.getName(),
currentUser,
userPortalContext);

UserPortal userPortal = userPortalConfig.getUserPortal();
UserNavigation navigation = userPortal.getNavigation(new SiteKey(siteType.getName(), site.getName()));
if (navigation != null) {
rootNode = userPortal.getNode(navigation, Scope.ALL, UserNodeFilterConfig.builder().withReadWriteCheck().build(), null);
}
} catch (Exception e) {
LOG.error("Error while getting site {} navigations for user {}", site.getName(), currentUser, e);
}

if (excludeEmptyNavigationSites && (rootNode == null || rootNode.getChildren().isEmpty())) {
return null;
}
List<UserNodeRestEntity> siteNavigations = null;
if (expandNavigations && rootNode != null) {
siteNavigations = toUserNodeRestEntity(rootNode.getChildren(),
true,
getOrganizationService(),
getLayoutService(),
getUserACL());
}
return new SiteEntity(siteType,
site.getName(),
Expand All @@ -1807,14 +1834,8 @@ public static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest r
site.isDisplayed(),
site.getDisplayOrder(),
isMetaSite(site.getName()),
rootNode == null ? null
: toUserNodeRestEntity(rootNode.getChildren(),
true,
getOrganizationService(),
getLayoutService(),
getUserACL()),
siteNavigations,
getUserACL().hasPermission(site.getEditPermission()));

}

private static List<Map<String, Object>> computePermissions(String[] permissions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,39 @@ public SiteRest(LayoutService layoutService) {
@ApiResponse(responseCode = "500", description = "Internal server error"), })
public Response getSites(@Context
HttpServletRequest request,
@Parameter(description = "Portal site type, possible values: PORTAL, GROUP or USER", required = true)
@Parameter(description = "Portal site types, possible values: PORTAL, GROUP or USER", required = true)
@QueryParam("siteType")
String siteTypeName,
String siteType,
@Parameter(description = "Site type to be excluded")
@QueryParam("excludedSiteType")
String excludedSiteType,
@Parameter(description = "Site name to be excluded")
@QueryParam("excludedSiteName")
String excludedSiteName,
@Parameter(description = "to exclude space sites")
@DefaultValue("false")
@QueryParam("excludeSpaceSites")
boolean excludeSpaceSites,
@Parameter(description = "to exclude sites with empty navigation")
@DefaultValue("false")
@QueryParam("excludeEmptyNavigationSites")
boolean excludeEmptyNavigationSites,
@Parameter(description = "to expand site navigations nodes")
@DefaultValue("false")
@QueryParam("expandNavigations")
boolean expandNavigations,
@Parameter(description = "to retrieve sites with its displayed status")
@Parameter(description = "to sort with display order")
@DefaultValue("false")
@QueryParam("sortByDisplayOrder")
boolean sortByDisplayOrder,
@Parameter(description = "to filter sites by displayed status")
@DefaultValue("false")
@QueryParam("filterByDisplayed")
boolean filterByDisplayed,
@Parameter(description = "to retrieve sites with displayed status")
@DefaultValue("false")
@QueryParam("displayed")
Boolean displayed,
@Parameter(description = "to retrieve all sites")
@DefaultValue("true")
@QueryParam("allSites")
boolean allSites,
boolean displayed,
@Parameter(description = "to filter sites by view/edit permissions")
@DefaultValue("false")
@QueryParam("filterByPermissions")
Expand All @@ -97,21 +112,25 @@ public Response getSites(@Context
int limit) {
try {
SiteFilter siteFilter = new SiteFilter();
if (siteTypeName != null) {
siteFilter.setSiteType(SiteType.valueOf(siteTypeName.toUpperCase()));
if (siteType != null) {
siteFilter.setSiteType(SiteType.valueOf(siteType.toUpperCase()));
}
if (excludedSiteType != null) {
siteFilter.setExcludedSiteType(SiteType.valueOf(excludedSiteType.toUpperCase()));
}
if (excludedSiteName != null) {
siteFilter.setExcludedSiteName(excludedSiteName);
}
if (displayed != null) {
siteFilter.setExcludeSpaceSites(excludeSpaceSites);
siteFilter.setSortByDisplayOrder(sortByDisplayOrder);
if (filterByDisplayed) {
siteFilter.setFilterByDisplayed(filterByDisplayed);
siteFilter.setDisplayed(displayed);
}
siteFilter.setAllSites(allSites);
siteFilter.setFilterByPermission(filterByPermission);
siteFilter.setLimit(limit);
siteFilter.setOffset(offset);
List<PortalConfig> sites = layoutService.getSites(siteFilter);
return Response.ok(EntityBuilder.buildSiteEntities(sites, request, expandNavigations)).build();
return Response.ok(EntityBuilder.buildSiteEntities(sites, request, expandNavigations, excludeEmptyNavigationSites, filterByPermission, sortByDisplayOrder)).build();
} catch (Exception e) {
LOG.warn("Error while retrieving sites", e);
return Response.serverError().build();
Expand Down
12 changes: 9 additions & 3 deletions webapp/portlet/src/main/webapp/vue-apps/common/js/SiteService.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
export function getSites(siteType, excludedSiteName, expandNavigations, displayed, allSites, filterByPermissions) {
export function getSites(siteType, excludedSiteType, excludedSiteName, excludeEmptyNavigationSites, excludeSpaceSites, expandNavigations, filterByDisplayed, sortByDisplayOrder, displayed, filterByPermissions) {
const formData = new FormData();
if (siteType) {
formData.append('siteType', siteType);
}
if (excludedSiteType) {
formData.append('excludedSiteType', excludedSiteType);
}
if (excludedSiteName) {
formData.append('excludedSiteName', excludedSiteName);
}
formData.append('excludeEmptyNavigationSites', excludeEmptyNavigationSites);
formData.append('excludeSpaceSites', excludeSpaceSites);
formData.append('expandNavigations', expandNavigations);
if (displayed) {
formData.append('filterByDisplayed', filterByDisplayed);
formData.append('sortByDisplayOrder', sortByDisplayOrder);
if (filterByDisplayed) {
formData.append('displayed', displayed);
}
formData.append('allSites', allSites);
formData.append('filterByPermissions', filterByPermissions);
const params = new URLSearchParams(formData).toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ export default {
},
retrieveSites(){
if (eXo.env.portal.newLeftNavigationDrawer) {
return this.$siteService.getSites('PORTAL', 'global', true, true, false, true)
return this.$siteService.getSites('PORTAL', null, 'global', true, true, true, true, true, true, true)
.then(data => this.sites = data || []);
}
},
Expand Down