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: Implement left navigation level 1 for sites - EXO-65477 - Meeds-io/MIPs#72 #2771

Merged
merged 4 commits into from
Sep 4, 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 @@ -24,6 +24,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.*;
import javax.ws.rs.core.Response.ResponseBuilder;

Expand All @@ -37,10 +38,20 @@
import org.exoplatform.commons.utils.ListAccess;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.portal.application.localization.LocalizationFilter;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.config.UserPortalConfig;
import org.exoplatform.portal.config.UserPortalConfigService;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.SiteFilter;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.mop.SiteType;
import org.exoplatform.portal.mop.navigation.Scope;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.portal.mop.user.HttpUserPortalContext;
import org.exoplatform.portal.mop.user.UserNavigation;
import org.exoplatform.portal.mop.user.UserNode;
import org.exoplatform.portal.mop.user.UserNodeFilterConfig;
import org.exoplatform.portal.mop.user.UserPortal;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.organization.*;
Expand Down Expand Up @@ -75,6 +86,7 @@
import org.exoplatform.social.service.rest.Util;
import org.exoplatform.social.service.rest.api.VersionResources;
import org.exoplatform.ws.frameworks.json.impl.*;
import static org.exoplatform.portal.mop.rest.EntityBuilder.toUserNodeRestEntity;

