1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.security.authorization; |
21 |
|
|
22 |
|
import java.io.Serializable; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.EnumSet; |
26 |
|
import java.util.HashMap; |
27 |
|
import java.util.LinkedList; |
28 |
|
import java.util.List; |
29 |
|
import java.util.Map; |
30 |
|
import java.util.Set; |
31 |
|
|
32 |
|
import org.apache.commons.lang3.builder.EqualsBuilder; |
33 |
|
import org.xwiki.model.EntityType; |
34 |
|
|
35 |
|
import static org.xwiki.security.SecurityReference.FARM; |
36 |
|
import static org.xwiki.security.authorization.RuleState.ALLOW; |
37 |
|
import static org.xwiki.security.authorization.RuleState.DENY; |
38 |
|
import static org.xwiki.security.authorization.RuleState.UNDETERMINED; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
@version |
43 |
|
@since |
44 |
|
|
|
|
| 81.1% |
Uncovered Elements: 25 (132) |
Complexity: 44 |
Complexity Density: 0.54 |
|
45 |
|
public class Right implements RightDescription, Serializable, Comparable<Right> |
46 |
|
{ |
47 |
|
|
48 |
|
public static final Right LOGIN; |
49 |
|
|
50 |
|
|
51 |
|
public static final Right VIEW; |
52 |
|
|
53 |
|
|
54 |
|
public static final Right EDIT; |
55 |
|
|
56 |
|
|
57 |
|
public static final Right DELETE; |
58 |
|
|
59 |
|
|
60 |
|
public static final Right CREATOR; |
61 |
|
|
62 |
|
|
63 |
|
public static final Right ADMIN; |
64 |
|
|
65 |
|
|
66 |
|
public static final Right PROGRAM; |
67 |
|
|
68 |
|
|
69 |
|
public static final Right SCRIPT; |
70 |
|
|
71 |
|
|
72 |
|
public static final Right REGISTER; |
73 |
|
|
74 |
|
|
75 |
|
public static final Right COMMENT; |
76 |
|
|
77 |
|
|
78 |
|
public static final Right CREATE_WIKI; |
79 |
|
|
80 |
|
|
81 |
|
public static final Right ILLEGAL; |
82 |
|
|
83 |
|
|
84 |
|
public static final String ILLEGAL_RIGHT_NAME = "illegal"; |
85 |
|
|
86 |
|
|
87 |
|
public static final Set<EntityType> FARM_ONLY = null; |
88 |
|
|
89 |
|
|
90 |
|
public static final Set<EntityType> WIKI_ONLY = EnumSet.of(EntityType.WIKI); |
91 |
|
|
92 |
|
|
93 |
|
public static final Set<EntityType> WIKI_SPACE = EnumSet.of(EntityType.WIKI, EntityType.SPACE); |
94 |
|
|
95 |
|
|
96 |
|
public static final Set<EntityType> WIKI_SPACE_DOCUMENT |
97 |
|
= EnumSet.of(EntityType.WIKI, EntityType.SPACE, EntityType.DOCUMENT); |
98 |
|
|
99 |
|
|
100 |
|
private static final long serialVersionUID = 1L; |
101 |
|
|
102 |
|
|
103 |
|
private static final List<Right> VALUES = new ArrayList<Right>(); |
104 |
|
|
105 |
|
|
106 |
|
private static final List<Right> UNMODIFIABLE_VALUES = Collections.unmodifiableList(VALUES); |
107 |
|
|
108 |
|
|
109 |
|
private static final List<String> ALL_RIGHTS = new LinkedList<String>(); |
110 |
|
|
111 |
|
|
112 |
|
private static final List<String> UNMODIFIABLE_ALL_RIGHTS = Collections.unmodifiableList(ALL_RIGHTS); |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
private static final Map<EntityType, Set<Right>> ENABLED_RIGHTS = new HashMap<EntityType, Set<Right>>(); |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
private static final Map<EntityType, Set<Right>> UNMODIFIABLE_ENABLED_RIGHTS |
124 |
|
= new HashMap<EntityType, Set<Right>>(); |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
126 |
75 |
static {... |
127 |
75 |
LOGIN = new Right("login", ALLOW, ALLOW, true, null, WIKI_ONLY, true); |
128 |
75 |
VIEW = new Right("view", ALLOW, DENY, true, null, WIKI_SPACE_DOCUMENT, true); |
129 |
75 |
EDIT = new Right("edit", ALLOW, DENY, true, new RightSet(VIEW), WIKI_SPACE_DOCUMENT, false); |
130 |
75 |
DELETE = new Right("delete", DENY, DENY, true, null, WIKI_SPACE_DOCUMENT, false); |
131 |
75 |
CREATOR = |
132 |
|
new Right("creator", DENY, ALLOW, false, new RightSet(DELETE), EnumSet.of(EntityType.DOCUMENT), false); |
133 |
75 |
REGISTER = new Right("register", ALLOW, ALLOW, true, null, WIKI_ONLY, false); |
134 |
75 |
COMMENT = new Right("comment", ALLOW, DENY, true, null, WIKI_SPACE_DOCUMENT, false); |
135 |
75 |
SCRIPT = new Right("script", DENY, DENY, true, null, WIKI_SPACE_DOCUMENT, true); |
136 |
|
|
137 |
75 |
ADMIN = new Right("admin", DENY, ALLOW, false, |
138 |
|
new RightSet(LOGIN, VIEW, SCRIPT, EDIT, DELETE, REGISTER, COMMENT), WIKI_SPACE, true); |
139 |
|
|
140 |
75 |
CREATE_WIKI = new Right("createwiki", DENY, DENY, true, null, FARM_ONLY, false); |
141 |
|
|
142 |
75 |
PROGRAM = new Right("programming", DENY, ALLOW, false, |
143 |
|
new RightSet(LOGIN, VIEW, SCRIPT, EDIT, DELETE, REGISTER, COMMENT, ADMIN, CREATE_WIKI), FARM_ONLY, true); |
144 |
|
|
145 |
75 |
ILLEGAL = new Right(ILLEGAL_RIGHT_NAME, DENY, DENY, false, null, null, false); |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
private final int value; |
150 |
|
|
151 |
|
|
152 |
|
private final String name; |
153 |
|
|
154 |
|
|
155 |
|
private final RuleState defaultState; |
156 |
|
|
157 |
|
|
158 |
|
private final RuleState tieResolutionPolicy; |
159 |
|
|
160 |
|
|
161 |
|
private final boolean inheritanceOverridePolicy; |
162 |
|
|
163 |
|
|
164 |
|
private final Set<Right> impliedRights; |
165 |
|
|
166 |
|
|
167 |
|
private final boolean isReadOnly; |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
@link |
173 |
|
|
174 |
|
@param |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
11 |
Right(RightDescription description)... |
177 |
|
{ |
178 |
11 |
this(description.getName(), description.getDefaultState(), description.getTieResolutionPolicy(), |
179 |
|
description.getInheritanceOverridePolicy(), |
180 |
|
description.getImpliedRights(), |
181 |
|
description.getTargetedEntityType(), description.isReadOnly()); |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
@param |
187 |
|
@param |
188 |
|
@param |
189 |
|
@param |
190 |
|
@param |
191 |
|
@param |
192 |
|
@param |
193 |
|
|
|
|
| 92.9% |
Uncovered Elements: 2 (28) |
Complexity: 5 |
Complexity Density: 0.25 |
|
194 |
911 |
private Right(String name, RuleState defaultState, RuleState tieResolutionPolicy,... |
195 |
|
boolean inheritanceOverridePolicy, Set<Right> impliedRights, Set<EntityType> validEntityTypes, |
196 |
|
boolean isReadOnly) |
197 |
|
{ |
198 |
911 |
checkIllegalArguments(name, defaultState, tieResolutionPolicy); |
199 |
|
|
200 |
911 |
this.name = name; |
201 |
911 |
this.defaultState = defaultState; |
202 |
911 |
this.tieResolutionPolicy = tieResolutionPolicy; |
203 |
911 |
this.inheritanceOverridePolicy = inheritanceOverridePolicy; |
204 |
911 |
this.impliedRights = cloneImpliedRights(impliedRights); |
205 |
911 |
this.isReadOnly = isReadOnly; |
206 |
|
|
207 |
911 |
synchronized (VALUES) { |
208 |
911 |
this.value = VALUES.size(); |
209 |
911 |
if (this.value >= 64) { |
210 |
0 |
throw new IndexOutOfBoundsException(); |
211 |
|
} |
212 |
911 |
VALUES.add(this); |
213 |
911 |
if (!name.equals(ILLEGAL_RIGHT_NAME)) { |
214 |
836 |
ALL_RIGHTS.add(name); |
215 |
|
} |
216 |
911 |
if (validEntityTypes != null) { |
217 |
686 |
for (EntityType type : validEntityTypes) { |
218 |
1533 |
if (type == EntityType.WIKI) { |
219 |
|
|
220 |
611 |
enableFor(FARM); |
221 |
|
} |
222 |
1533 |
enableFor(type); |
223 |
|
} |
224 |
|
} else { |
225 |
|
|
226 |
225 |
enableFor(FARM); |
227 |
|
} |
228 |
|
} |
229 |
|
} |
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
@param |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
235 |
2369 |
private void enableFor(EntityType type)... |
236 |
|
{ |
237 |
2369 |
Set<Right> rights = ENABLED_RIGHTS.get(type); |
238 |
2369 |
if (rights == null) { |
239 |
300 |
rights = new RightSet(); |
240 |
300 |
ENABLED_RIGHTS.put(type, rights); |
241 |
300 |
UNMODIFIABLE_ENABLED_RIGHTS.put(type, Collections.unmodifiableSet(rights)); |
242 |
|
} |
243 |
2369 |
rights.add(this); |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
@param |
250 |
|
@param |
251 |
|
@param |
252 |
|
|
|
|
| 50% |
Uncovered Elements: 6 (12) |
Complexity: 9 |
Complexity Density: 1.5 |
|
253 |
911 |
private void checkIllegalArguments(String name, RuleState defaultState, RuleState tieResolutionPolicy)... |
254 |
|
{ |
255 |
911 |
if (name == null || ALL_RIGHTS.contains(name) || (ILLEGAL != null && name.equals(ILLEGAL_RIGHT_NAME))) { |
256 |
0 |
throw new IllegalArgumentException(String.format("Duplicate name for right [%s]", name)); |
257 |
|
} |
258 |
|
|
259 |
911 |
if (defaultState == null || defaultState == UNDETERMINED) { |
260 |
0 |
throw new IllegalArgumentException( |
261 |
|
String.format("Invalid default state [%s] for right [%s]", defaultState, name)); |
262 |
|
} |
263 |
|
|
264 |
911 |
if (tieResolutionPolicy == null || tieResolutionPolicy == UNDETERMINED) { |
265 |
0 |
throw new IllegalArgumentException( |
266 |
|
String.format("Invalid tie resolution policy [%s] for right [%s]", tieResolutionPolicy, name)); |
267 |
|
} |
268 |
|
} |
269 |
|
|
270 |
|
|
271 |
|
|
272 |
|
@param |
273 |
|
@return |
274 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
275 |
911 |
private Set<Right> cloneImpliedRights(Set<Right> impliedRights)... |
276 |
|
{ |
277 |
911 |
if (impliedRights == null || impliedRights.size() == 0) { |
278 |
609 |
return null; |
279 |
|
} |
280 |
|
|
281 |
302 |
Set<Right> implied = new RightSet(impliedRights); |
282 |
|
|
283 |
302 |
if (implied.size() > 0) { |
284 |
302 |
return Collections.unmodifiableSet(implied); |
285 |
|
} else { |
286 |
0 |
return null; |
287 |
|
} |
288 |
|
} |
289 |
|
|
290 |
|
|
291 |
|
@return |
292 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
293 |
41287 |
public static List<Right> values()... |
294 |
|
{ |
295 |
41290 |
return UNMODIFIABLE_VALUES; |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
@param |
301 |
|
@return |
302 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
303 |
151015 |
public static Right toRight(String string)... |
304 |
|
{ |
305 |
151015 |
for (Right right : VALUES) { |
306 |
706721 |
if (right.name.equals(string)) { |
307 |
151012 |
return right; |
308 |
|
} |
309 |
|
} |
310 |
1 |
return ILLEGAL; |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
@return |
318 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
319 |
88641 |
public static Set<Right> getEnabledRights(EntityType entityType)... |
320 |
|
{ |
321 |
88643 |
Set<Right> enabledRights = UNMODIFIABLE_ENABLED_RIGHTS.get(entityType); |
322 |
88642 |
if (enabledRights == null) { |
323 |
2 |
enabledRights = Collections.<Right>emptySet(); |
324 |
|
} |
325 |
88643 |
return enabledRights; |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
@param |
331 |
|
@return |
332 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
333 |
124530 |
public static Right get(int ordinal) {... |
334 |
124529 |
return VALUES.get(ordinal); |
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
|
@return |
339 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
340 |
38615 |
public static int size() {... |
341 |
38616 |
return values().size(); |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
@return |
346 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
347 |
2 |
public static List<String> getAllRightsAsString()... |
348 |
|
{ |
349 |
2 |
return UNMODIFIABLE_ALL_RIGHTS; |
350 |
|
} |
351 |
|
|
352 |
|
|
353 |
|
@return |
354 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
355 |
474385 |
public int ordinal()... |
356 |
|
{ |
357 |
474390 |
return value; |
358 |
|
} |
359 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
360 |
19527 |
@Override... |
361 |
|
public String getName() |
362 |
|
{ |
363 |
19528 |
return name; |
364 |
|
} |
365 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
366 |
10176 |
@Override... |
367 |
|
public String toString() |
368 |
|
{ |
369 |
10176 |
return getName(); |
370 |
|
} |
371 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
372 |
1385 |
@Override... |
373 |
|
public Set<Right> getImpliedRights() |
374 |
|
{ |
375 |
1385 |
return impliedRights; |
376 |
|
} |
377 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.5 |
|
378 |
0 |
@Override... |
379 |
|
public Set<EntityType> getTargetedEntityType() |
380 |
|
{ |
381 |
0 |
List<EntityType> levels = new ArrayList<EntityType>(); |
382 |
0 |
for (Map.Entry<EntityType, Set<Right>> entry : ENABLED_RIGHTS.entrySet()) { |
383 |
0 |
if (entry.getValue().contains(this)) { |
384 |
0 |
levels.add(entry.getKey()); |
385 |
|
} |
386 |
|
} |
387 |
0 |
if (levels.contains(null) && levels.contains(EntityType.WIKI)) { |
388 |
0 |
levels.remove(null); |
389 |
|
} else { |
390 |
0 |
return null; |
391 |
|
} |
392 |
0 |
return EnumSet.copyOf(levels); |
393 |
|
} |
394 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
395 |
1438 |
@Override... |
396 |
|
public boolean getInheritanceOverridePolicy() |
397 |
|
{ |
398 |
1438 |
return inheritanceOverridePolicy; |
399 |
|
} |
400 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
401 |
1446 |
@Override... |
402 |
|
public RuleState getTieResolutionPolicy() |
403 |
|
{ |
404 |
1446 |
return tieResolutionPolicy; |
405 |
|
} |
406 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
407 |
23840 |
@Override... |
408 |
|
public RuleState getDefaultState() |
409 |
|
{ |
410 |
23840 |
return this.defaultState; |
411 |
|
} |
412 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
413 |
61460 |
@Override... |
414 |
|
public boolean isReadOnly() |
415 |
|
{ |
416 |
61459 |
return this.isReadOnly; |
417 |
|
} |
418 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
419 |
76 |
@Override... |
420 |
|
public int compareTo(Right other) |
421 |
|
{ |
422 |
76 |
return this.ordinal() - other.ordinal(); |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
|
@param |
427 |
|
@return |
428 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
429 |
0 |
boolean like(RightDescription description)... |
430 |
|
{ |
431 |
0 |
return new EqualsBuilder() |
432 |
|
.append(this.isReadOnly(), description.isReadOnly()) |
433 |
|
.append(this.getDefaultState(), description.getDefaultState()) |
434 |
|
.append(this.getTieResolutionPolicy(), description.getTieResolutionPolicy()) |
435 |
|
.append(this.getInheritanceOverridePolicy(), description.getInheritanceOverridePolicy()) |
436 |
|
.append(this.getTargetedEntityType(), description.getTargetedEntityType()) |
437 |
|
.append(this.getImpliedRights(), description.getImpliedRights()) |
438 |
|
.isEquals(); |
439 |
|
} |
440 |
|
} |