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

refactor: disallow parametrized items in auto menu (#2392) (CP: 24.4) #2456

Merged
merged 3 commits into from
May 22, 2024
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 @@ -18,34 +18,31 @@
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.router.BeforeEnterListener;
import com.vaadin.flow.router.RouteParameterData;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.auth.MenuAccessControl;
import com.vaadin.flow.server.auth.NavigationAccessControl;
import com.vaadin.flow.server.auth.ViewAccessChecker;
import com.vaadin.hilla.route.records.ClientViewConfig;

import org.jsoup.nodes.DataNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.Nullable;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.lang.Nullable;

import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.flow.internal.AnnotationReader;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.RouteData;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.router.RouteParameterData;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
import com.vaadin.flow.server.auth.MenuAccessControl;
import com.vaadin.flow.server.auth.NavigationAccessControl;
import com.vaadin.flow.server.auth.ViewAccessChecker;
import com.vaadin.flow.server.communication.IndexHtmlRequestListener;
import com.vaadin.flow.server.communication.IndexHtmlResponse;
import com.vaadin.hilla.route.records.AvailableViewInfo;
Expand Down Expand Up @@ -141,111 +138,80 @@ protected Map<String, AvailableViewInfo> collectClientViews(
deploymentConfiguration);
}

var clientViews = new HashMap<String, AvailableViewInfo>();
clientRouteRegistry.getAllRoutes().entrySet().stream()
.filter(clientViewConfigEntry -> routeUtil.isRouteAllowed(
isUserInRole, isUserAuthenticated,
clientViewConfigEntry.getValue()))
.forEach(clientViewConfigEntry -> {
final String route = clientViewConfigEntry.getKey();
final ClientViewConfig config = clientViewConfigEntry
.getValue();
final AvailableViewInfo availableViewInfo = new AvailableViewInfo(
config.getTitle(), config.getRolesAllowed(),
config.isLoginRequired(), config.getRoute(),
config.isLazy(), config.isAutoRegistered(),
config.menu(), config.getRouteParameters());
clientViews.put(route, availableViewInfo);
});
return clientViews;
return clientRouteRegistry.getAllRoutes().values().stream()
.filter(config -> config.getRouteParameters() == null
|| config.getRouteParameters().isEmpty()
|| config.getRouteParameters().values().stream()
.noneMatch(
param -> param == RouteParamType.REQUIRED))
.filter(config -> routeUtil.isRouteAllowed(isUserInRole,
isUserAuthenticated, config))
.map(config -> new AvailableViewInfo(config.getTitle(),
config.getRolesAllowed(), config.isLoginRequired(),
config.getRoute(), config.isLazy(),
config.isAutoRegistered(), config.menu(),
config.getRouteParameters()))
.collect(Collectors.toMap(AvailableViewInfo::route,
Function.identity()));
}

protected Map<String, AvailableViewInfo> collectServerViews(
VaadinRequest vaadinRequest) {
var serverViews = new HashMap<String, AvailableViewInfo>();
final VaadinService vaadinService = VaadinService.getCurrent();
if (vaadinService == null) {
LOGGER.debug(
"No VaadinService found, skipping server view collection");
return serverViews;
return Collections.emptyMap();
}
final RouteRegistry serverRouteRegistry = vaadinService.getRouter()
.getRegistry();
final var serverRouteRegistry = vaadinService.getRouter().getRegistry();

List<BeforeEnterListener> accessControls = Stream
.of(accessControl, viewAccessChecker).filter(Objects::nonNull)
.toList();
var accessControls = Stream.of(accessControl, viewAccessChecker)
.filter(Objects::nonNull).toList();

List<RouteData> serverRoutes = Collections.emptyList();
var serverRoutes = Collections.<RouteData> emptyList();
if (vaadinService.getInstantiator().getMenuAccessControl()
.getPopulateClientSideMenu() == MenuAccessControl.PopulateClientMenu.ALWAYS
|| clientRouteRegistry.hasMainLayout()) {
serverRoutes = serverRouteRegistry
.getRegisteredAccessibleMenuRoutes(vaadinRequest,
accessControls);
}
serverRoutes.forEach(serverView -> {
final Class<? extends com.vaadin.flow.component.Component> viewClass = serverView
.getNavigationTarget();
final boolean excludeFromMenu;
final String targetUrl = serverView.getTemplate();
if (targetUrl != null) {
final String url;
if (serverView.getRouteParameters() != null
&& !serverView.getRouteParameters().isEmpty()) {
String editUrl = "/" + targetUrl;
excludeFromMenu = serverView.getRouteParameters().values()
.stream()
.anyMatch(param -> !param.getTemplate()
.contains("?")
&& !param.getTemplate().contains("*"));
for (RouteParameterData param : serverView
.getRouteParameters().values().stream()
.filter(param -> param.getTemplate().contains("?")
|| param.getTemplate().contains("*"))
.toList()) {
editUrl = editUrl.replace("/" + param.getTemplate(),
"");
}
if (editUrl.isEmpty()) {
editUrl = "/";
}
url = editUrl;
} else {
excludeFromMenu = false;
url = "/" + targetUrl;
}
return serverRoutes.stream()
.filter(serverView -> serverView.getTemplate() != null)
.map(serverView -> {
final var viewClass = serverView.getNavigationTarget();
final var url = getRouteUrl(serverView);

final String title;
PageTitle pageTitle = AnnotationReader
.getAnnotationFor(viewClass, PageTitle.class)
.orElse(null);
if (pageTitle != null) {
title = pageTitle.value();
} else {
title = serverView.getNavigationTarget().getSimpleName();
}

final ClientViewMenuConfig menuConfig = Optional
.ofNullable(serverView.getMenuData())
.map(menu -> new ClientViewMenuConfig(
(menu.getTitle() == null
|| menu.getTitle().isBlank()) ? title
: menu.getTitle(),
menu.getOrder(), menu.getIcon(),
excludeFromMenu || menu.isExclude()))
.orElse(null);
final String title;
var pageTitle = AnnotationReader
.getAnnotationFor(viewClass, PageTitle.class)
.orElse(null);
if (pageTitle != null) {
title = pageTitle.value();
} else {
title = serverView.getNavigationTarget()
.getSimpleName();
}

final Map<String, RouteParamType> routeParameters = getRouteParameters(
serverView);
final var menuConfig = Optional
.ofNullable(serverView.getMenuData())
.map(menu -> new ClientViewMenuConfig(
(menu.getTitle() == null
|| menu.getTitle().isBlank())
? title
: menu.getTitle(),
menu.getOrder(), menu.getIcon(),
menu.isExclude()))
.orElse(null);

final AvailableViewInfo availableViewInfo = new AvailableViewInfo(
title, null, false, url, false, false, menuConfig,
routeParameters);
serverViews.put(url, availableViewInfo);
}
});
return serverViews;
return new AvailableViewInfo(title, null, false, url, false,
false, menuConfig,
serverView.getRouteParameters().values().stream());
})
.filter(view -> view.routeParameters().values().stream()
.noneMatch(param -> param == RouteParamType.REQUIRED))
.collect(Collectors.toMap(AvailableViewInfo::route,
Function.identity()));
}

