Repository: wicket Updated Branches: refs/heads/wicket-7.x 6f728f824 -> 14b922cde
Page instantiation protection by combined ruleset. There is currently no way to annotate a page with instantiation rules on multiple roles. It is possible to use an AuthorizeInstantiation. But this can only roles using an OR ruleset. With this annotation in use we are able to combine multiple OR rulesets in an AND relation. Example: A page can be instantiate only by users with ADMIN and DATABASE role. Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/14b922cd Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/14b922cd Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/14b922cd Branch: refs/heads/wicket-7.x Commit: 14b922cde4f62a73e7006373b4f43b9af727bf41 Parents: 6f728f8 Author: René Dieckmann <[email protected]> Authored: Thu Oct 13 14:23:40 2016 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Mon Mar 20 22:35:54 2017 +0100 ---------------------------------------------------------------------- .../AnnotationsRoleAuthorizationStrategy.java | 41 ++++++++++++- .../annotations/AuthorizeInstantiations.java | 64 ++++++++++++++++++++ ...nnotationsRoleAuthorizationStrategyTest.java | 30 +++++++++ 3 files changed, 133 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/14b922cd/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategy.java ---------------------------------------------------------------------- diff --git a/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategy.java b/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategy.java index 3a72d38..ff7b9dd 100644 --- a/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategy.java +++ b/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategy.java @@ -58,7 +58,7 @@ public class AnnotationsRoleAuthorizationStrategy extends AbstractRoleAuthorizat final AuthorizeInstantiation classAnnotation = componentClass.getAnnotation(AuthorizeInstantiation.class); if (classAnnotation != null) { - authorized = hasAny(new Roles(classAnnotation.value())); + authorized = check(classAnnotation); } else { @@ -69,7 +69,22 @@ public class AnnotationsRoleAuthorizationStrategy extends AbstractRoleAuthorizat final AuthorizeInstantiation packageAnnotation = componentPackage.getAnnotation(AuthorizeInstantiation.class); if (packageAnnotation != null) { - authorized = hasAny(new Roles(packageAnnotation.value())); + authorized = check(packageAnnotation); + } + } + } + + // Check for multiple instantiations + final AuthorizeInstantiations authorizeInstantiationsAnnotation = componentClass + .getAnnotation(AuthorizeInstantiations.class); + if (authorizeInstantiationsAnnotation != null) + { + for (final AuthorizeInstantiation authorizeInstantiationAnnotation : authorizeInstantiationsAnnotation + .ruleset()) + { + if (!check(authorizeInstantiationAnnotation)) + { + authorized = false; } } } @@ -78,6 +93,28 @@ public class AnnotationsRoleAuthorizationStrategy extends AbstractRoleAuthorizat } /** + * Check if annotated instantiation is allowed. + * + * @param authorizeInstantiationAnnotation + * The annotations information + * @return False if the instantiation is not authorized + */ + private <T extends IRequestableComponent> boolean check( + final AuthorizeInstantiation authorizeInstantiationAnnotation) + { + // We are authorized unless we are found not to be + boolean authorized = true; + + // Check class annotation first because it is more specific than package annotation + if (authorizeInstantiationAnnotation != null) + { + authorized = hasAny(new Roles(authorizeInstantiationAnnotation.value())); + } + + return authorized; + } + + /** * @see org.apache.wicket.authorization.IAuthorizationStrategy#isActionAuthorized(org.apache.wicket.Component, * org.apache.wicket.authorization.Action) */ http://git-wip-us.apache.org/repos/asf/wicket/blob/14b922cd/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AuthorizeInstantiations.java ---------------------------------------------------------------------- diff --git a/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AuthorizeInstantiations.java b/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AuthorizeInstantiations.java new file mode 100644 index 0000000..de708a7 --- /dev/null +++ b/wicket-auth-roles/src/main/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AuthorizeInstantiations.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.wicket.authroles.authorization.strategies.role.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Groups a set (technically an array) of {@link AuthorizeInstantiation}s for page authorization. + * + * This offers the ability to instantiate a page based on combined permissions / roles required. It + * represents an AND relationship between the included permissions / roles. + * + * This can be used like this: + * + * <pre> + * @AuthorizeInstantiations(ruleset = { @AuthorizeInstantiation("ADMIN"), + * @AuthorizeInstantiation("MANAGER") }) + * public class ForAdministrativeManagers extends WebPage + * { + * public ForAdministrativeManagers() + * { + * super(); + * } + * } + * </pre> + * + * @see org.apache.wicket.authorization.IAuthorizationStrategy + * @see AnnotationsRoleAuthorizationStrategy + * @see AuthorizeInstantiation + * @see AuthorizeInstantiations + * @author René Dieckmann ([email protected]) + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE }) +@Documented +@Inherited +public @interface AuthorizeInstantiations { + + /** + * The combined ruleset. + * + * @return the combined ruleset + */ + AuthorizeInstantiation[] ruleset(); +} http://git-wip-us.apache.org/repos/asf/wicket/blob/14b922cd/wicket-auth-roles/src/test/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategyTest.java ---------------------------------------------------------------------- diff --git a/wicket-auth-roles/src/test/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategyTest.java b/wicket-auth-roles/src/test/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategyTest.java index 90b3d66..9ef17ce 100644 --- a/wicket-auth-roles/src/test/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategyTest.java +++ b/wicket-auth-roles/src/test/java/org/apache/wicket/authroles/authorization/strategies/role/annotations/AnnotationsRoleAuthorizationStrategyTest.java @@ -135,6 +135,22 @@ public class AnnotationsRoleAuthorizationStrategyTest } @Test + public void allowsInstantiationWithAllRequiredRoles() throws Exception + { + AnnotationsRoleAuthorizationStrategy strategy = new AnnotationsRoleAuthorizationStrategy( + roles("role1", "role2")); + assertTrue(strategy.isInstantiationAuthorized(TestComponent_Roleset_Instantiate.class)); + } + + @Test + public void deniesInstantiationWithoutAllRequiredRoles() throws Exception + { + AnnotationsRoleAuthorizationStrategy strategy = new AnnotationsRoleAuthorizationStrategy( + roles("role2")); + assertFalse(strategy.isInstantiationAuthorized(TestComponent_Roleset_Instantiate.class)); + } + + @Test public void allowsResourceWithRequiredRole() throws Exception { AnnotationsRoleAuthorizationStrategy strategy = new AnnotationsRoleAuthorizationStrategy( @@ -179,6 +195,20 @@ public class AnnotationsRoleAuthorizationStrategyTest } } + + @AuthorizeInstantiations(ruleset = { @AuthorizeInstantiation({ "role1" }), + @AuthorizeInstantiation({ "role2" }) }) + private static class TestComponent_Roleset_Instantiate extends WebComponent + { + + private static final long serialVersionUID = 1L; + + private TestComponent_Roleset_Instantiate() + { + super("notUsed"); + } + + } @AuthorizeAction(action = "RENDER", roles = {"role1"}, deny = {"role3"}) private static class TestComponent_Render extends WebComponent
