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

File WikiCreationJobScriptServices.java

 

Coverage histogram

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

Code metrics

6
21
7
1
176
94
13
0.62
3
7
1.86

Classes

Class Line # Actions
WikiCreationJobScriptServices 56 21 0% 13 2
0.941176594.1%
 

Contributing tests

This file is covered by 5 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.platform.wiki.creationjob.script;
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.component.annotation.Component;
30    import org.xwiki.context.Execution;
31    import org.xwiki.extension.ExtensionId;
32    import org.xwiki.extension.distribution.internal.DistributionManager;
33    import org.xwiki.job.Job;
34    import org.xwiki.job.event.status.JobStatus;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.platform.wiki.creationjob.WikiCreationRequest;
37    import org.xwiki.platform.wiki.creationjob.WikiCreator;
38    import org.xwiki.platform.wiki.creationjob.WikiCreationException;
39    import org.xwiki.script.service.ScriptService;
40    import org.xwiki.security.authorization.AccessDeniedException;
41    import org.xwiki.security.authorization.AuthorizationManager;
42    import org.xwiki.security.authorization.Right;
43    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
44   
45    import com.xpn.xwiki.XWikiContext;
46   
47    /**
48    * Script services for the creation of wikis.
49    *
50    * @version $Id: cff08628458824ccf4b2c5e7882c2f78b748dc2d $
51    * @since 7.0M2
52    */
53    @Component
54    @Singleton
55    @Named("wiki.creationjob")
 
56    public class WikiCreationJobScriptServices implements ScriptService
57    {
58    /**
59    * The key under which the last encountered error is stored in the current execution context.
60    */
61    private static final String ERROR_KEY = "scriptservice.wikicreationjob.error";
62   
63    @Inject
64    private WikiCreator wikiCreator;
65   
66    @Inject
67    private Execution execution;
68   
69    @Inject
70    private AuthorizationManager authorizationManager;
71   
72    @Inject
73    private WikiDescriptorManager wikiDescriptorManager;
74   
75    @Inject
76    private Provider<XWikiContext> xcontextProvider;
77   
78    @Inject
79    private DistributionManager distributionManager;
80   
81    @Inject
82    private Logger logger;
83   
84    /**
85    * Asynchronously create a wiki.
86    *
87    * @param request creation wiki request containing all information about the wiki to create
88    * @return the creationjob that creates the wiki
89    */
 
90  6 toggle public Job createWiki(WikiCreationRequest request)
91    {
92  6 try {
93    // Verify that the user has the CREATE_WIKI right
94  6 XWikiContext xcontext = xcontextProvider.get();
95  6 WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId());
96  6 authorizationManager.checkAccess(Right.CREATE_WIKI, xcontext.getUserReference(), mainWikiReference);
97   
98    // Verify that if an extension id is provided, this extension is authorized.
99  5 if (request.getExtensionId() != null) {
100  3 if (!isAuthorizedExtension(request.getExtensionId())) {
101  1 throw new WikiCreationException(String.format("The extension [%s] is not authorized.",
102    request.getExtensionId()));
103    }
104    }
105  4 return wikiCreator.createWiki(request);
106   
107    } catch (WikiCreationException e) {
108  1 setLastError(e);
109  1 logger.warn("Failed to create a new wiki.", e);
110    } catch (AccessDeniedException e) {
111  1 setLastError(e);
112    }
113   
114  2 return null;
115    }
116   
 
117  3 toggle private boolean isAuthorizedExtension(ExtensionId extensionId)
118    {
119  3 ExtensionId defaultExtension = getDefaultWikiExtensionId();
120  3 if (defaultExtension != null && StringUtils.isNotBlank(defaultExtension.getId())) {
121    // For now, only the extension declared in the WAR is authorized
122  3 return defaultExtension.equals(extensionId);
123    } else {
124    // Unless there is nothing set, and so we allow everything
125    // TODO: work with flavor manager instead
126  0 return true;
127    }
128    }
129   
130    /**
131    * @return the extension id of the default flavor
132    */
 
133  4 toggle public ExtensionId getDefaultWikiExtensionId()
134    {
135  4 return distributionManager.getWikiUIExtensionId();
136    }
137   
138    /**
139    * @param wikiId id of the wiki
140    * @return the creationjob status corresponding to the creation of the wiki
141    */
 
142  75 toggle public JobStatus getJobStatus(String wikiId)
143    {
144  75 return wikiCreator.getJobStatus(wikiId);
145    }
146   
147    /**
148    * @return a new request for the creation of a new wiki
149    */
 
150  4 toggle public WikiCreationRequest newWikiCreationRequest()
151    {
152  4 return new WikiCreationRequest();
153    }
154   
155    /**
156    * Get the error generated while performing the previously called action.
157    * @return an eventual exception or {@code null} if no exception was thrown
158    * @since 1.1
159    */
 
160  3 toggle public Exception getLastError()
161    {
162  3 return (Exception) this.execution.getContext().getProperty(ERROR_KEY);
163    }
164   
165    /**
166    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
167    *
168    * @param e the exception to store, can be {@code null} to clear the previously stored exception
169    * @see #getLastError()
170    * @since 1.1
171    */
 
172  2 toggle private void setLastError(Exception e)
173    {
174  2 this.execution.getContext().setProperty(ERROR_KEY, e);
175    }
176    }