private Map<String, RouteParamType> getRouteParameters(
Expand All @@ -265,4 +231,32 @@ private Map<String, RouteParamType> getRouteParameters(
});
return routeParameters;
}

/**
* Get the route url for the route. If the route has optional parameters,
* the url is stripped off from them.
*
* @param route
* route to get url for
* @return url for the route
*/
private static String getRouteUrl(RouteData route) {
if (route.getRouteParameters() != null
&& !route.getRouteParameters().isEmpty()) {
String editUrl = "/" + route.getTemplate();
var params = route.getRouteParameters().values().stream()
.filter(param -> param.getTemplate().contains("?")
|| param.getTemplate().contains("*"))
.toList();
for (RouteParameterData param : params) {
editUrl = editUrl.replace("/" + param.getTemplate(), "");
}
if (editUrl.isEmpty()) {
editUrl = "/";
}
return editUrl;
} else {
return "/" + route.getTemplate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
*/
package com.vaadin.hilla.route.records;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.vaadin.flow.router.RouteParameterData;

/**
* Represents a server side view configuration for the client side.
Expand All @@ -34,11 +38,25 @@
* @param routeParameters
*/
public record AvailableViewInfo(String title, String[] rolesAllowed,
Boolean requiresLogin,
String route, Boolean lazy,
Boolean register,
ClientViewMenuConfig menu,
@JsonProperty("params") Map<String, RouteParamType> routeParameters) {
Boolean requiresLogin, String route, Boolean lazy, Boolean register,
ClientViewMenuConfig menu,
@JsonProperty("params") Map<String, RouteParamType> routeParameters) {

public AvailableViewInfo(String title, String[] rolesAllowed,
Boolean requiresLogin, String route, Boolean lazy, Boolean register,
ClientViewMenuConfig menu,
Stream<RouteParameterData> routeParameters) {
this(title, rolesAllowed, requiresLogin, route, lazy, register, menu,
routeParameters.collect(Collectors
.toMap(RouteParameterData::getTemplate, (params) -> {
if (params.getTemplate().contains("*")) {
return RouteParamType.WILDCARD;
} else if (params.getTemplate().contains("?")) {
return RouteParamType.OPTIONAL;
}
return RouteParamType.REQUIRED;
})));
}

@Override
public boolean equals(final Object o) {
Expand All @@ -49,31 +67,29 @@ public boolean equals(final Object o) {
}
final AvailableViewInfo that = (AvailableViewInfo) o;
return Objects.equals(title, that.title)
&& Arrays.equals(rolesAllowed, that.rolesAllowed)
&& Objects.equals(requiresLogin, that.requiresLogin)
&& Objects.equals(route, that.route)
&& Objects.equals(lazy, that.lazy)
&& Objects.equals(register, that.register)
&& Objects.equals(menu, that.menu)
&& Objects.equals(routeParameters, that.routeParameters);
&& Arrays.equals(rolesAllowed, that.rolesAllowed)
&& Objects.equals(requiresLogin, that.requiresLogin)
&& Objects.equals(route, that.route)
&& Objects.equals(lazy, that.lazy)
&& Objects.equals(register, that.register)
&& Objects.equals(menu, that.menu)
&& Objects.equals(routeParameters, that.routeParameters);
}

@Override
public int hashCode() {
int result = Objects.hash(title, route, lazy, register, menu, routeParameters);
int result = Objects.hash(title, route, lazy, register, menu,
routeParameters);
result = 31 * result + Arrays.hashCode(rolesAllowed);
return result;
}

@Override
public String toString() {
return "AvailableViewInfo{" + "title='" + title
+ '\'' + ", rolesAllowed=" + Arrays.toString(rolesAllowed)
+ ", requiresLogin=" + requiresLogin
+ ", route='" + route + '\''
+ ", lazy=" + lazy
+ ", register=" + register
+ ", menu=" + menu
+ ", routeParameters=" + routeParameters + '}';
return "AvailableViewInfo{" + "title='" + title + '\''
+ ", rolesAllowed=" + Arrays.toString(rolesAllowed)
+ ", requiresLogin=" + requiresLogin + ", route='" + route
+ '\'' + ", lazy=" + lazy + ", register=" + register + ", menu="
+ menu + ", routeParameters=" + routeParameters + '}';
}
}
Loading
Loading