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