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.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.HashSet; |
24 |
|
import java.util.Set; |
25 |
|
|
26 |
|
import javax.inject.Inject; |
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Provider; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.model.EntityType; |
33 |
|
import org.xwiki.model.reference.DocumentReference; |
34 |
|
import org.xwiki.model.reference.EntityReference; |
35 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
36 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
37 |
|
import org.xwiki.security.authorization.AccessDeniedException; |
38 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
39 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
40 |
|
import org.xwiki.security.authorization.Right; |
41 |
|
import org.xwiki.security.internal.XWikiConstants; |
42 |
|
|
43 |
|
import com.xpn.xwiki.XWikiContext; |
44 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
45 |
|
|
46 |
|
|
47 |
|
@link |
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 85.6% |
Uncovered Elements: 15 (104) |
Complexity: 33 |
Complexity Density: 0.58 |
|
54 |
|
public class DefaultContextualAuthorizationManager implements ContextualAuthorizationManager |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private static final Set<Right> CONTENT_AUTHOR_RIGHTS = new HashSet<Right>(Arrays.asList(Right.SCRIPT, |
60 |
|
Right.PROGRAM)); |
61 |
|
|
62 |
|
@Inject |
63 |
|
private AuthorizationManager authorizationManager; |
64 |
|
|
65 |
|
@Inject |
66 |
|
private RenderingContext renderingContext; |
67 |
|
|
68 |
|
@Inject |
69 |
|
@Named("current") |
70 |
|
private EntityReferenceResolver<EntityReference> resolver; |
71 |
|
|
72 |
|
@Inject |
73 |
|
private Provider<XWikiContext> xcontextProvider; |
74 |
|
|
|
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
75 |
5 |
@Override... |
76 |
|
public void checkAccess(Right right) throws AccessDeniedException |
77 |
|
{ |
78 |
5 |
if (CONTENT_AUTHOR_RIGHTS.contains(right)) { |
79 |
5 |
EntityReference entity; |
80 |
5 |
if (right == Right.PROGRAM) { |
81 |
|
|
82 |
5 |
entity = null; |
83 |
|
} else { |
84 |
0 |
entity = getCurrentEntity(); |
85 |
|
} |
86 |
5 |
checkAccess(right, getCurrentUser(right, null), entity); |
87 |
|
} else { |
88 |
0 |
checkAccess(right, getCurrentEntity()); |
89 |
|
} |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
92 |
1 |
@Override... |
93 |
|
public void checkAccess(Right right, EntityReference entity) throws AccessDeniedException |
94 |
|
{ |
95 |
1 |
DocumentReference user = getCurrentUser(right, entity); |
96 |
|
|
97 |
1 |
checkAccess(right, user, entity); |
98 |
|
} |
99 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
100 |
6 |
private void checkAccess(Right right, DocumentReference user, EntityReference entity) throws AccessDeniedException... |
101 |
|
{ |
102 |
6 |
if (!checkPreAccess(right)) { |
103 |
0 |
throw new AccessDeniedException(right, user, entity); |
104 |
|
} |
105 |
|
|
106 |
6 |
this.authorizationManager.checkAccess(right, user, getFullReference(entity)); |
107 |
|
} |
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
109 |
32218 |
@Override... |
110 |
|
public boolean hasAccess(Right right) |
111 |
|
{ |
112 |
32217 |
if (CONTENT_AUTHOR_RIGHTS.contains(right)) { |
113 |
11319 |
EntityReference entity; |
114 |
11317 |
if (right == Right.PROGRAM) { |
115 |
|
|
116 |
2730 |
entity = null; |
117 |
|
} else { |
118 |
8589 |
entity = getCurrentEntity(); |
119 |
|
} |
120 |
11317 |
return hasAccess(right, getCurrentUser(right, null), entity); |
121 |
|
} |
122 |
|
|
123 |
20899 |
return hasAccess(right, getCurrentEntity()); |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
126 |
93874 |
@Override... |
127 |
|
public boolean hasAccess(Right right, EntityReference entity) |
128 |
|
{ |
129 |
93873 |
DocumentReference user = getCurrentUser(right, entity); |
130 |
|
|
131 |
93872 |
return hasAccess(right, user, entity); |
132 |
|
} |
133 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
134 |
105182 |
private boolean hasAccess(Right right, DocumentReference user, EntityReference entity)... |
135 |
|
{ |
136 |
105185 |
return checkPreAccess(right) && this.authorizationManager.hasAccess(right, user, getFullReference(entity)); |
137 |
|
} |
138 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 2 |
Complexity Density: 2 |
|
139 |
105172 |
private EntityReference getFullReference(EntityReference reference)... |
140 |
|
{ |
141 |
105175 |
return reference != null ? this.resolver.resolve(reference, reference.getType()) : null; |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@param |
148 |
|
@return |
149 |
|
|
|
|
| 83.3% |
Uncovered Elements: 2 (12) |
Complexity: 5 |
Complexity Density: 0.83 |
|
150 |
105190 |
private boolean checkPreAccess(Right right)... |
151 |
|
{ |
152 |
105194 |
if (CONTENT_AUTHOR_RIGHTS.contains(right)) { |
153 |
18152 |
if (this.renderingContext.isRestricted()) { |
154 |
0 |
return false; |
155 |
18152 |
} else if (right == Right.PROGRAM && this.xcontextProvider.get().hasDroppedPermissions()) { |
156 |
22 |
return false; |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
105167 |
return true; |
161 |
|
} |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 4 |
Complexity Density: 0.8 |
|
163 |
105196 |
private DocumentReference getCurrentUser(Right right, EntityReference entity)... |
164 |
|
{ |
165 |
|
|
166 |
105195 |
if (CONTENT_AUTHOR_RIGHTS.contains(right)) { |
167 |
18152 |
XWikiDocument doc = entity == null ? getProgrammingDocument() : getDocument(entity); |
168 |
18151 |
if (doc != null) { |
169 |
18105 |
return getContentAuthor(doc); |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
87088 |
return this.xcontextProvider.get().getUserReference(); |
174 |
|
} |
175 |
|
|
|
|
| 61.5% |
Uncovered Elements: 5 (13) |
Complexity: 4 |
Complexity Density: 0.44 |
|
176 |
6828 |
private XWikiDocument getDocument(EntityReference entity)... |
177 |
|
{ |
178 |
6828 |
if (entity == null) { |
179 |
0 |
return null; |
180 |
|
} |
181 |
|
|
182 |
6828 |
EntityReference docEntity = entity.extractReference(EntityType.DOCUMENT); |
183 |
6828 |
if (docEntity == null) { |
184 |
0 |
return null; |
185 |
|
} |
186 |
|
|
187 |
6828 |
XWikiContext xcontext = this.xcontextProvider.get(); |
188 |
|
|
189 |
6828 |
try { |
190 |
6828 |
return xcontext.getWiki().getDocument(new DocumentReference(docEntity), xcontext); |
191 |
|
} catch (Exception e) { |
192 |
|
|
193 |
|
} |
194 |
|
|
195 |
0 |
return null; |
196 |
|
} |
197 |
|
|
198 |
|
|
199 |
|
@param |
200 |
|
@return |
201 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
202 |
18107 |
private DocumentReference getContentAuthor(XWikiDocument doc)... |
203 |
|
{ |
204 |
18107 |
DocumentReference user = doc.getContentAuthorReference(); |
205 |
|
|
206 |
18105 |
if (user != null && XWikiConstants.GUEST_USER.equals(user.getName())) { |
207 |
|
|
208 |
|
|
209 |
0 |
user = null; |
210 |
|
} |
211 |
|
|
212 |
18107 |
return user; |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@return |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
220 |
29487 |
private EntityReference getCurrentEntity()... |
221 |
|
{ |
222 |
29487 |
XWikiContext xcontext = this.xcontextProvider.get(); |
223 |
29488 |
XWikiDocument doc = xcontext.getDoc(); |
224 |
|
|
225 |
29488 |
if (doc != null) { |
226 |
29485 |
return doc.getDocumentReference(); |
227 |
|
} |
228 |
|
|
229 |
1 |
return null; |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
@return |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
237 |
11324 |
private XWikiDocument getProgrammingDocument()... |
238 |
|
{ |
239 |
11324 |
XWikiContext xcontext = this.xcontextProvider.get(); |
240 |
|
|
241 |
11324 |
XWikiDocument document = (XWikiDocument) xcontext.get(XWikiDocument.CKEY_SDOC); |
242 |
11323 |
if (document == null) { |
243 |
140 |
document = xcontext.getDoc(); |
244 |
|
} |
245 |
|
|
246 |
11323 |
return document; |
247 |
|
} |
248 |
|
} |