1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.wikibridge; |
21 |
|
|
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.Map; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Singleton; |
27 |
|
|
28 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
29 |
|
import org.xwiki.component.annotation.Component; |
30 |
|
import org.xwiki.component.descriptor.DefaultComponentDescriptor; |
31 |
|
import org.xwiki.component.manager.ComponentLookupException; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.model.ModelContext; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.EntityReference; |
36 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
37 |
|
import org.xwiki.rendering.macro.Macro; |
38 |
|
import org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException; |
39 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacro; |
40 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor; |
41 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroException; |
42 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroFactory; |
43 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroManager; |
44 |
|
import org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility; |
45 |
|
|
46 |
|
|
47 |
|
@link |
48 |
|
|
49 |
|
@version |
50 |
|
@since |
51 |
|
|
52 |
|
@Component |
53 |
|
@Singleton |
|
|
| 92.9% |
Uncovered Elements: 4 (56) |
Complexity: 12 |
Complexity Density: 0.27 |
|
54 |
|
public class DefaultWikiMacroManager implements WikiMacroManager |
55 |
|
{ |
56 |
|
|
57 |
|
@link |
58 |
|
|
59 |
|
|
60 |
|
@Inject |
61 |
|
private ComponentManager rootComponentManager; |
62 |
|
|
63 |
|
|
64 |
|
@link |
65 |
|
|
66 |
|
@Inject |
67 |
|
private DocumentAccessBridge bridge; |
68 |
|
|
69 |
|
|
70 |
|
@link |
71 |
|
|
72 |
|
@Inject |
73 |
|
private ModelContext modelContext; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@Inject |
79 |
|
private WikiMacroFactory wikiMacroFactory; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@Inject |
85 |
|
private EntityReferenceSerializer<String> serializer; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
private Map<DocumentReference, WikiMacroData> wikiMacroMap = new HashMap<>(); |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
96 |
|
private static class WikiMacroData |
97 |
|
{ |
98 |
|
|
99 |
|
@see |
100 |
|
|
101 |
|
private String hint; |
102 |
|
|
103 |
|
|
104 |
|
@see |
105 |
|
|
106 |
|
private WikiMacro wikiMacro; |
107 |
|
|
108 |
|
|
109 |
|
@param@link |
110 |
|
@param@link |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
112 |
243 |
WikiMacroData(String hint, WikiMacro wikiMacro)... |
113 |
|
{ |
114 |
243 |
this.hint = hint; |
115 |
243 |
this.wikiMacro = wikiMacro; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
@return |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
18 |
public String getHint()... |
122 |
|
{ |
123 |
18 |
return this.hint; |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
@return |
128 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
129 |
42 |
public WikiMacro getWikiMacro()... |
130 |
|
{ |
131 |
42 |
return this.wikiMacro; |
132 |
|
} |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
135 |
423 |
@Override... |
136 |
|
public boolean hasWikiMacro(DocumentReference documentReference) |
137 |
|
{ |
138 |
423 |
return (null != this.wikiMacroMap.get(documentReference)); |
139 |
|
} |
140 |
|
|
|
|
| 95% |
Uncovered Elements: 1 (20) |
Complexity: 4 |
Complexity Density: 0.25 |
|
141 |
246 |
@Override... |
142 |
|
public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) |
143 |
|
throws InsufficientPrivilegesException, WikiMacroException |
144 |
|
{ |
145 |
246 |
WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); |
146 |
|
|
147 |
|
|
148 |
246 |
if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) { |
149 |
243 |
DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>(); |
150 |
243 |
componentDescriptor.setRoleType(Macro.class); |
151 |
243 |
componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId()); |
152 |
|
|
153 |
|
|
154 |
243 |
String currentUser = this.bridge.getCurrentUser(); |
155 |
243 |
EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); |
156 |
243 |
try { |
157 |
|
|
158 |
|
|
159 |
243 |
this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro |
160 |
|
.getAuthorReference() : this.bridge.getCurrentUserReference())); |
161 |
243 |
this.modelContext.setCurrentEntityReference(documentReference); |
162 |
|
|
163 |
|
|
164 |
243 |
findComponentManager(macroDescriptor.getVisibility()).registerComponent(componentDescriptor, wikiMacro); |
165 |
243 |
this.wikiMacroMap.put(documentReference, |
166 |
|
new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro)); |
167 |
|
} catch (Exception e) { |
168 |
0 |
throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", |
169 |
|
wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()), e); |
170 |
|
} finally { |
171 |
|
|
172 |
243 |
this.bridge.setCurrentUser(currentUser); |
173 |
243 |
this.modelContext.setCurrentEntityReference(currentEntityReference); |
174 |
|
} |
175 |
|
} else { |
176 |
3 |
throw new InsufficientPrivilegesException(String.format( |
177 |
|
"Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro |
178 |
|
.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); |
179 |
|
} |
180 |
|
} |
181 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 4 |
Complexity Density: 0.25 |
|
182 |
21 |
@Override... |
183 |
|
public void unregisterWikiMacro(DocumentReference documentReference) throws WikiMacroException |
184 |
|
{ |
185 |
21 |
WikiMacroData macroData = this.wikiMacroMap.get(documentReference); |
186 |
21 |
if (macroData != null) { |
187 |
21 |
WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) macroData.getWikiMacro().getDescriptor(); |
188 |
|
|
189 |
|
|
190 |
21 |
if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) { |
191 |
18 |
String currentUser = this.bridge.getCurrentUser(); |
192 |
18 |
EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); |
193 |
18 |
try { |
194 |
|
|
195 |
|
|
196 |
18 |
this.bridge |
197 |
|
.setCurrentUser(this.serializer.serialize(macroData.getWikiMacro().getAuthorReference())); |
198 |
18 |
this.modelContext.setCurrentEntityReference(documentReference); |
199 |
|
|
200 |
18 |
findComponentManager(macroDescriptor.getVisibility()).unregisterComponent(Macro.class, |
201 |
|
macroData.getHint()); |
202 |
18 |
this.wikiMacroMap.remove(documentReference); |
203 |
|
} catch (Exception e) { |
204 |
0 |
throw new WikiMacroException(String.format("Failed to unregister macro [%s] in [%s] for " |
205 |
|
+ "visibility [%s]", macroData.getHint(), documentReference, macroDescriptor.getVisibility()), |
206 |
|
e); |
207 |
|
} finally { |
208 |
18 |
this.bridge.setCurrentUser(currentUser); |
209 |
18 |
this.modelContext.setCurrentEntityReference(currentEntityReference); |
210 |
|
} |
211 |
|
} else { |
212 |
3 |
throw new WikiMacroException(String.format("Unable to unregister macro [%s] in [%s] for visibility " |
213 |
|
+ "[%s] due to insufficient privileges", macroData.getWikiMacro().getDescriptor().getId().getId(), |
214 |
|
documentReference, macroDescriptor.getVisibility())); |
215 |
|
} |
216 |
|
} else { |
217 |
0 |
throw new WikiMacroException(String.format("Macro in [%s] isn't registered", documentReference)); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
@param |
223 |
|
@return |
224 |
|
|
225 |
|
|
226 |
|
@throws |
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.27 |
|
228 |
261 |
private ComponentManager findComponentManager(WikiMacroVisibility visibility) throws ComponentLookupException... |
229 |
|
{ |
230 |
261 |
ComponentManager cm; |
231 |
|
|
232 |
261 |
switch (visibility) { |
233 |
42 |
case USER: |
234 |
42 |
cm = this.rootComponentManager.getInstance(ComponentManager.class, "user"); |
235 |
42 |
break; |
236 |
196 |
case WIKI: |
237 |
196 |
cm = this.rootComponentManager.getInstance(ComponentManager.class, "wiki"); |
238 |
196 |
break; |
239 |
23 |
default: |
240 |
23 |
cm = this.rootComponentManager; |
241 |
|
} |
242 |
|
|
243 |
261 |
return cm; |
244 |
|
} |
245 |
|
} |