| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package com.xpn.xwiki.web; |
| 21 |
|
|
| 22 |
|
import javax.inject.Provider; |
| 23 |
|
import javax.script.ScriptContext; |
| 24 |
|
|
| 25 |
|
import org.apache.commons.lang3.StringUtils; |
| 26 |
|
import org.xwiki.model.reference.DocumentReference; |
| 27 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 28 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
| 29 |
|
import org.xwiki.model.reference.SpaceReference; |
| 30 |
|
import org.xwiki.security.authorization.ContextualAuthorizationManager; |
| 31 |
|
import org.xwiki.security.authorization.Right; |
| 32 |
|
|
| 33 |
|
import com.xpn.xwiki.XWiki; |
| 34 |
|
import com.xpn.xwiki.XWikiContext; |
| 35 |
|
import com.xpn.xwiki.XWikiException; |
| 36 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 37 |
|
import com.xpn.xwiki.objects.BaseObject; |
| 38 |
|
import com.xpn.xwiki.util.Util; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
@version |
| 44 |
|
@since |
| 45 |
|
|
| |
|
| 92.8% |
Uncovered Elements: 11 (153) |
Complexity: 33 |
Complexity Density: 0.33 |
|
| 46 |
|
public class CreateAction extends XWikiAction |
| 47 |
|
{ |
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
private static final String CREATE_TEMPLATE = "create"; |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private static final String PARENT = "parent"; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
private static final String SPACE_REFERENCE = "spaceReference"; |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
private static final String NAME = "name"; |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
private static final String TEMPLATE = "template"; |
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
private static final String IS_SPACE = "isSpace"; |
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 81 |
|
|
| 82 |
|
private static final String WEBHOME = "WebHome"; |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
private static final String LOCAL_SERIALIZER_HINT = "local"; |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
private static final String CURRENT_MIXED_RESOLVER_HINT = "currentmixed"; |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 97 |
44 |
public CreateAction()... |
| 98 |
|
{ |
| 99 |
44 |
this.waitForXWikiInitialization = false; |
| 100 |
|
} |
| 101 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (28) |
Complexity: 6 |
Complexity Density: 0.3 |
|
| 102 |
98 |
@Override... |
| 103 |
|
public String render(XWikiContext context) throws XWikiException |
| 104 |
|
{ |
| 105 |
98 |
CreateActionRequestHandler handler = new CreateActionRequestHandler(context); |
| 106 |
|
|
| 107 |
|
|
| 108 |
98 |
handler.processRequest(); |
| 109 |
|
|
| 110 |
|
|
| 111 |
98 |
ScriptContext scontext = getCurrentScriptContext(); |
| 112 |
98 |
scontext.setAttribute(SPACE_REFERENCE, handler.getSpaceReference(), ScriptContext.ENGINE_SCOPE); |
| 113 |
98 |
scontext.setAttribute(NAME, handler.getName(), ScriptContext.ENGINE_SCOPE); |
| 114 |
98 |
scontext.setAttribute(IS_SPACE, handler.isSpace(), ScriptContext.ENGINE_SCOPE); |
| 115 |
|
|
| 116 |
98 |
scontext.setAttribute("availableTemplateProviders", handler.getAvailableTemplateProviders(), |
| 117 |
|
ScriptContext.ENGINE_SCOPE); |
| 118 |
|
|
| 119 |
98 |
DocumentReference newDocumentReference = handler.getNewDocumentReference(); |
| 120 |
98 |
if (newDocumentReference == null) { |
| 121 |
|
|
| 122 |
22 |
return CREATE_TEMPLATE; |
| 123 |
|
} |
| 124 |
|
|
| 125 |
|
|
| 126 |
76 |
if (!handler.isTemplateProviderAllowedToCreateInCurrentSpace()) { |
| 127 |
|
|
| 128 |
|
|
| 129 |
3 |
return CREATE_TEMPLATE; |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
|
|
| 134 |
|
|
| 135 |
73 |
checkRights(newDocumentReference.getLastSpaceReference(), context); |
| 136 |
|
|
| 137 |
|
|
| 138 |
73 |
XWikiDocument newDocument = context.getWiki().getDocument(newDocumentReference, context); |
| 139 |
73 |
if (handler.isDocumentAlreadyExisting(newDocument)) { |
| 140 |
4 |
return CREATE_TEMPLATE; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
69 |
if (StringUtils.isBlank(handler.getType()) && !handler.hasTemplate()) { |
| 150 |
11 |
return CREATE_TEMPLATE; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
58 |
doCreate(context, newDocument, handler.isSpace(), handler.getTemplateProvider()); |
| 155 |
|
|
| 156 |
58 |
return null; |
| 157 |
|
} |
| 158 |
|
|
| 159 |
|
|
| 160 |
|
@param |
| 161 |
|
@param |
| 162 |
|
@throws |
| 163 |
|
|
| |
|
| 50% |
Uncovered Elements: 3 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 164 |
73 |
private void checkRights(SpaceReference spaceReference, XWikiContext context) throws XWikiException... |
| 165 |
|
{ |
| 166 |
73 |
ContextualAuthorizationManager authManager = Utils.getComponent(ContextualAuthorizationManager.class); |
| 167 |
73 |
if (!authManager.hasAccess(Right.EDIT, spaceReference)) { |
| 168 |
0 |
Object[] args = { spaceReference.toString(), context.getUser() }; |
| 169 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, |
| 170 |
|
"The creation of a document into the space {0} has been denied to user {1}", null, args); |
| 171 |
|
} |
| 172 |
|
} |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
|
| 177 |
|
@param |
| 178 |
|
@param |
| 179 |
|
@param |
| 180 |
|
@param |
| 181 |
|
@throws |
| 182 |
|
|
| |
|
| 89.5% |
Uncovered Elements: 4 (38) |
Complexity: 5 |
Complexity Density: 0.17 |
|
| 183 |
58 |
private void doCreate(XWikiContext context, XWikiDocument newDocument, boolean isSpace, BaseObject templateProvider)... |
| 184 |
|
throws XWikiException |
| 185 |
|
{ |
| 186 |
58 |
XWikiRequest request = context.getRequest(); |
| 187 |
58 |
XWikiDocument doc = context.getDoc(); |
| 188 |
|
|
| 189 |
|
|
| 190 |
58 |
DocumentReferenceResolver<String> resolver = |
| 191 |
|
Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, CURRENT_MIXED_RESOLVER_HINT); |
| 192 |
|
|
| 193 |
58 |
String parent = getParent(request, doc, isSpace, context); |
| 194 |
|
|
| 195 |
|
|
| 196 |
58 |
String title = getTitle(request, newDocument, isSpace); |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
58 |
String template = getTemplate(templateProvider, request); |
| 201 |
|
|
| 202 |
|
|
| 203 |
58 |
boolean toSave = getSaveBeforeEdit(templateProvider); |
| 204 |
|
|
| 205 |
58 |
String redirectParams = null; |
| 206 |
58 |
String editMode = null; |
| 207 |
58 |
if (toSave) { |
| 208 |
1 |
XWiki xwiki = context.getWiki(); |
| 209 |
|
|
| 210 |
1 |
DocumentReference templateReference = resolver.resolve(template); |
| 211 |
1 |
newDocument.readFromTemplate(templateReference, context); |
| 212 |
1 |
if (!StringUtils.isEmpty(parent)) { |
| 213 |
1 |
DocumentReference parentReference = resolver.resolve(parent); |
| 214 |
1 |
newDocument.setParentReference(parentReference); |
| 215 |
|
} |
| 216 |
1 |
if (title != null) { |
| 217 |
1 |
newDocument.setTitle(title); |
| 218 |
|
} |
| 219 |
1 |
DocumentReference currentUserReference = context.getUserReference(); |
| 220 |
1 |
newDocument.setAuthorReference(currentUserReference); |
| 221 |
1 |
newDocument.setCreatorReference(currentUserReference); |
| 222 |
|
|
| 223 |
1 |
xwiki.saveDocument(newDocument, context); |
| 224 |
1 |
editMode = newDocument.getDefaultEditMode(context); |
| 225 |
|
} else { |
| 226 |
|
|
| 227 |
57 |
redirectParams = getRedirectParameters(parent, title, template); |
| 228 |
|
|
| 229 |
|
|
| 230 |
57 |
editMode = getEditMode(template, resolver, context); |
| 231 |
|
} |
| 232 |
|
|
| 233 |
|
|
| 234 |
58 |
String redirectURL = newDocument.getURL(editMode, redirectParams, context); |
| 235 |
58 |
redirectURL = context.getResponse().encodeRedirectURL(redirectURL); |
| 236 |
58 |
if (context.getRequest().getParameterMap().containsKey("ajax")) { |
| 237 |
|
|
| 238 |
|
|
| 239 |
0 |
context.getResponse().setHeader("redirect", redirectURL); |
| 240 |
|
} else { |
| 241 |
|
|
| 242 |
58 |
sendRedirect(context.getResponse(), redirectURL); |
| 243 |
|
} |
| 244 |
|
} |
| 245 |
|
|
| 246 |
|
|
| 247 |
|
@param |
| 248 |
|
@param |
| 249 |
|
@param |
| 250 |
|
@param |
| 251 |
|
@return |
| 252 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 253 |
57 |
private String getRedirectParameters(String parent, String title, String template)... |
| 254 |
|
{ |
| 255 |
57 |
String redirectParams; |
| 256 |
57 |
redirectParams = "template=" + Util.encodeURI(template, null); |
| 257 |
57 |
if (parent != null) { |
| 258 |
57 |
redirectParams += "&parent=" + Util.encodeURI(parent, null); |
| 259 |
|
} |
| 260 |
57 |
if (title != null) { |
| 261 |
57 |
redirectParams += "&title=" + Util.encodeURI(title, null); |
| 262 |
|
} |
| 263 |
57 |
return redirectParams; |
| 264 |
|
} |
| 265 |
|
|
| 266 |
|
|
| 267 |
|
@param |
| 268 |
|
@param |
| 269 |
|
@return |
| 270 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 271 |
58 |
private String getTemplate(BaseObject templateProvider, XWikiRequest request)... |
| 272 |
|
{ |
| 273 |
58 |
String result = ""; |
| 274 |
|
|
| 275 |
58 |
if (templateProvider != null) { |
| 276 |
18 |
result = templateProvider.getStringValue(TEMPLATE); |
| 277 |
40 |
} else if (request.getParameter(TEMPLATE) != null) { |
| 278 |
5 |
result = request.getParameter(TEMPLATE); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
58 |
return result; |
| 282 |
|
} |
| 283 |
|
|
| 284 |
|
|
| 285 |
|
@param |
| 286 |
|
@param |
| 287 |
|
@param |
| 288 |
|
@param |
| 289 |
|
@return |
| 290 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 291 |
58 |
private String getParent(XWikiRequest request, XWikiDocument doc, boolean isSpace, XWikiContext context)... |
| 292 |
|
{ |
| 293 |
|
|
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
|
| 299 |
|
|
| 300 |
58 |
String parent = request.getParameter(PARENT); |
| 301 |
58 |
if (StringUtils.isEmpty(parent)) { |
| 302 |
53 |
EntityReferenceSerializer<String> localSerializer = |
| 303 |
|
Utils.getComponent(EntityReferenceSerializer.TYPE_STRING, LOCAL_SERIALIZER_HINT); |
| 304 |
|
|
| 305 |
53 |
if (doc.isNew()) { |
| 306 |
|
|
| 307 |
20 |
Provider<DocumentReference> defaultDocumentReferenceProvider = |
| 308 |
|
Utils.getComponent(DocumentReference.TYPE_PROVIDER); |
| 309 |
|
|
| 310 |
20 |
DocumentReference parentRef = |
| 311 |
|
defaultDocumentReferenceProvider.get().setWikiReference(context.getWikiReference()); |
| 312 |
|
|
| 313 |
20 |
parent = localSerializer.serialize(parentRef); |
| 314 |
|
} else { |
| 315 |
|
|
| 316 |
33 |
DocumentReference parentRef = doc.getDocumentReference(); |
| 317 |
|
|
| 318 |
33 |
parent = localSerializer.serialize(parentRef); |
| 319 |
|
} |
| 320 |
|
} |
| 321 |
|
|
| 322 |
58 |
return parent; |
| 323 |
|
} |
| 324 |
|
|
| 325 |
|
|
| 326 |
|
@param |
| 327 |
|
@param |
| 328 |
|
@param |
| 329 |
|
@return |
| 330 |
|
|
| 331 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
| 332 |
58 |
private String getTitle(XWikiRequest request, XWikiDocument newDocument, boolean isSpace)... |
| 333 |
|
{ |
| 334 |
58 |
String title = request.getParameter("title"); |
| 335 |
58 |
if (StringUtils.isEmpty(title)) { |
| 336 |
40 |
if (isSpace) { |
| 337 |
22 |
title = newDocument.getDocumentReference().getLastSpaceReference().getName(); |
| 338 |
|
} else { |
| 339 |
18 |
title = newDocument.getDocumentReference().getName(); |
| 340 |
|
|
| 341 |
18 |
if (WEBHOME.equals(title)) { |
| 342 |
0 |
title = newDocument.getDocumentReference().getLastSpaceReference().getName(); |
| 343 |
|
} |
| 344 |
|
} |
| 345 |
|
} |
| 346 |
|
|
| 347 |
58 |
return title; |
| 348 |
|
} |
| 349 |
|
|
| 350 |
|
|
| 351 |
|
@param |
| 352 |
|
@return |
| 353 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 354 |
58 |
boolean getSaveBeforeEdit(BaseObject templateProvider)... |
| 355 |
|
{ |
| 356 |
58 |
boolean toSave = false; |
| 357 |
|
|
| 358 |
58 |
if (templateProvider != null) { |
| 359 |
|
|
| 360 |
18 |
String action = templateProvider.getStringValue("action"); |
| 361 |
18 |
if ("saveandedit".equals(action)) { |
| 362 |
1 |
toSave = true; |
| 363 |
|
} |
| 364 |
|
} |
| 365 |
|
|
| 366 |
58 |
return toSave; |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
|
| 370 |
|
@param |
| 371 |
|
@param |
| 372 |
|
@param |
| 373 |
|
@return |
| 374 |
|
@throws |
| 375 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
| 376 |
57 |
private String getEditMode(String template, DocumentReferenceResolver<String> resolver, XWikiContext context)... |
| 377 |
|
throws XWikiException |
| 378 |
|
{ |
| 379 |
|
|
| 380 |
|
|
| 381 |
57 |
String editAction = "edit"; |
| 382 |
57 |
XWiki xwiki = context.getWiki(); |
| 383 |
57 |
if (!StringUtils.isEmpty(template)) { |
| 384 |
22 |
DocumentReference templateReference = resolver.resolve(template); |
| 385 |
22 |
if (xwiki.exists(templateReference, context)) { |
| 386 |
21 |
editAction = xwiki.getDocument(templateReference, context).getDefaultEditMode(context); |
| 387 |
|
} |
| 388 |
|
} |
| 389 |
|
|
| 390 |
57 |
return editAction; |
| 391 |
|
} |
| 392 |
|
} |