public class EntityBuilder {

Expand Down Expand Up @@ -151,6 +163,14 @@ public class EntityBuilder {
public static final CacheControl NO_CACHE_CC = new CacheControl();

private static final JsonEntityProvider JSON_ENTITY_PROVIDER = new JsonEntityProvider();

public static final String GROUP = "group";

private static UserPortalConfigService userPortalConfigService;

private static LayoutService layoutService;

private static UserACL userACL;

static {
NO_CACHE_CC.setNoCache(true);
Expand Down Expand Up @@ -1688,4 +1708,104 @@ public static RelationshipManager getRelationshipManager() {
}
return relationshipManager;
}
}

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

private static SiteEntity buildSiteEntity(PortalConfig site, HttpServletRequest request, SiteFilter siteFilter) {
if (site == null) {
return null;
}
SiteType siteType = SiteType.valueOf(site.getType().toUpperCase());
String displayName = site.getLabel();
org.exoplatform.services.security.Identity userIdentity = ConversationState.getCurrent().getIdentity();
if (SiteType.GROUP.equals(siteType)) {
try {
Group siteGroup = getOrganizationService().getGroupHandler().findGroupById(site.getName());
if (siteGroup == null || !userIdentity.isMemberOf(siteGroup.getId())) {
return null;
} else if (StringUtils.isBlank(displayName)) {
displayName = siteGroup.getLabel();
}
} catch (Exception e) {
LOG.error("Error while retrieving group with name ", site.getName(), e);
}
}
List<Map<String, Object>> accessPermissions = computePermissions(site.getAccessPermissions());
Map<String, Object> editPermission = computePermission(site.getEditPermission());

UserNode rootNode = null;
if (siteFilter.isExpandNavigations()) {
String currentUser = userIdentity.getUserId();
try {
HttpUserPortalContext userPortalContext = new HttpUserPortalContext(request);
UserPortalConfig userPortalCfg = getUserPortalConfigService().getUserPortalConfig(site.getName(),
currentUser,
userPortalContext);
UserPortal userPortal = userPortalCfg.getUserPortal();
UserNavigation navigation = userPortal.getNavigation(new SiteKey(siteType.getName(), site.getName()));
rootNode = userPortal.getNode(navigation, Scope.ALL, UserNodeFilterConfig.builder().build(), null);
} catch (Exception e) {
LOG.error("Error while getting site {} navigations for user {}", site.getName(), currentUser, e);
}
}
return new SiteEntity(siteType,
site.getName(),
!StringUtils.isBlank(displayName) ? displayName : site.getName(),
site.getDescription(),
accessPermissions,
editPermission,
site.isDisplayed(),
site.getDisplayOrder(),
isMetaSite(site.getName()),
rootNode == null ? null
: toUserNodeRestEntity(rootNode.getChildren(),
true,
getOrganizationService(),
getLayoutService(),
getUserACL()));

}

private static List<Map<String, Object>> computePermissions(String[] permissions) {
return Arrays.stream(permissions).map(EntityBuilder::computePermission).toList();
}

private static Map<String, Object> computePermission(String permission) {
Map<String, Object> accessPermission = new HashMap<>();
try {
accessPermission.put("membershipType", permission.split(":")[0]);
accessPermission.put(GROUP, getOrganizationService().getGroupHandler().findGroupById(permission.split(":")[1]));
} catch (Exception e) {
LOG.error("Error while computing user permission ", permission, e);
}
return accessPermission;
}

private static UserPortalConfigService getUserPortalConfigService() {
if (userPortalConfigService == null) {
userPortalConfigService =
ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(UserPortalConfigService.class);
}
return userPortalConfigService;
}

private static UserACL getUserACL() {
if (userACL == null) {
userACL = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(UserACL.class);
}
return userACL;
}

private static LayoutService getLayoutService() {
if (layoutService == null) {
layoutService = ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(LayoutService.class);
}
return layoutService;
}

private static boolean isMetaSite(String siteName) {
return getUserPortalConfigService().getDefaultPortal().equals(siteName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
* Copyright (C) 2023 Meeds Association
* [email protected]
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.exoplatform.social.rest.entity;

import java.util.List;
import java.util.Map;

import org.exoplatform.portal.mop.SiteType;
import org.exoplatform.portal.mop.rest.model.UserNodeRestEntity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SiteEntity {

private SiteType siteType;

private String name;

private String displayName;

private String description;

private List<Map<String, Object>> accessPermissions;

private Map<String, Object> editPermission;

private boolean displayed;

private int displayOrder;

private boolean metaSite;

List<UserNodeRestEntity> siteNavigations;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
* Copyright (C) 2023 Meeds Association
* [email protected]
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.exoplatform.social.rest.impl.site;

import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.portal.mop.SiteFilter;
import org.exoplatform.portal.mop.SiteType;
import org.exoplatform.portal.mop.service.LayoutService;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.resource.ResourceContainer;
import org.exoplatform.social.rest.api.EntityBuilder;
import org.exoplatform.social.service.rest.api.VersionResources;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@Path(VersionResources.VERSION_ONE + "/social/sites")
@Tag(name = VersionResources.VERSION_ONE + "/social/sites", description = "Manage sites")
public class SiteRest implements ResourceContainer {

private static final Log LOG = ExoLogger.getLogger(SiteRest.class);

private LayoutService layoutService;

public SiteRest(LayoutService layoutService) {
this.layoutService = layoutService;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed("users")
@Operation(summary = "Gets sites", description = "Gets sites", method = "GET")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Request fulfilled"),
@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)
@QueryParam("siteType")
String siteTypeName,
@Parameter(description = "Site name to be excluded")
@QueryParam("excludedSiteName")
String excludedSiteName,
@Parameter(description = "to include empty site in results in portal type case")
@DefaultValue("true")
@QueryParam("includeEmpty")
boolean includeEmpty,
@Parameter(description = "to expand site navigations nodes")
@DefaultValue("false")
@QueryParam("expandNavigations")
boolean expandNavigations,
@Parameter(description = "to retrieve sites with its displayed status")
@DefaultValue("false")
@QueryParam("displayed")
boolean displayed,
@Parameter(description = "to retrieve all sites")
@DefaultValue("true")
@QueryParam("allSites")
boolean allSites,
@Parameter(description = "to filter sites by view/edit permissions")
@DefaultValue("false")
@QueryParam("filterByPermissions")
boolean filterByPermission,
@Parameter(description = "Offset of results to retrieve")
@QueryParam("offset")
@DefaultValue("0")
int offset,
@Parameter(description = "Limit of results to retrieve")
@QueryParam("limit")
@DefaultValue("0")
int limit) {
try {
SiteFilter siteFilter = new SiteFilter();
if (siteTypeName != null) {
siteFilter.setSiteType(SiteType.valueOf(siteTypeName.toUpperCase()));
}
siteFilter.setExcludedSiteName(excludedSiteName);
siteFilter.setIncludeEmpty(includeEmpty);
siteFilter.setDisplayed(displayed);
siteFilter.setAllSites(allSites);
siteFilter.setExpandNavigations(expandNavigations);
siteFilter.setFilterByPermission(filterByPermission);
siteFilter.setLimit(limit);
siteFilter.setOffset(offset);
List<PortalConfig> sites = layoutService.getSites(siteFilter);
return Response.ok(EntityBuilder.buildSiteEntities(sites, request, siteFilter)).build();
} catch (Exception e) {
LOG.warn("Error while retrieving sites", e);
return Response.serverError().build();
}
}
}
Loading
Loading