1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.template.script

File WikiTemplateManagerScript.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
47
9
1
275
145
19
0.4
5.22
9
2.11

Classes

Class Line # Actions
WikiTemplateManagerScript 59 47 0% 19 0
1.0100%
 

Contributing tests

This file is covered by 17 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Script service to manage the wiki templates.
52    *
53    * @since 5.3M2
54    * @version $Id: 587330dcf8fa2ce538a7c0b1a528cb46d61b7659 $
55    */
56    @Component
57    @Named("wiki.template")
58    @Singleton
 
59    public class WikiTemplateManagerScript implements ScriptService
60    {
61    /**
62    * Field name of the last API exception inserted in context.
63    */
64    @Deprecated
65    public static final String CONTEXT_LASTEXCEPTION = "lastexception";
66   
67    /**
68    * The key under which the last encountered error is stored in the current execution context.
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    * Used to access current {@link com.xpn.xwiki.XWikiContext}.
86    */
87    @Inject
88    private Provider<XWikiContext> xcontextProvider;
89   
90    /**
91    * Execution context.
92    */
93    @Inject
94    private Execution execution;
95   
96    /**
97    * Logging tool.
98    */
99    @Inject
100    private Logger logger;
101   
102    /**
103    * Get the error generated while performing the previously called action.
104    *
105    * @return an eventual exception or {@code null} if no exception was thrown
106    */
 
107  10 toggle public Exception getLastError()
108    {
109  10 return (Exception) this.execution.getContext().getProperty(WIKITEMPLATEERROR_KEY);
110    }
111   
112    /**
113    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
114    *
115    * @param e the exception to store, can be {@code null} to clear the previously stored exception
116    * @see #getLastError()
117    */
 
118  10 toggle private void setLastError(Exception e)
119    {
120  10 this.execution.getContext().setProperty(WIKITEMPLATEERROR_KEY, e);
121    }
122   
123    /**
124    * Get the list of all wiki templates.
125    *
126    * @return list of wiki templates
127    */
 
128  6 toggle 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    * Set if the specified wiki is a template or not.
140    *
141    * @param wikiId the ID of the wiki to specify
142    * @param value whether or not the wiki is a template
143    * @return true if the action succeed
144    */
 
145  6 toggle public boolean setTemplate(String wikiId, boolean value)
146    {
147  6 XWikiContext context = xcontextProvider.get();
148  6 try {
149    // Check if the current script has the programing rights
150  6 authorizationManager.checkAccess(Right.PROGRAM, context.getDoc().getAuthorReference(),
151    context.getDoc().getDocumentReference());
152    // Get the descriptor
153  5 WikiDescriptor descriptor = wikiDescriptorManager.getById(wikiId);
154    // Get the wiki owner
155  4 String owner = descriptor.getOwnerId();
156    // Check right access
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    // Do the job
163  3 wikiTemplateManager.setTemplate(wikiId, value);
164    // Return success
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 wikiId The id of the wiki to test
182    * @return if the wiki is a template or not (or null if problems occur)
183    */
 
184  3 toggle 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    * Create a new wiki from the specified template.
196    *
197    * @param newWikiId ID of the wiki to create
198    * @param newWikiAlias Default alias of the wiki to create
199    * @param templateId Id of the template to use
200    * @param ownerId Id of the wiki owner
201    * @param failOnExist fail the creation of the wiki id if not available
202    * @return true if it succeed
203    */
 
204  4 toggle 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    // Check if the current script has the programing rights
210  4 authorizationManager.checkAccess(Right.PROGRAM, context.getDoc().getAuthorReference(),
211    context.getDoc().getDocumentReference());
212    // Check if the user has the right
213  3 authorizationManager.checkAccess(Right.CREATE_WIKI, context.getUserReference(),
214    new WikiReference(context.getMainXWiki()));
215   
216    // Do the job
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    * Log exception and store it in the context.
230    *
231    * @param errorMessage error message
232    * @param e the caught exception
233    * @see #CONTEXT_LASTEXCEPTION
234    */
 
235  10 toggle private void error(String errorMessage, Exception e)
236    {
237    // Log exception.
238  10 logger.error(errorMessage, e);
239   
240    // Store exception in context.
241  10 setLastError(e);
242    // Deprecated but still usable
243  10 this.execution.getContext().setProperty(CONTEXT_LASTEXCEPTION, e);
244    }
245   
246    /**
247    * @return the last exception, or null if there is not.
248    * @deprecated since 5.4RC1 use {@link #getLastError()} ()} instead
249    */
 
250  1 toggle @Deprecated
251    public Exception getLastException()
252    {
253  1 return (Exception) this.execution.getContext().getProperty(CONTEXT_LASTEXCEPTION);
254    }
255   
256    /**
257    * Get the status of the wiki creation job.
258    *
259    * @param jobId id of the provisioning job.
260    * @return the status of the job
261    */
 
262  3 toggle 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    }