1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.skin; |
21 |
|
|
22 |
|
import javax.inject.Inject; |
23 |
|
import javax.inject.Named; |
24 |
|
import javax.inject.Provider; |
25 |
|
import javax.inject.Singleton; |
26 |
|
|
27 |
|
import org.apache.commons.lang3.StringUtils; |
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
30 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
31 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
32 |
|
import org.xwiki.cache.Cache; |
33 |
|
import org.xwiki.cache.CacheException; |
34 |
|
import org.xwiki.cache.CacheManager; |
35 |
|
import org.xwiki.cache.config.LRUCacheConfiguration; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.component.phase.Initializable; |
38 |
|
import org.xwiki.component.phase.InitializationException; |
39 |
|
import org.xwiki.configuration.ConfigurationSource; |
40 |
|
import org.xwiki.environment.Environment; |
41 |
|
import org.xwiki.observation.AbstractEventListener; |
42 |
|
import org.xwiki.observation.ObservationManager; |
43 |
|
import org.xwiki.observation.event.Event; |
44 |
|
import org.xwiki.rendering.syntax.SyntaxFactory; |
45 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
46 |
|
import org.xwiki.security.authorization.Right; |
47 |
|
import org.xwiki.skin.ResourceRepository; |
48 |
|
import org.xwiki.skin.Skin; |
49 |
|
|
50 |
|
import com.xpn.xwiki.XWikiContext; |
51 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
52 |
|
|
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component(roles = InternalSkinManager.class) |
58 |
|
@Singleton |
|
|
| 69.7% |
Uncovered Elements: 47 (155) |
Complexity: 43 |
Complexity Density: 0.49 |
|
59 |
|
public class InternalSkinManager implements Initializable |
60 |
|
{ |
61 |
|
public static final String CKEY_SKIN = "skin"; |
62 |
|
|
63 |
|
public static final String CKEY_PARENTSKIN = "baseskin"; |
64 |
|
|
65 |
|
@Inject |
66 |
|
private InternalSkinConfiguration skinConfiguration; |
67 |
|
|
68 |
|
@Inject |
69 |
|
private Provider<XWikiContext> xcontextProvider; |
70 |
|
|
71 |
|
@Inject |
72 |
|
private WikiSkinUtils wikiSkinUtils; |
73 |
|
|
74 |
|
@Inject |
75 |
|
private Environment environment; |
76 |
|
|
77 |
|
@Inject |
78 |
|
@Named("all") |
79 |
|
private ConfigurationSource allConfiguration; |
80 |
|
|
81 |
|
@Inject |
82 |
|
private ObservationManager observation; |
83 |
|
|
84 |
|
@Inject |
85 |
|
private CacheManager cacheManager; |
86 |
|
|
87 |
|
@Inject |
88 |
|
private ContextualAuthorizationManager authorization; |
89 |
|
|
90 |
|
@Inject |
91 |
|
private Logger logger; |
92 |
|
|
93 |
|
@Inject |
94 |
|
private SyntaxFactory syntaxFactory; |
95 |
|
|
96 |
|
private Cache<Skin> cache; |
97 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
98 |
99 |
@Override... |
99 |
|
public void initialize() throws InitializationException |
100 |
|
{ |
101 |
|
|
102 |
99 |
try { |
103 |
99 |
this.cache = this.cacheManager.createNewCache(new LRUCacheConfiguration("skins", 100, 86400)); |
104 |
|
} catch (CacheException e) { |
105 |
0 |
throw new InitializationException("Failed to initialize cache", e); |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
99 |
this.observation.addListener(new AbstractEventListener("skins", new DocumentUpdatedEvent(), |
110 |
|
new DocumentDeletedEvent(), new DocumentCreatedEvent()) |
111 |
|
{ |
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
112 |
3966 |
@Override... |
113 |
|
public void onEvent(Event event, Object source, Object data) |
114 |
|
{ |
115 |
3966 |
XWikiDocument document = (XWikiDocument) source; |
116 |
|
|
117 |
3966 |
if (document.getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null |
118 |
|
|| document.getOriginalDocument().getXObject(WikiSkinUtils.SKINCLASS_REFERENCE) != null) { |
119 |
|
|
120 |
0 |
InternalSkinManager.this.cache.removeAll(); |
121 |
|
} |
122 |
|
|
123 |
|
} |
124 |
|
}); |
125 |
|
} |
126 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
127 |
303945 |
public Skin getSkin(String id)... |
128 |
|
{ |
129 |
303949 |
if (StringUtils.isBlank(id)) { |
130 |
295 |
return null; |
131 |
|
} |
132 |
|
|
133 |
303648 |
Skin skin = this.cache.get(id); |
134 |
|
|
135 |
303646 |
if (skin == null) { |
136 |
303650 |
skin = createSkin(id); |
137 |
|
} |
138 |
|
|
139 |
303660 |
return skin; |
140 |
|
} |
141 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
142 |
303635 |
private Skin createSkin(String id)... |
143 |
|
{ |
144 |
303642 |
Skin skin; |
145 |
|
|
146 |
303673 |
if (this.wikiSkinUtils.isWikiSkin(id)) { |
147 |
166 |
skin = new WikiSkin(id, this, this.skinConfiguration, this.wikiSkinUtils, this.logger, this.syntaxFactory); |
148 |
|
} else { |
149 |
303506 |
skin = new EnvironmentSkin(id, this, this.skinConfiguration, this.logger, this.syntaxFactory, |
150 |
|
this.environment, this.xcontextProvider); |
151 |
|
} |
152 |
|
|
153 |
303653 |
return skin; |
154 |
|
} |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
156 |
303568 |
public Skin getCurrentSkin(boolean testRights)... |
157 |
|
{ |
158 |
303571 |
return getSkin(getCurrentSkinId(testRights)); |
159 |
|
} |
160 |
|
|
|
|
| 88.4% |
Uncovered Elements: 5 (43) |
Complexity: 10 |
Complexity Density: 0.4 |
|
161 |
314687 |
public String getCurrentSkinId(boolean testRights)... |
162 |
|
{ |
163 |
314705 |
String skin; |
164 |
|
|
165 |
314708 |
XWikiContext xcontext = this.xcontextProvider.get(); |
166 |
|
|
167 |
314751 |
if (xcontext != null) { |
168 |
|
|
169 |
314746 |
skin = (String) xcontext.get(CKEY_SKIN); |
170 |
314741 |
if (StringUtils.isNotEmpty(skin)) { |
171 |
302998 |
return skin; |
172 |
|
} else { |
173 |
11739 |
skin = null; |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
11741 |
if (xcontext.getRequest() != null) { |
178 |
11720 |
skin = xcontext.getRequest().getParameter("skin"); |
179 |
11720 |
if (StringUtils.isNotEmpty(skin)) { |
180 |
1226 |
return skin; |
181 |
|
} else { |
182 |
10484 |
skin = null; |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
10506 |
skin = this.allConfiguration.getProperty("skin"); |
188 |
10516 |
if (skin != null) { |
189 |
0 |
return skin; |
190 |
|
} |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
10523 |
skin = getDefaultSkinId(); |
195 |
|
|
196 |
10523 |
if (xcontext != null) { |
197 |
|
|
198 |
|
|
199 |
10507 |
if (testRights) { |
200 |
2619 |
XWikiDocument document = this.wikiSkinUtils.getSkinDocument(skin); |
201 |
2618 |
if (document != null) { |
202 |
9 |
if (!this.authorization.hasAccess(Right.VIEW, document.getDocumentReference())) { |
203 |
0 |
this.logger.debug( |
204 |
|
"Cannot access configured wiki skin [{}] due to access rights, using the default skin.", |
205 |
|
skin); |
206 |
0 |
skin = getDefaultSkinId(); |
207 |
|
} |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
10509 |
xcontext.put(CKEY_SKIN, skin); |
213 |
|
} |
214 |
|
|
215 |
10514 |
return skin; |
216 |
|
} |
217 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
218 |
0 |
public String getParentSkin(String skinId)... |
219 |
|
{ |
220 |
0 |
Skin skin = getSkin(skinId); |
221 |
0 |
if (skin != null) { |
222 |
0 |
ResourceRepository parent = skin.getParent(); |
223 |
0 |
if (parent != null) { |
224 |
0 |
return parent.getId(); |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
0 |
return null; |
229 |
|
} |
230 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
231 |
0 |
public Skin getCurrentParentSkin(boolean testRights)... |
232 |
|
{ |
233 |
0 |
return getSkin(getCurrentParentSkinId(testRights)); |
234 |
|
} |
235 |
|
|
|
|
| 57.1% |
Uncovered Elements: 15 (35) |
Complexity: 9 |
Complexity Density: 0.47 |
|
236 |
1113 |
public String getCurrentParentSkinId(boolean testRights)... |
237 |
|
{ |
238 |
|
|
239 |
1113 |
String baseSkin = getContextParentId(); |
240 |
|
|
241 |
|
|
242 |
1091 |
if (baseSkin == null) { |
243 |
1091 |
Skin skin = getCurrentSkin(testRights); |
244 |
1114 |
if (skin != null) { |
245 |
1113 |
ResourceRepository parent = skin.getParent(); |
246 |
1111 |
if (parent != null) { |
247 |
1107 |
baseSkin = parent.getId(); |
248 |
|
} |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
1099 |
if (baseSkin == null) { |
254 |
1092 |
baseSkin = getDefaultParentSkinId(); |
255 |
|
} |
256 |
|
|
257 |
1114 |
XWikiContext xcontext = this.xcontextProvider.get(); |
258 |
|
|
259 |
1113 |
if (xcontext != null) { |
260 |
|
|
261 |
|
|
262 |
1112 |
if (testRights) { |
263 |
0 |
XWikiDocument document = this.wikiSkinUtils.getSkinDocument(baseSkin); |
264 |
0 |
if (document != null) { |
265 |
0 |
if (!this.authorization.hasAccess(Right.VIEW, document.getDocumentReference())) { |
266 |
0 |
this.logger.debug( |
267 |
|
"Cannot access configured wiki skin [{}] due to access rights, using the default skin.", |
268 |
|
baseSkin); |
269 |
0 |
baseSkin = getDefaultParentSkinId(); |
270 |
|
} |
271 |
|
} |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
1113 |
xcontext.put(CKEY_PARENTSKIN, baseSkin); |
276 |
|
} |
277 |
|
|
278 |
1112 |
return baseSkin; |
279 |
|
} |
280 |
|
|
|
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
281 |
1109 |
public String getContextParentId()... |
282 |
|
{ |
283 |
1111 |
String parentId = null; |
284 |
|
|
285 |
1096 |
XWikiContext xcontext = this.xcontextProvider.get(); |
286 |
|
|
287 |
1106 |
if (xcontext != null) { |
288 |
1099 |
parentId = (String) xcontext.get(CKEY_PARENTSKIN); |
289 |
1103 |
if (StringUtils.isEmpty(parentId)) { |
290 |
1098 |
parentId = null; |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
1102 |
return parentId; |
295 |
|
} |
296 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
297 |
0 |
public Skin getDefaultSkin()... |
298 |
|
{ |
299 |
0 |
return getSkin(getDefaultSkinId()); |
300 |
|
} |
301 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
302 |
10522 |
public String getDefaultSkinId()... |
303 |
|
{ |
304 |
10522 |
String skin = this.skinConfiguration.getDefaultSkinId(); |
305 |
|
|
306 |
|
|
307 |
10522 |
if (skin == null) { |
308 |
0 |
skin = getDefaultParentSkinId(); |
309 |
|
} |
310 |
|
|
311 |
10518 |
return skin; |
312 |
|
} |
313 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
314 |
0 |
public Skin getDefaultParentSkin()... |
315 |
|
{ |
316 |
0 |
return getSkin(this.skinConfiguration.getDefaultParentSkinId()); |
317 |
|
} |
318 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
319 |
2206 |
public String getDefaultParentSkinId()... |
320 |
|
{ |
321 |
2214 |
String skin = this.skinConfiguration.getDefaultParentSkinId(); |
322 |
|
|
323 |
|
|
324 |
2228 |
if (skin == null) { |
325 |
0 |
skin = this.skinConfiguration.getDefaultSkinId(); |
326 |
|
} |
327 |
|
|
328 |
2229 |
return skin; |
329 |
|
} |
330 |
|
} |