/* * Copyright 2005 the original author or authors. * * Licensed 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.wamblee.photos.authorizationrules; import org.wamblee.security.authorization.AllOperation; import org.wamblee.security.authorization.AuthorizationResult; import org.wamblee.security.authorization.IsaOperationCondition; import org.wamblee.security.authorization.RegexpPathCondition; import org.wamblee.security.authorization.UrlAuthorizationRule; import org.wamblee.security.authorization.UserCondition; import org.wamblee.security.tapestry.ProtectedPage; /** * AUthorization rule for pages. */ public class PageAuthorizationRule extends UrlAuthorizationRule { /** * Type-safe construction of page authorization rule. * @param aResult Result. * @param aUserCondition User condition. * @param aPageList A list of page names. */ public PageAuthorizationRule(AuthorizationResult aResult, UserCondition aUserCondition, String[] aPageList) { super(aResult, aUserCondition, new RegexpPathCondition(getPageRegex(aPageList)), ProtectedPage.class, new IsaOperationCondition(AllOperation.class)); } /** * Converts a list of page names into a regular expression for the pages. * @param aPageList List of pages. * @return Regexp matching any of the given pagenames. */ private static String getPageRegex(String[] aPageList) { String result = ""; for (int i = 0; i < aPageList.length; i++) { if ( result.length() == 0 ) { result = "/" + aPageList[i]; } else { result = result + "|/" + aPageList[i]; } } return result; } /** * Weakly typed constructor, useful for configuration. * * @param aResult Result for this rule. * @param aUserCondition User condition. * @param aPageList List of pages this rule is for. */ public PageAuthorizationRule( String aResult, UserCondition aUserCondition, String[] aPageList) { this( AuthorizationResult.valueOf(aResult), aUserCondition, aPageList); } /** * Constructor for OR mapping. */ protected PageAuthorizationRule() { super(); } /* (non-Javadoc) * @see org.wamblee.security.authorization.UrlAuthorizationRule#getResourcePath(java.lang.Object) */ @Override protected String getResourcePath(Object aResource) { ProtectedPage page = (ProtectedPage)aResource; return "/" + page.getPageName(); } }