001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.auth.permissions; 020 021import java.io.Serializable; 022import java.security.Permission; 023import java.security.PermissionCollection; 024 025/** 026 * <p> 027 * Permission to perform all operations on a given wiki. 028 * </p> 029 * @since 2.3.80 030 */ 031public final class AllPermission extends Permission implements Serializable 032{ 033 private static final long serialVersionUID = 1L; 034 035 private static final String WILDCARD = "*"; 036 037 private final String m_wiki; 038 039 /** For serialization purposes. */ 040 protected AllPermission() 041 { 042 this(null); 043 } 044 045 /** 046 * Creates a new AllPermission for the given wikis. 047 * 048 * @param wiki the wiki to which the permission should apply. If null, will 049 * apply to all wikis. 050 */ 051 public AllPermission( String wiki ) 052 { 053 super( wiki ); 054 m_wiki = ( wiki == null ) ? WILDCARD : wiki; 055 } 056 057 /** 058 * Two AllPermission objects are considered equal if their wikis are equal. 059 * @see java.lang.Object#equals(java.lang.Object) 060 * 061 * @return {@inheritDoc} 062 * @param obj {@inheritDoc} 063 */ 064 public boolean equals( Object obj ) 065 { 066 if ( !( obj instanceof AllPermission ) ) 067 { 068 return false; 069 } 070 AllPermission p = (AllPermission) obj; 071 return p.m_wiki != null && p.m_wiki.equals( m_wiki ); 072 } 073 074 /** 075 * No-op; always returns <code>null</code> 076 * @see java.security.Permission#getActions() 077 * 078 * @return Always null. 079 */ 080 public String getActions() 081 { 082 return null; 083 } 084 085 /** 086 * Returns the name of the wiki containing the page represented by this 087 * permission; may return the wildcard string. 088 * @return The wiki 089 */ 090 public String getWiki() 091 { 092 return m_wiki; 093 } 094 095 /** 096 * Returns the hash code for this WikiPermission. 097 * @see java.lang.Object#hashCode() 098 * 099 * @return {@inheritDoc} 100 */ 101 public int hashCode() 102 { 103 return m_wiki.hashCode(); 104 } 105 106 /** 107 * WikiPermission can only imply other WikiPermissions; no other permission 108 * types are implied. One WikiPermission implies another if all of the other 109 * WikiPermission's actions are equal to, or a subset of, those for this 110 * permission. 111 * @param permission the permission which may (or may not) be implied by 112 * this instance 113 * @return <code>true</code> if the permission is implied, 114 * <code>false</code> otherwise 115 * @see java.security.Permission#implies(java.security.Permission) 116 */ 117 public boolean implies( Permission permission ) 118 { 119 // Permission must be a JSPWiki permission, PagePermission or AllPermission 120 if ( !isJSPWikiPermission( permission ) ) 121 { 122 return false; 123 } 124 String wiki = null; 125 if ( permission instanceof AllPermission ) 126 { 127 wiki = ( (AllPermission) permission ).getWiki(); 128 } 129 else if ( permission instanceof PagePermission ) 130 { 131 wiki = ( (PagePermission) permission ).getWiki(); 132 } 133 if ( permission instanceof WikiPermission ) 134 { 135 wiki = ( (WikiPermission) permission ).getWiki(); 136 } 137 if ( permission instanceof GroupPermission ) 138 { 139 wiki = ( (GroupPermission) permission ).getWiki(); 140 } 141 142 // If the wiki is implied, it's allowed 143 return PagePermission.isSubset( m_wiki, wiki ); 144 } 145 146 /** 147 * Returns a new {@link AllPermissionCollection}. 148 * @see java.security.Permission#newPermissionCollection() 149 * 150 * @return {@inheritDoc} 151 */ 152 public PermissionCollection newPermissionCollection() 153 { 154 return new AllPermissionCollection(); 155 } 156 157 /** 158 * Prints a human-readable representation of this permission. 159 * @see java.lang.Object#toString() 160 * @return {@inheritDoc} 161 */ 162 public String toString() 163 { 164 return "(\"" + this.getClass().getName() + "\",\"" + m_wiki + "\")"; 165 } 166 167 /** 168 * Checks if the given permission is one of ours. 169 * 170 * @param permission Permission to check 171 * @return true, if the permission is a JSPWiki permission; false otherwise. 172 */ 173 protected static boolean isJSPWikiPermission( Permission permission ) 174 { 175 return permission instanceof WikiPermission || 176 permission instanceof PagePermission || 177 permission instanceof GroupPermission || 178 permission instanceof AllPermission; 179 } 180 181}