1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wiki.template.script; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.List; |
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.slf4j.Logger; |
32 |
|
import org.xwiki.component.annotation.Component; |
33 |
|
import org.xwiki.context.Execution; |
34 |
|
import org.xwiki.job.event.status.JobStatus; |
35 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
36 |
|
import org.xwiki.model.reference.WikiReference; |
37 |
|
import org.xwiki.script.service.ScriptService; |
38 |
|
import org.xwiki.security.authorization.AccessDeniedException; |
39 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
40 |
|
import org.xwiki.security.authorization.Right; |
41 |
|
import org.xwiki.wiki.descriptor.WikiDescriptor; |
42 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
43 |
|
import org.xwiki.wiki.manager.WikiManagerException; |
44 |
|
import org.xwiki.wiki.provisioning.WikiProvisioningJob; |
45 |
|
import org.xwiki.wiki.template.WikiTemplateManager; |
46 |
|
import org.xwiki.wiki.template.WikiTemplateManagerException; |
47 |
|
|
48 |
|
import com.xpn.xwiki.XWikiContext; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@since |
54 |
|
@version |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("wiki.template") |
58 |
|
@Singleton |
|
|
| 100% |
Uncovered Elements: 0 (60) |
Complexity: 19 |
Complexity Density: 0.4 |
|
59 |
|
public class WikiTemplateManagerScript implements ScriptService |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@Deprecated |
65 |
|
public static final String CONTEXT_LASTEXCEPTION = "lastexception"; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String WIKITEMPLATEERROR_KEY = "scriptservice.wiki.template.error"; |
71 |
|
|
72 |
|
@Inject |
73 |
|
private WikiTemplateManager wikiTemplateManager; |
74 |
|
|
75 |
|
@Inject |
76 |
|
private WikiDescriptorManager wikiDescriptorManager; |
77 |
|
|
78 |
|
@Inject |
79 |
|
private AuthorizationManager authorizationManager; |
80 |
|
|
81 |
|
@Inject |
82 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
83 |
|
|
84 |
|
|
85 |
|
@link |
86 |
|
|
87 |
|
@Inject |
88 |
|
private Provider<XWikiContext> xcontextProvider; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
private Execution execution; |
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
@Inject |
100 |
|
private Logger logger; |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
@return |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
10 |
public Exception getLastError()... |
108 |
|
{ |
109 |
10 |
return (Exception) this.execution.getContext().getProperty(WIKITEMPLATEERROR_KEY); |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
@link |
114 |
|
|
115 |
|
@param |
116 |
|
@see |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
10 |
private void setLastError(Exception e)... |
119 |
|
{ |
120 |
10 |
this.execution.getContext().setProperty(WIKITEMPLATEERROR_KEY, e); |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
@return |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
128 |
6 |
public Collection<WikiDescriptor> getTemplates()... |
129 |
|
{ |
130 |
6 |
try { |
131 |
6 |
return wikiTemplateManager.getTemplates(); |
132 |
|
} catch (WikiTemplateManagerException e) { |
133 |
1 |
error("Error while getting all the wiki templates.", e); |
134 |
1 |
return new ArrayList<WikiDescriptor>(); |
135 |
|
} |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@param |
142 |
|
@param |
143 |
|
@return |
144 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 5 |
Complexity Density: 0.29 |
|
145 |
6 |
public boolean setTemplate(String wikiId, boolean value)... |
146 |
|
{ |
147 |
6 |
XWikiContext context = xcontextProvider.get(); |
148 |
6 |
try { |
149 |
|
|
150 |
6 |
authorizationManager.checkAccess(Right.PROGRAM, context.getDoc().getAuthorReference(), |
151 |
|
context.getDoc().getDocumentReference()); |
152 |
|
|
153 |
5 |
WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId); |
154 |
|
|
155 |
4 |
String owner = descriptor.getOwnerId(); |
156 |
|
|
157 |
4 |
WikiReference wikiReference = new WikiReference(descriptor.getId()); |
158 |
4 |
String currentUser = entityReferenceSerializer.serialize(context.getUserReference()); |
159 |
4 |
if (!currentUser.equals(owner)) { |
160 |
2 |
authorizationManager.checkAccess(Right.ADMIN, context.getUserReference(), wikiReference); |
161 |
|
} |
162 |
|
|
163 |
3 |
wikiTemplateManager.setTemplate(wikiId, value); |
164 |
|
|
165 |
2 |
return true; |
166 |
|
} catch (WikiTemplateManagerException e) { |
167 |
1 |
error(String.format("Failed to set the template value [%s] for the wiki [%s].", value, wikiId), e); |
168 |
1 |
return false; |
169 |
|
} catch (AccessDeniedException e) { |
170 |
2 |
error(String.format("Access denied for [%s] to change the template value of the wiki [%s]. The user has" |
171 |
|
+ " not the right to perform this operation or the script has not the programming right.", |
172 |
|
context.getUserReference(), wikiId), e); |
173 |
2 |
return false; |
174 |
|
} catch (WikiManagerException e) { |
175 |
1 |
error(String.format("Failed to get the descriptor of the wiki [%s].", wikiId), e); |
176 |
1 |
return false; |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
@param |
182 |
|
@return |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
184 |
3 |
public Boolean isTemplate(String wikiId)... |
185 |
|
{ |
186 |
3 |
try { |
187 |
3 |
return wikiTemplateManager.isTemplate(wikiId); |
188 |
|
} catch (WikiTemplateManagerException e) { |
189 |
1 |
error(String.format("Failed to get if the wiki [%s] is a template or not.", wikiId), e); |
190 |
1 |
return null; |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@param |
199 |
|
@param |
200 |
|
@param |
201 |
|
@param |
202 |
|
@return |
203 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.33 |
|
204 |
4 |
public boolean createWikiFromTemplate(String newWikiId, String newWikiAlias,... |
205 |
|
String templateId, String ownerId, boolean failOnExist) |
206 |
|
{ |
207 |
4 |
try { |
208 |
4 |
XWikiContext context = xcontextProvider.get(); |
209 |
|
|
210 |
4 |
authorizationManager.checkAccess(Right.PROGRAM, context.getDoc().getAuthorReference(), |
211 |
|
context.getDoc().getDocumentReference()); |
212 |
|
|
213 |
3 |
authorizationManager.checkAccess(Right.CREATE_WIKI, context.getUserReference(), |
214 |
|
new WikiReference(context.getMainXWiki())); |
215 |
|
|
216 |
|
|
217 |
2 |
wikiTemplateManager.createWikiFromTemplate(newWikiId, newWikiAlias, templateId, ownerId, |
218 |
|
failOnExist); |
219 |
1 |
return true; |
220 |
|
} catch (WikiTemplateManagerException e) { |
221 |
1 |
error("Failed to create the wiki from the template.", e); |
222 |
|
} catch (AccessDeniedException e) { |
223 |
2 |
error("Error, you or this script does not have the right to create a wiki from a template.", e); |
224 |
|
} |
225 |
3 |
return false; |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
@param |
232 |
|
@param |
233 |
|
@see |
234 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
235 |
10 |
private void error(String errorMessage, Exception e)... |
236 |
|
{ |
237 |
|
|
238 |
10 |
logger.error(errorMessage, e); |
239 |
|
|
240 |
|
|
241 |
10 |
setLastError(e); |
242 |
|
|
243 |
10 |
this.execution.getContext().setProperty(CONTEXT_LASTEXCEPTION, e); |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
@return |
248 |
|
@deprecated@link |
249 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
250 |
1 |
@Deprecated... |
251 |
|
public Exception getLastException() |
252 |
|
{ |
253 |
1 |
return (Exception) this.execution.getContext().getProperty(CONTEXT_LASTEXCEPTION); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
@param |
260 |
|
@return |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
262 |
3 |
public JobStatus getWikiProvisioningJobStatus(List<String> jobId)... |
263 |
|
{ |
264 |
3 |
try { |
265 |
3 |
WikiProvisioningJob wikiProvisioningJob = wikiTemplateManager.getWikiProvisioningJob(jobId); |
266 |
2 |
if (wikiProvisioningJob == null) { |
267 |
1 |
return null; |
268 |
|
} |
269 |
1 |
return wikiProvisioningJob.getStatus(); |
270 |
|
} catch (WikiTemplateManagerException e) { |
271 |
1 |
error("Failed to get tge wiki provisioning job.", e); |
272 |
1 |
return null; |
273 |
|
} |
274 |
|
} |
275 |
|
} |