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.authroles.authorization.strategies.role; 018 019import org.apache.wicket.authorization.IAuthorizationStrategy; 020import org.apache.wicket.util.lang.Args; 021 022/** 023 * Base strategy that uses an instance of 024 * {@link IRoleCheckingStrategy}. 025 * 026 * @author Eelco Hillenius 027 */ 028public abstract class AbstractRoleAuthorizationStrategy extends IAuthorizationStrategy.AllowAllAuthorizationStrategy 029{ 030 /** Role checking strategy. */ 031 private final IRoleCheckingStrategy roleCheckingStrategy; 032 033 /** 034 * Construct. 035 * 036 * @param roleCheckingStrategy 037 * the authorizer delegate 038 */ 039 public AbstractRoleAuthorizationStrategy(IRoleCheckingStrategy roleCheckingStrategy) 040 { 041 Args.notNull(roleCheckingStrategy, "roleCheckingStrategy"); 042 this.roleCheckingStrategy = roleCheckingStrategy; 043 } 044 045 /** 046 * Gets whether any of the given roles applies to the authorizer. 047 * 048 * @param roles 049 * the roles 050 * @return whether any of the given roles applies to the authorizer 051 */ 052 protected final boolean hasAny(Roles roles) 053 { 054 if (roles.isEmpty()) 055 { 056 return true; 057 } 058 else 059 { 060 return roleCheckingStrategy.hasAnyRole(roles); 061 } 062 } 063 064 /** 065 * Conducts a check to see if the roles object is empty. Since the roles object does not contain 066 * any null values and will always hold an empty string, an extra test is required beyond 067 * roles.isEmpty(). 068 * 069 * @param roles 070 * the Roles object to test 071 * @return true if the object holds no real roles 072 */ 073 protected final boolean isEmpty(Roles roles) 074 { 075 if (roles.isEmpty()) 076 { 077 return true; 078 } 079 080 return false; 081 } 082}