001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.authorization.strategies.page;
018
019import org.apache.wicket.Page;
020import org.apache.wicket.authorization.IAuthorizationStrategy;
021import org.apache.wicket.request.component.IRequestableComponent;
022
023/**
024 * An abstract base class for implementing simple authorization of Pages. Users should override
025 * {@link #isPageAuthorized(Class)}, which gets called for Page classes when they are being
026 * constructed.
027 * 
028 * @author Jonathan Locke
029 * @author Eelco Hillenius
030 */
031public abstract class AbstractPageAuthorizationStrategy extends IAuthorizationStrategy.AllowAllAuthorizationStrategy
032{
033        /**
034         * @see org.apache.wicket.authorization.IAuthorizationStrategy#isInstantiationAuthorized(java.lang.Class)
035         */
036        @Override
037        @SuppressWarnings("unchecked")
038        public final <T extends IRequestableComponent> boolean isInstantiationAuthorized(
039                final Class<T> componentClass)
040        {
041                if (instanceOf(componentClass, Page.class))
042                {
043                        return isPageAuthorized((Class<? extends Page>)componentClass);
044                }
045                return true;
046        }
047
048        /**
049         * Works like instanceof operator where instanceOf(a, b) is the runtime equivalent of (a
050         * instanceof b).
051         * 
052         * @param type
053         *            The type to check
054         * @param superType
055         *            The interface or superclass that the type needs to implement or extend
056         * @return True if the type is an instance of the superType
057         */
058        protected boolean instanceOf(final Class<?> type, final Class<?> superType)
059        {
060                return superType != null && superType.isAssignableFrom(type);
061        }
062
063        /**
064         * Whether to page may be created. Returns true by default.
065         * 
066         * @param <T>
067         *            the type of the page
068         * 
069         * @param pageClass
070         *            The Page class
071         * @return True if to page may be created
072         */
073        protected <T extends Page> boolean isPageAuthorized(Class<T> pageClass)
074        {
075                return true;
076        }
077}