Skip to content

Commit 7dfa668

Browse files
authored
fix: Make deny layout more descriptive. (#22839)
Have the deny and log be more descriptive on which view and which layout denies navigation due to access rules. Closes #21956
1 parent 1fea417 commit 7dfa668

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

flow-server/src/main/java/com/vaadin/flow/server/auth/AnnotatedViewAccessChecker.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ public AccessCheckResult check(NavigationContext context) {
8484
boolean hasAccess = accessAnnotationChecker.hasAccess(layout,
8585
context.getPrincipal(), context::hasRole);
8686
if (!hasAccess) {
87-
logDeniedByLayoutAccessRules(context, targetView);
88-
return context.deny("Denied access to view due to layout '"
89-
+ targetView.getSimpleName() + "' access rules."
87+
logDeniedByLayoutAccessRules(context, layout);
88+
return context.deny("Denied access to view '"
89+
+ targetView.getSimpleName() + "' due to layout '"
90+
+ layout.getSimpleName() + "' access rules. "
9091
+ "Consider adding one of the following annotations "
9192
+ "to make the layout accessible: @AnonymousAllowed, "
92-
+ "@PermitAll, @RolesAllowed.");
93+
+ "@PermitAll, or @RolesAllowed.");
9394
}
9495
}
9596
} else {
@@ -106,14 +107,14 @@ public AccessCheckResult check(NavigationContext context) {
106107
parent, context.getPrincipal(), context::hasRole);
107108
if (!hasAccess) {
108109
logDeniedByLayoutAccessRules(context, parent,
109-
"Denied access to view due to parent layout '{}' access rules");
110-
return context.deny(
111-
"Denied access to view due to parent layout '"
112-
+ targetView.getSimpleName()
113-
+ "' access rules."
114-
+ "Consider adding one of the following annotations "
115-
+ "to make the parent layouts accessible: @AnonymousAllowed, "
116-
+ "@PermitAll, @RolesAllowed.");
110+
"Denied access to view '{}' due to parent layout '{}' access rules");
111+
return context.deny("Denied access to view '"
112+
+ targetView.getSimpleName()
113+
+ "' due to parent layout '"
114+
+ parent.getSimpleName() + "' access rules. "
115+
+ "Consider adding one of the following annotations "
116+
+ "to make the parent layout accessible: @AnonymousAllowed, "
117+
+ "@PermitAll, or @RolesAllowed.");
117118
}
118119
}
119120
}
@@ -139,11 +140,13 @@ public AccessCheckResult check(NavigationContext context) {
139140
(Class<? extends Component>) targetView)
140141
.isEmpty()) {
141142
logDeniedByLayoutAccessRules(context, targetView);
142-
denyReason = "Denied access to view due to layout '"
143-
+ targetView.getSimpleName() + "' access rules."
143+
denyReason = "Denied access to view '"
144+
+ context.getNavigationTarget().getSimpleName()
145+
+ "' due to layout '" + targetView.getSimpleName()
146+
+ "' access rules. "
144147
+ "Consider adding one of the following annotations "
145148
+ "to make the layout accessible: @AnonymousAllowed, "
146-
+ "@PermitAll, @RolesAllowed.";
149+
+ "@PermitAll, or @RolesAllowed.";
147150
}
148151
} else {
149152
denyReason = "Access is denied by annotations on the view.";
@@ -152,17 +155,20 @@ public AccessCheckResult check(NavigationContext context) {
152155
}
153156

154157
private void logDeniedByLayoutAccessRules(NavigationContext context,
155-
Class<?> viewClass) {
156-
logDeniedByLayoutAccessRules(context, viewClass,
157-
"Denied access to view due to layout '{}' access rules");
158+
Class<?> layoutClass) {
159+
String msg = "Denied access to view '{}' due to layout '{}' access rules. "
160+
+ "Consider adding @AnonymousAllowed, @PermitAll, or @RolesAllowed to the layout class.";
161+
logDeniedByLayoutAccessRules(context, layoutClass, msg);
158162
}
159163

160164
private void logDeniedByLayoutAccessRules(NavigationContext context,
161-
Class<?> viewClass, String msg) {
165+
Class<?> layoutClass, String msg) {
162166
if (context.isNavigating()) {
163-
LOGGER.warn(msg, viewClass.getSimpleName());
167+
LOGGER.warn(msg, context.getNavigationTarget().getSimpleName(),
168+
layoutClass.getSimpleName());
164169
} else {
165-
LOGGER.trace(msg, viewClass.getSimpleName());
170+
LOGGER.trace(msg, context.getNavigationTarget().getSimpleName(),
171+
layoutClass.getSimpleName());
166172
}
167173
}
168174

0 commit comments

Comments
 (0)