| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.acegisecurity.acl.basic; |
| 17 |
|
|
| 18 |
|
import org.apache.commons.logging.Log; |
| 19 |
|
import org.apache.commons.logging.LogFactory; |
| 20 |
|
|
| 21 |
|
import org.springframework.util.Assert; |
| 22 |
|
|
| 23 |
|
import java.util.Arrays; |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
public abstract class AbstractBasicAclEntry implements BasicAclEntry { |
| 33 |
|
|
| 34 |
|
|
| 35 |
2 |
private static final Log logger = LogFactory.getLog(AbstractBasicAclEntry.class); |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
private AclObjectIdentity aclObjectIdentity; |
| 40 |
|
private AclObjectIdentity aclObjectParentIdentity; |
| 41 |
|
private Object recipient; |
| 42 |
|
private int[] validPermissions; |
| 43 |
196 |
private int mask = 0; |
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
public AbstractBasicAclEntry(Object recipient, AclObjectIdentity aclObjectIdentity, |
| 48 |
166 |
AclObjectIdentity aclObjectParentIdentity, int mask) { |
| 49 |
166 |
Assert.notNull(recipient, "recipient cannot be null"); |
| 50 |
|
|
| 51 |
165 |
Assert.notNull(aclObjectIdentity, "aclObjectIdentity cannot be null"); |
| 52 |
|
|
| 53 |
164 |
validPermissions = getValidPermissions(); |
| 54 |
164 |
Arrays.sort(validPermissions); |
| 55 |
|
|
| 56 |
1804 |
for (int i = 0; i < validPermissions.length; i++) { |
| 57 |
1640 |
if (logger.isDebugEnabled()) { |
| 58 |
0 |
logger.debug("Valid permission: " + printPermissionsBlock(validPermissions[i]) + " " |
| 59 |
|
+ printBinary(validPermissions[i]) + " (" + validPermissions[i] + ")"); |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
|
|
| 63 |
164 |
this.recipient = recipient; |
| 64 |
164 |
this.aclObjectIdentity = aclObjectIdentity; |
| 65 |
164 |
this.aclObjectParentIdentity = aclObjectParentIdentity; |
| 66 |
164 |
this.mask = mask; |
| 67 |
164 |
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
30 |
protected AbstractBasicAclEntry() { |
| 73 |
30 |
validPermissions = getValidPermissions(); |
| 74 |
30 |
Arrays.sort(validPermissions); |
| 75 |
30 |
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
public int addPermission(int permissionToAdd) { |
| 80 |
3 |
return addPermissions(new int[] {permissionToAdd}); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
public int addPermissions(int[] permissionsToAdd) { |
| 84 |
7 |
if (logger.isDebugEnabled()) { |
| 85 |
0 |
logger.debug("BEFORE Permissions: " + printPermissionsBlock(mask) + " " + printBinary(mask) + " (" + mask |
| 86 |
|
+ ")"); |
| 87 |
|
} |
| 88 |
|
|
| 89 |
21 |
for (int i = 0; i < permissionsToAdd.length; i++) { |
| 90 |
14 |
if (logger.isDebugEnabled()) { |
| 91 |
0 |
logger.debug("Add permission: " + printPermissionsBlock(permissionsToAdd[i]) + " " |
| 92 |
|
+ printBinary(permissionsToAdd[i]) + " (" + permissionsToAdd[i] + ")"); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
14 |
this.mask |= permissionsToAdd[i]; |
| 96 |
|
} |
| 97 |
|
|
| 98 |
7 |
if (Arrays.binarySearch(validPermissions, this.mask) < 0) { |
| 99 |
1 |
throw new IllegalArgumentException("Resulting permission set will be invalid."); |
| 100 |
|
} else { |
| 101 |
6 |
if (logger.isDebugEnabled()) { |
| 102 |
0 |
logger.debug("AFTER Permissions: " + printPermissionsBlock(mask) + " " + printBinary(mask) + " (" |
| 103 |
|
+ mask + ")"); |
| 104 |
|
} |
| 105 |
|
|
| 106 |
6 |
return this.mask; |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
public int deletePermission(int permissionToDelete) { |
| 111 |
3 |
return deletePermissions(new int[] {permissionToDelete}); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
public int deletePermissions(int[] permissionsToDelete) { |
| 115 |
4 |
if (logger.isDebugEnabled()) { |
| 116 |
0 |
logger.debug("BEFORE Permissions: " + printPermissionsBlock(mask) + " " + printBinary(mask) + " (" + mask |
| 117 |
|
+ ")"); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
9 |
for (int i = 0; i < permissionsToDelete.length; i++) { |
| 121 |
5 |
if (logger.isDebugEnabled()) { |
| 122 |
0 |
logger.debug("Delete permission: " + printPermissionsBlock(permissionsToDelete[i]) + " " |
| 123 |
|
+ printBinary(permissionsToDelete[i]) + " (" + permissionsToDelete[i] + ")"); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
5 |
this.mask &= ~permissionsToDelete[i]; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
4 |
if (Arrays.binarySearch(validPermissions, this.mask) < 0) { |
| 130 |
1 |
throw new IllegalArgumentException("Resulting permission set will be invalid."); |
| 131 |
|
} else { |
| 132 |
3 |
if (logger.isDebugEnabled()) { |
| 133 |
0 |
logger.debug("AFTER Permissions: " + printPermissionsBlock(mask) + " " + printBinary(mask) + " (" |
| 134 |
|
+ mask + ")"); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
3 |
return this.mask; |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
public AclObjectIdentity getAclObjectIdentity() { |
| 142 |
39 |
return this.aclObjectIdentity; |
| 143 |
|
} |
| 144 |
|
|
| 145 |
|
public AclObjectIdentity getAclObjectParentIdentity() { |
| 146 |
26 |
return this.aclObjectParentIdentity; |
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
public int getMask() { |
| 150 |
21 |
return this.mask; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
public Object getRecipient() { |
| 154 |
137 |
return this.recipient; |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
|
| 158 |
|
|
| 159 |
|
|
| 160 |
|
|
| 161 |
|
|
| 162 |
|
|
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
public abstract int[] getValidPermissions(); |
| 174 |
|
|
| 175 |
|
public boolean isPermitted(int permissionToCheck) { |
| 176 |
40 |
return isPermitted(this.mask, permissionToCheck); |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
protected boolean isPermitted(int maskToCheck, int permissionToCheck) { |
| 180 |
70 |
return ((maskToCheck & permissionToCheck) == permissionToCheck); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
private String printBinary(int i) { |
| 184 |
1 |
String s = Integer.toString(i, 2); |
| 185 |
|
|
| 186 |
1 |
String pattern = "................................"; |
| 187 |
|
|
| 188 |
1 |
String temp1 = pattern.substring(0, pattern.length() - s.length()); |
| 189 |
|
|
| 190 |
1 |
String temp2 = temp1 + s; |
| 191 |
|
|
| 192 |
1 |
return temp2.replace('0', '.'); |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
|
| 196 |
|
|
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
|
| 203 |
|
public abstract String printPermissionsBlock(int i); |
| 204 |
|
|
| 205 |
|
|
| 206 |
|
|
| 207 |
|
|
| 208 |
|
|
| 209 |
|
|
| 210 |
|
|
| 211 |
|
public String printPermissionsBlock() { |
| 212 |
5 |
return printPermissionsBlock(this.mask); |
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
public void setAclObjectIdentity(AclObjectIdentity aclObjectIdentity) { |
| 216 |
27 |
this.aclObjectIdentity = aclObjectIdentity; |
| 217 |
27 |
} |
| 218 |
|
|
| 219 |
|
public void setAclObjectParentIdentity(AclObjectIdentity aclObjectParentIdentity) { |
| 220 |
27 |
this.aclObjectParentIdentity = aclObjectParentIdentity; |
| 221 |
27 |
} |
| 222 |
|
|
| 223 |
|
public void setMask(int mask) { |
| 224 |
26 |
this.mask = mask; |
| 225 |
26 |
} |
| 226 |
|
|
| 227 |
|
public void setRecipient(Object recipient) { |
| 228 |
26 |
this.recipient = recipient; |
| 229 |
26 |
} |
| 230 |
|
|
| 231 |
|
public String toString() { |
| 232 |
1 |
StringBuffer sb = new StringBuffer(); |
| 233 |
1 |
sb.append(getClass().getName()); |
| 234 |
1 |
sb.append("[").append(aclObjectIdentity).append(",").append(recipient); |
| 235 |
1 |
sb.append("=").append(printPermissionsBlock(mask)).append(" "); |
| 236 |
1 |
sb.append(printBinary(mask)).append(" ("); |
| 237 |
1 |
sb.append(mask).append(")").append("]"); |
| 238 |
|
|
| 239 |
1 |
return sb.toString(); |
| 240 |
|
} |
| 241 |
|
|
| 242 |
|
public int togglePermission(int permissionToToggle) { |
| 243 |
4 |
this.mask ^= permissionToToggle; |
| 244 |
|
|
| 245 |
4 |
if (Arrays.binarySearch(validPermissions, this.mask) < 0) { |
| 246 |
1 |
throw new IllegalArgumentException("Resulting permission set will be invalid."); |
| 247 |
|
} else { |
| 248 |
3 |
return this.mask; |
| 249 |
|
} |
| 250 |
|
} |
| 251 |
|
} |