1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.uiextension.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import javax.inject.Inject; |
26 |
|
import javax.inject.Named; |
27 |
|
import javax.inject.Provider; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.slf4j.Logger; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.component.manager.ComponentManager; |
33 |
|
import org.xwiki.component.wiki.WikiComponent; |
34 |
|
import org.xwiki.component.wiki.WikiComponentBuilder; |
35 |
|
import org.xwiki.component.wiki.WikiComponentException; |
36 |
|
import org.xwiki.component.wiki.WikiComponentScope; |
37 |
|
import org.xwiki.model.reference.DocumentReference; |
38 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
39 |
|
import org.xwiki.security.authorization.AuthorExecutor; |
40 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
41 |
|
import org.xwiki.security.authorization.Right; |
42 |
|
|
43 |
|
import com.xpn.xwiki.XWikiContext; |
44 |
|
import com.xpn.xwiki.XWikiException; |
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
import com.xpn.xwiki.objects.BaseObject; |
47 |
|
|
48 |
|
|
49 |
|
@link |
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Singleton |
56 |
|
@Named("uiextension") |
|
|
| 96.3% |
Uncovered Elements: 2 (54) |
Complexity: 12 |
Complexity Density: 0.29 |
|
57 |
|
public class WikiUIExtensionComponentBuilder implements WikiComponentBuilder, WikiUIExtensionConstants |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Inject |
63 |
|
private Logger logger; |
64 |
|
|
65 |
|
|
66 |
|
@see |
67 |
|
|
68 |
|
|
69 |
|
@Inject |
70 |
|
@Named("compactwiki") |
71 |
|
private EntityReferenceSerializer<String> compactWikiSerializer; |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
@Inject |
77 |
|
private EntityReferenceSerializer<String> serializer; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@Inject |
83 |
|
@Named("wiki") |
84 |
|
private ComponentManager cm; |
85 |
|
|
86 |
|
@Inject |
87 |
|
private Provider<XWikiContext> xcontextProvider; |
88 |
|
|
89 |
|
@Inject |
90 |
|
private AuthorizationManager authorization; |
91 |
|
|
92 |
|
@Inject |
93 |
|
private AuthorExecutor authorExecutor; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
@link |
98 |
|
|
99 |
|
|
100 |
|
@param |
101 |
|
@param |
102 |
|
@throws |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 5 |
Complexity Density: 1.25 |
|
104 |
130 |
private void checkRights(XWikiDocument extensionsDoc, WikiComponentScope scope) throws WikiComponentException... |
105 |
|
{ |
106 |
130 |
if (scope == WikiComponentScope.GLOBAL |
107 |
|
&& !this.authorization.hasAccess(Right.PROGRAM, extensionsDoc.getContentAuthorReference(), null)) { |
108 |
1 |
throw new WikiComponentException("Registering global UI extensions requires programming rights"); |
109 |
129 |
} else if (scope == WikiComponentScope.WIKI |
110 |
|
&& !this.authorization.hasAccess(Right.ADMIN, extensionsDoc.getContentAuthorReference(), extensionsDoc |
111 |
|
.getDocumentReference().getWikiReference())) { |
112 |
1 |
throw new WikiComponentException( |
113 |
|
"Registering UI extensions at wiki level requires wiki administration rights"); |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
@link |
119 |
|
|
120 |
|
@param |
121 |
|
@return@link |
122 |
|
@throws |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
124 |
130 |
private List<BaseObject> getExtensionDefinitions(XWikiDocument extensionsDoc) throws WikiComponentException... |
125 |
|
{ |
126 |
|
|
127 |
130 |
List<BaseObject> extensionDefinitions = extensionsDoc.getXObjects(UI_EXTENSION_CLASS); |
128 |
|
|
129 |
130 |
if (extensionDefinitions.size() == 0) { |
130 |
1 |
throw new WikiComponentException(String.format("No UI extension object could be found in document [%s]", |
131 |
|
extensionsDoc.getDocumentReference())); |
132 |
|
} |
133 |
|
|
134 |
129 |
return extensionDefinitions; |
135 |
|
} |
136 |
|
|
|
|
| 96.2% |
Uncovered Elements: 1 (26) |
Complexity: 3 |
Complexity Density: 0.12 |
|
137 |
130 |
@Override... |
138 |
|
public List<WikiComponent> buildComponents(DocumentReference reference) throws WikiComponentException |
139 |
|
{ |
140 |
130 |
List<WikiComponent> extensions = new ArrayList<WikiComponent>(); |
141 |
130 |
XWikiDocument doc = null; |
142 |
|
|
143 |
130 |
try { |
144 |
130 |
XWikiContext xcontext = xcontextProvider.get(); |
145 |
130 |
doc = xcontext.getWiki().getDocument(reference, xcontext); |
146 |
|
} catch (XWikiException e) { |
147 |
0 |
throw new WikiComponentException( |
148 |
|
String.format("Failed to create UI Extension(s) document [%s]", reference), e); |
149 |
|
} |
150 |
|
|
151 |
130 |
for (BaseObject extensionDefinition : this.getExtensionDefinitions(doc)) { |
152 |
133 |
if (extensionDefinition == null) { |
153 |
3 |
continue; |
154 |
|
} |
155 |
|
|
156 |
130 |
String id = extensionDefinition.getStringValue(ID_PROPERTY); |
157 |
130 |
String extensionPointId = extensionDefinition.getStringValue(EXTENSION_POINT_ID_PROPERTY); |
158 |
130 |
String content = extensionDefinition.getStringValue(CONTENT_PROPERTY); |
159 |
130 |
String rawParameters = extensionDefinition.getStringValue(PARAMETERS_PROPERTY); |
160 |
130 |
WikiComponentScope scope = |
161 |
|
WikiComponentScope.fromString(extensionDefinition.getStringValue(SCOPE_PROPERTY)); |
162 |
|
|
163 |
|
|
164 |
130 |
this.checkRights(doc, scope); |
165 |
|
|
166 |
128 |
String roleHint = this.serializer.serialize(extensionDefinition.getReference()); |
167 |
|
|
168 |
128 |
WikiUIExtension extension = |
169 |
|
new WikiUIExtension(roleHint, id, extensionPointId, extensionDefinition.getReference(), |
170 |
|
doc.getAuthorReference(), this.authorExecutor); |
171 |
|
|
172 |
|
|
173 |
|
|
174 |
128 |
WikiUIExtensionParameters parameters = new WikiUIExtensionParameters(id, rawParameters, cm); |
175 |
128 |
extension.setParameters(parameters); |
176 |
|
|
177 |
|
|
178 |
128 |
WikiUIExtensionRenderer renderer = |
179 |
|
new WikiUIExtensionRenderer(roleHint, content, doc.getDocumentReference(), cm); |
180 |
128 |
extension.setRenderer(renderer); |
181 |
128 |
extension.setScope(scope); |
182 |
128 |
extensions.add(extension); |
183 |
|
} |
184 |
|
|
185 |
127 |
return extensions; |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
@return |
190 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 2 |
Complexity Density: 0.2 |
|
191 |
3873 |
@Override... |
192 |
|
public List<DocumentReference> getDocumentReferences() |
193 |
|
{ |
194 |
3873 |
List<DocumentReference> results = new ArrayList<DocumentReference>(); |
195 |
|
|
196 |
3873 |
String query = |
197 |
|
", BaseObject as obj, StringProperty as epId where obj.className=? " |
198 |
|
+ "and obj.name=doc.fullName and epId.id.id=obj.id and epId.id.name=? " |
199 |
|
+ "and (epId.value <> '' or (epId.value is not null and '' is null))"; |
200 |
3873 |
List<String> parameters = new ArrayList<String>(); |
201 |
3873 |
parameters.add(this.compactWikiSerializer.serialize(UI_EXTENSION_CLASS)); |
202 |
3873 |
parameters.add(EXTENSION_POINT_ID_PROPERTY); |
203 |
|
|
204 |
3873 |
try { |
205 |
3873 |
XWikiContext xcontext = xcontextProvider.get(); |
206 |
3873 |
results.addAll(xcontext.getWiki().getStore().searchDocumentReferences(query, parameters, xcontext)); |
207 |
|
} catch (XWikiException e) { |
208 |
0 |
this.logger.warn("Search for UI extensions failed: [{}]", e.getMessage()); |
209 |
|
} |
210 |
|
|
211 |
3873 |
return results; |
212 |
|
} |
213 |
|
} |