Class NavigationContext
The navigation context is responsible for providing details about the current
navigation, such as the navigation target, the location and the current user,
and to allow NavigationAccessChecker to create a representation of
their decisions.
A NavigationAccessChecker should use allow(),
neutral(), deny(String) and reject(String) methods
to create a result in
NavigationAccessChecker.check(NavigationContext) implementation.
allow()created a result whose meaning is that the access to a view is granted.neutral()means that the access checker is not able to determine if the current navigation should be allowed or not.deny(String)is used to create a response that will prevent the navigation to the target view. The given reason should provide details that will help to debug access control issues.reject(String)denies the access to the target view, but should be used to indicate mistakes in security configuration that do not allow the navigation checker to take a decision; for example, a configuration where the path /my/view is public, but /my/* is protected.
- Since:
- 24.3
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe reason why an access check is run. -
Constructor Summary
ConstructorsConstructorDescriptionNavigationContext(BeforeEnterEvent event, Principal principal, Predicate<String> roleChecker) Create a new navigation context instance based on aBeforeEnterEvent.NavigationContext(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters, Principal principal, Predicate<String> roleChecker, boolean errorHandling) Creates a new navigation context instance for ongoing navigation.NavigationContext(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters, Principal principal, Predicate<String> roleChecker, boolean errorHandling, boolean navigating) Deprecated, for removal: This API element is subject to removal in a future version.NavigationContext(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters, Principal principal, Predicate<String> roleChecker, boolean errorHandling, NavigationContext.Phase phase) Creates a new navigation context instance for the given phase. -
Method Summary
Modifier and TypeMethodDescriptionallow()Create a result instance informing that the navigation to the target view is allowed for the current user.Create a result instance informing that the navigation to the target view is denied for the current user.static NavigationContextforPublicRouteCheck(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters) Creates a navigation context to find out if the target view is public, that is, if an anonymous user has access to it.List<Class<? extends RouterLayout>> Gets the layouts the navigation target is shown in, starting from the layout closest to the navigation target.Gets the requested location.Class<?> Gets the navigation target.Gets the route parameters associated with this event.getPhase()Gets the reason why the access check is run.Gets the principal for the currently logged in user.Gets the router that triggered the navigation change.booleanGets if the current user belongs the specified logical role.booleanGets if current navigation change is related to error handling or if it is a regular navigation to a target view.booleanGets if navigation is ongoing or not, that is, if the phase isNavigationContext.Phase.NAVIGATION.neutral()Create a result instance informing that the checker cannot take a decision based on the given navigation information.Create a result instance informing that the navigation to the target view is denied for the current user because of a misconfiguration or a critical development time error.
-
Constructor Details
-
NavigationContext
@Deprecated(since="25.4", forRemoval=true) public NavigationContext(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters, Principal principal, Predicate<String> roleChecker, boolean errorHandling, boolean navigating) Deprecated, for removal: This API element is subject to removal in a future version.useNavigationContext(Router, Class, Location, RouteParameters, Principal, Predicate, boolean, Phase)and give theNavigationContext.Phaseof the checkCreates a new navigation context instance.- Parameters:
router- the router that triggered the change, not nullnavigationTarget- navigation target class, not nulllocation- the requested location, not nullparameters- route parameters, not nullprincipal- the principal of the userroleChecker- a function that can answer if a user has a given roleerrorHandling- true if the current navigation is related to an error handling phase, false for a regular navigation to a target viewnavigating- true if the navigation is ongoing, false if not (e.g. during access checks outside of navigation)- Since:
- 25.0
-
NavigationContext
public NavigationContext(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters, Principal principal, Predicate<String> roleChecker, boolean errorHandling) Creates a new navigation context instance for ongoing navigation.- Parameters:
router- the router that triggered the change, not nullnavigationTarget- navigation target class, not nulllocation- the requested location, not nullparameters- route parameters, not nullprincipal- the principal of the userroleChecker- a function that can answer if a user has a given roleerrorHandling- true if the current navigation is related to an error handling phase, false for a regular navigation to a target view
-
NavigationContext
public NavigationContext(BeforeEnterEvent event, Principal principal, Predicate<String> roleChecker) Create a new navigation context instance based on aBeforeEnterEvent.- Parameters:
event- the event created before ongoing navigation happens.principal- the principal of the userroleChecker- a function that can answer if a user has a given role
-
-
Method Details
-
forPublicRouteCheck
public static NavigationContext forPublicRouteCheck(Router router, Class<?> navigationTarget, Location location, RouteParameters parameters) Creates a navigation context to find out if the target view is public, that is, if an anonymous user has access to it.The context has no principal and no role is granted. The phase is
NavigationContext.Phase.PUBLIC_ROUTE_CHECK.- Parameters:
router- the router, not nullnavigationTarget- navigation target class, not nulllocation- the requested location, not nullparameters- route parameters, not null- Returns:
- a navigation context for the public route check
- Since:
- 25.4
-
getRouter
Gets the router that triggered the navigation change.- Returns:
- router that triggered the navigation change, not
null
-
getLocation
Gets the requested location.Note that in case of error handling the location still references the initial request.
- Returns:
- the requested location, not
null - See Also:
-
getParameters
Gets the route parameters associated with this event.- Returns:
- route parameters retrieved from the navigation url, not
null
-
getPrincipal
Gets the principal for the currently logged in user.- Returns:
- a representation of the currently logged in user or
nullif no user is currently logged in
-
isErrorHandling
public boolean isErrorHandling()Gets if current navigation change is related to error handling or if it is a regular navigation to a target view.- Returns:
- true if the current navigation is related to an error handling phase, false for a regular navigation to a target view
-
getPhase
Gets the reason why the access check is run.- Returns:
- the phase of the access check, not
null - Since:
- 25.4
-
hasRole
Gets if the current user belongs the specified logical role.- Parameters:
role- a String specifying the name of the role- Returns:
- true if the current user belongs to the given role, false otherwise
-
getLayouts
Gets the layouts the navigation target is shown in, starting from the layout closest to the navigation target.During navigation, these are the layouts the router creates for the matched route or route alias, including an automatic layout. Outside of navigation, they are resolved from the route registry for the location.
- Returns:
- an unmodifiable list of the layouts of the navigation target, or
an empty list if there are none, not
null - Since:
- 25.4
-
allow
Create a result instance informing that the navigation to the target view is allowed for the current user.- Returns:
- a
AccessCheckDecision.ALLOWresult instance.
-
neutral
Create a result instance informing that the checker cannot take a decision based on the given navigation information.- Returns:
- a
AccessCheckDecision.NEUTRALresult instance.
-
deny
Create a result instance informing that the navigation to the target view is denied for the current user.- Parameters:
reason- a message explaining why the navigation has been denied. Useful for debugging purposes.- Returns:
- a
AccessCheckDecision.DENYresult instance.
-
reject
Create a result instance informing that the navigation to the target view is denied for the current user because of a misconfiguration or a critical development time error.- Parameters:
reason- a message explaining why the navigation has been denied and the critical issue encountered. Useful for debugging purposes.- Returns:
- a
AccessCheckDecision.REJECTresult instance.
-
NavigationContext(Router, Class, Location, RouteParameters, Principal, Predicate, boolean, Phase)and give theNavigationContext.Phaseof the check