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.shiro.authz.permission; 020 021import org.apache.shiro.authz.Permission; 022import org.apache.shiro.util.CollectionUtils; 023import org.apache.shiro.lang.util.StringUtils; 024 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.Iterator; 028import java.util.List; 029import java.util.Set; 030 031/** 032 * A <code>WildcardPermission</code> is a very flexible permission construct supporting multiple levels of 033 * permission matching. However, most people will probably follow some standard conventions as explained below. 034 * <p/> 035 * <h3>Simple Usage</h3> 036 * <p/> 037 * In the simplest form, <code>WildcardPermission</code> can be used as a simple permission string. You could grant a 038 * user an "editNewsletter" permission and then check to see if the user has the editNewsletter 039 * permission by calling 040 * <p/> 041 * <code>subject.isPermitted("editNewsletter")</code> 042 * <p/> 043 * This is (mostly) equivalent to 044 * <p/> 045 * <code>subject.isPermitted( new WildcardPermission("editNewsletter") )</code> 046 * <p/> 047 * but more on that later. 048 * <p/> 049 * The simple permission string may work for simple applications, but it requires you to have permissions like 050 * <code>"viewNewsletter"</code>, <code>"deleteNewsletter"</code>, 051 * <code>"createNewsletter"</code>, etc. You can also grant a user <code>"*"</code> permissions 052 * using the wildcard character (giving this class its name), which means they have <em>all</em> permissions. But 053 * using this approach there's no way to just say a user has "all newsletter permissions". 054 * <p/> 055 * For this reason, <code>WildcardPermission</code> supports multiple <em>levels</em> of permissioning. 056 * <p/> 057 * <h3>Multiple Levels</h3> 058 * <p/> 059 * WildcardPermission</code> also supports the concept of multiple <em>levels</em>. For example, you could 060 * restructure the previous simple example by granting a user the permission <code>"newsletter:edit"</code>. 061 * The colon in this example is a special character used by the <code>WildcardPermission</code> that delimits the 062 * next token in the permission. 063 * <p/> 064 * In this example, the first token is the <em>domain</em> that is being operated on 065 * and the second token is the <em>action</em> being performed. Each level can contain multiple values. So you 066 * could simply grant a user the permission <code>"newsletter:view,edit,create"</code> which gives them 067 * access to perform <code>view</code>, <code>edit</code>, and <code>create</code> actions in the <code>newsletter</code> 068 * <em>domain</em>. Then you could check to see if the user has the <code>"newsletter:create"</code> 069 * permission by calling 070 * <p/> 071 * <code>subject.isPermitted("newsletter:create")</code> 072 * <p/> 073 * (which would return true). 074 * <p/> 075 * In addition to granting multiple permissions via a single string, you can grant all permission for a particular 076 * level. So if you wanted to grant a user all actions in the <code>newsletter</code> domain, you could simply give 077 * them <code>"newsletter:*"</code>. Now, any permission check for <code>"newsletter:XXX"</code> 078 * will return <code>true</code>. It is also possible to use the wildcard token at the domain level (or both): so you 079 * could grant a user the <code>"view"</code> action across all domains <code>"*:view"</code>. 080 * <p/> 081 * <h3>Instance-level Access Control</h3> 082 * <p/> 083 * Another common usage of the <code>WildcardPermission</code> is to model instance-level Access Control Lists. 084 * In this scenario you use three tokens - the first is the <em>domain</em>, the second is the <em>action</em>, and 085 * the third is the <em>instance</em> you are acting on. 086 * <p/> 087 * So for example you could grant a user <code>"newsletter:edit:12,13,18"</code>. In this example, assume 088 * that the third token is the system's ID of the newsletter. That would allow the user to edit newsletters 089 * <code>12</code>, <code>13</code>, and <code>18</code>. This is an extremely powerful way to express permissions, 090 * since you can now say things like <code>"newsletter:*:13"</code> (grant a user all actions for newsletter 091 * <code>13</code>), <code>"newsletter:view,create,edit:*"</code> (allow the user to 092 * <code>view</code>, <code>create</code>, or <code>edit</code> <em>any</em> newsletter), or 093 * <code>"newsletter:*:*</code> (allow the user to perform <em>any</em> action on <em>any</em> newsletter). 094 * <p/> 095 * To perform checks against these instance-level permissions, the application should include the instance ID in the 096 * permission check like so: 097 * <p/> 098 * <code>subject.isPermitted( "newsletter:edit:13" )</code> 099 * <p/> 100 * There is no limit to the number of tokens that can be used, so it is up to your imagination in terms of ways that 101 * this could be used in your application. However, the Shiro team likes to standardize some common usages shown 102 * above to help people get started and provide consistency in the Shiro community. 103 * 104 * @since 0.9 105 */ 106public class WildcardPermission implements Permission, Serializable { 107 108 /*-------------------------------------------- 109 | C O N S T A N T S | 110 ============================================*/ 111 protected static final String WILDCARD_TOKEN = "*"; 112 protected static final String PART_DIVIDER_TOKEN = ":"; 113 protected static final String SUBPART_DIVIDER_TOKEN = ","; 114 protected static final boolean DEFAULT_CASE_SENSITIVE = false; 115 116 /*-------------------------------------------- 117 | I N S T A N C E V A R I A B L E S | 118 ============================================*/ 119 private List<Set<String>> parts; 120 121 /*-------------------------------------------- 122 | C O N S T R U C T O R S | 123 ============================================*/ 124 125 /** 126 * Default no-arg constructor for subclasses only - end-user developers instantiating Permission instances must 127 * provide a wildcard string at a minimum, since Permission instances are immutable once instantiated. 128 * <p/> 129 * Note that the WildcardPermission class is very robust and typically subclasses are not necessary unless you 130 * wish to create type-safe Permission objects that would be used in your application, such as perhaps a 131 * {@code UserPermission}, {@code SystemPermission}, {@code PrinterPermission}, etc. If you want such type-safe 132 * permission usage, consider subclassing the {@link DomainPermission DomainPermission} class for your needs. 133 */ 134 protected WildcardPermission() { 135 } 136 137 public WildcardPermission(String wildcardString) { 138 this(wildcardString, DEFAULT_CASE_SENSITIVE); 139 } 140 141 public WildcardPermission(String wildcardString, boolean caseSensitive) { 142 setParts(wildcardString, caseSensitive); 143 } 144 145 protected void setParts(String wildcardString) { 146 setParts(wildcardString, DEFAULT_CASE_SENSITIVE); 147 } 148 149 protected void setParts(String wildcardString, boolean caseSensitive) { 150 wildcardString = StringUtils.clean(wildcardString); 151 152 if (wildcardString == null || wildcardString.isEmpty()) { 153 throw new IllegalArgumentException("Wildcard string cannot be null or empty." 154 + "Make sure permission strings are properly formatted."); 155 } 156 157 if (!caseSensitive) { 158 wildcardString = wildcardString.toLowerCase(); 159 } 160 161 List<String> parts = CollectionUtils.asList(wildcardString.split(PART_DIVIDER_TOKEN)); 162 163 this.parts = new ArrayList<Set<String>>(); 164 for (String part : parts) { 165 Set<String> subparts = CollectionUtils.asSet(part.split(SUBPART_DIVIDER_TOKEN)); 166 167 if (subparts.isEmpty()) { 168 throw new IllegalArgumentException("Wildcard string cannot contain parts with only dividers." 169 + "Make sure permission strings are properly formatted."); 170 } 171 this.parts.add(subparts); 172 } 173 174 if (this.parts.isEmpty()) { 175 throw new IllegalArgumentException("Wildcard string cannot contain only dividers." 176 + "Make sure permission strings are properly formatted."); 177 } 178 } 179 180 /*-------------------------------------------- 181 | A C C E S S O R S / M O D I F I E R S | 182 ============================================*/ 183 protected List<Set<String>> getParts() { 184 return this.parts; 185 } 186 187 /** 188 * Sets the pre-split String parts of this <code>WildcardPermission</code>. 189 * 190 * @param parts pre-split String parts. 191 * @since 1.3.0 192 */ 193 protected void setParts(List<Set<String>> parts) { 194 this.parts = parts; 195 } 196 197 /*-------------------------------------------- 198 | M E T H O D S | 199 ============================================*/ 200 201 public boolean implies(Permission p) { 202 // By default only supports comparisons with other WildcardPermissions 203 if (!(p instanceof WildcardPermission)) { 204 return false; 205 } 206 207 WildcardPermission wp = (WildcardPermission) p; 208 209 List<Set<String>> otherParts = wp.getParts(); 210 211 int i = 0; 212 for (Set<String> otherPart : otherParts) { 213 // If this permission has less parts than the other permission, everything after the number of parts contained 214 // in this permission is automatically implied, so return true 215 if (getParts().size() - 1 < i) { 216 return true; 217 } else { 218 Set<String> part = getParts().get(i); 219 if (!part.contains(WILDCARD_TOKEN) && !part.containsAll(otherPart)) { 220 return false; 221 } 222 i++; 223 } 224 } 225 226 // If this permission has more parts than the other parts, only imply it if all of the other parts are wildcards 227 for (; i < getParts().size(); i++) { 228 Set<String> part = getParts().get(i); 229 if (!part.contains(WILDCARD_TOKEN)) { 230 return false; 231 } 232 } 233 234 return true; 235 } 236 237 public String toString() { 238 StringBuilder buffer = new StringBuilder(); 239 for (Set<String> part : parts) { 240 if (buffer.length() > 0) { 241 buffer.append(PART_DIVIDER_TOKEN); 242 } 243 Iterator<String> partIt = part.iterator(); 244 while (partIt.hasNext()) { 245 buffer.append(partIt.next()); 246 if (partIt.hasNext()) { 247 buffer.append(SUBPART_DIVIDER_TOKEN); 248 } 249 } 250 } 251 return buffer.toString(); 252 } 253 254 public boolean equals(Object o) { 255 if (o instanceof WildcardPermission) { 256 WildcardPermission wp = (WildcardPermission) o; 257 return parts.equals(wp.parts); 258 } 259 return false; 260 } 261 262 public int hashCode() { 263 return parts.hashCode(); 264 } 265 266}