Search in sources :

Example 36 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DeleteAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    XWikiRequest request = context.getRequest();
    XWikiDocument doc = context.getDoc();
    String sindex = request.getParameter(RECYCLED_DOCUMENT_ID_PARAM);
    boolean recycleIdIsValid = false;
    if (sindex != null) {
        long index = Long.parseLong(sindex);
        if (context.getWiki().getRecycleBinStore().getDeletedDocument(index, context, true) != null) {
            recycleIdIsValid = true;
        }
    }
    if ("1".equals(request.getParameter(CONFIRM_PARAM))) {
        return "deleted";
    }
    if (doc.isNew() && !recycleIdIsValid) {
        return Utils.getPage(request, "docdoesnotexist");
    }
    return ACTION_NAME;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument)

Example 37 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DeleteAttachmentAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }
    XWikiRequest request = context.getRequest();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    XWikiAttachment attachment = null;
    XWiki xwiki = context.getWiki();
    String filename;
    // Delete from the trash
    if (request.getParameter("trashId") != null) {
        long trashId = NumberUtils.toLong(request.getParameter("trashId"));
        DeletedAttachment da = xwiki.getAttachmentRecycleBinStore().getDeletedAttachment(trashId, context, true);
        // don't try to delete it and instead redirect to the attachment list.
        if (da != null) {
            com.xpn.xwiki.api.DeletedAttachment daapi = new com.xpn.xwiki.api.DeletedAttachment(da, context);
            if (!daapi.canDelete()) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, "You are not allowed to delete an attachment from the trash " + "immediately after it has been deleted from the wiki");
            }
            if (!da.getDocName().equals(doc.getFullName())) {
                throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_URL_EXCEPTION, "The specified trash entry does not match the current document");
            }
            // TODO: Add a confirmation check
            xwiki.getAttachmentRecycleBinStore().deleteFromRecycleBin(trashId, context, true);
        }
        sendRedirect(response, Utils.getRedirect("attach", context));
        return false;
    }
    if (context.getMode() == XWikiContext.MODE_PORTLET) {
        filename = request.getParameter("filename");
    } else {
        // Note: We use getRequestURI() because the spec says the server doesn't decode it, as
        // we want to use our own decoding.
        String requestUri = request.getRequestURI();
        filename = getFileName();
    }
    XWikiDocument newdoc = doc.clone();
    // An attachment can be indicated either using an id, or using the filename.
    if (request.getParameter("id") != null) {
        int id = NumberUtils.toInt(request.getParameter("id"));
        if (newdoc.getAttachmentList().size() > id) {
            attachment = newdoc.getAttachmentList().get(id);
        }
    } else {
        attachment = newdoc.getAttachment(filename);
    }
    // No such attachment
    if (attachment == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        ScriptContext scriptContext = getCurrentScriptContext();
        if (scriptContext != null) {
            scriptContext.setAttribute("message", localizePlainOrKey("core.action.deleteAttachment.failed", filename), ScriptContext.ENGINE_SCOPE);
            scriptContext.setAttribute("details", localizePlainOrKey("platform.core.action.deleteAttachment.noAttachment"), ScriptContext.ENGINE_SCOPE);
        }
        return true;
    }
    newdoc.setAuthorReference(context.getUserReference());
    // Set "deleted attachment" as the version comment.
    String comment;
    if (attachment.isImage(context)) {
        comment = localizePlainOrKey("core.comment.deleteImageComment", filename);
    } else {
        comment = localizePlainOrKey("core.comment.deleteAttachmentComment", filename);
    }
    try {
        newdoc.removeAttachment(attachment);
        xwiki.saveDocument(newdoc, comment, context);
    } catch (Exception ex) {
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        ScriptContext scriptContext = getCurrentScriptContext();
        if (scriptContext != null) {
            scriptContext.setAttribute("message", localizePlainOrKey("core.action.deleteAttachment.failed", filename), ScriptContext.ENGINE_SCOPE);
            scriptContext.setAttribute("details", ExceptionUtils.getRootCauseMessage(ex), ScriptContext.ENGINE_SCOPE);
        }
        return true;
    }
    // forward to attach page
    if (!((Boolean) context.get("ajax")).booleanValue()) {
        String redirect = Utils.getRedirect("attach", context);
        sendRedirect(response, redirect);
    }
    return false;
}
Also used : XWiki(com.xpn.xwiki.XWiki) ScriptContext(javax.script.ScriptContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DeletedAttachment(com.xpn.xwiki.doc.DeletedAttachment) XWikiException(com.xpn.xwiki.XWikiException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiException(com.xpn.xwiki.XWikiException)

Example 38 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class DeleteVersionsAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    DeleteVersionsForm form = (DeleteVersionsForm) context.getForm();
    if (!form.isConfirmed() || !csrfTokenCheck(context)) {
        return true;
    }
    XWikiDocument doc = context.getDoc();
    String language = form.getLanguage();
    XWikiDocument tdoc = doc.getTranslatedDocument(language, context);
    XWikiDocumentArchive archive = tdoc.getDocumentArchive(context);
    // Get the versions
    Version[] versions = getVersionsFromForm(form, archive);
    Version v1 = versions[0];
    Version v2 = versions[1];
    if (v1 != null && v2 != null) {
        // Remove the versions
        archive.removeVersions(v1, v2, context);
        context.getWiki().getVersioningStore().saveXWikiDocArchive(archive, true, context);
        tdoc.setDocumentArchive(archive);
        // Is this the last remaining version? If so, then recycle the document.
        if (archive.getLatestVersion() == null) {
            // Wrap the work as a batch operation.
            BatchOperationExecutor batchOperationExecutor = Utils.getComponent(BatchOperationExecutor.class);
            batchOperationExecutor.execute(() -> {
                if (StringUtils.isEmpty(language) || language.equals(doc.getDefaultLanguage())) {
                    context.getWiki().deleteAllDocuments(doc, context);
                } else {
                    // Only delete the translation
                    context.getWiki().deleteDocument(tdoc, context);
                }
            });
        } else {
            // If we delete the most recent (current) version, then rollback to latest undeleted version.
            if (!tdoc.getRCSVersion().equals(archive.getLatestVersion())) {
                XWikiDocument newdoc = archive.loadDocument(archive.getLatestVersion(), context);
                // Reset the document reference, since the one taken from the archive might be wrong (old name from
                // before a rename)
                newdoc.setDocumentReference(tdoc.getDocumentReference());
                // Get rid of objects that don't exist in new version
                newdoc.addXObjectsToRemoveFromVersion(tdoc);
                // Make sure we don't create a new rev!
                newdoc.setMetaDataDirty(false);
                newdoc.setContentDirty(false);
                // Make sure the previous current document is seen as original document of
                // the new current document for comparisons
                newdoc.setOriginalDocument(tdoc.getOriginalDocument());
                // Update the database with what is now the current document
                context.getWiki().saveDocument(newdoc, newdoc.getComment(), context);
                context.setDoc(newdoc);
            }
        }
    }
    sendRedirect(context);
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) Version(org.suigeneris.jrcs.rcs.Version) BatchOperationExecutor(org.xwiki.refactoring.batch.BatchOperationExecutor)

Example 39 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class XWikiAuthServiceImpl method createUser.

protected String createUser(String user, XWikiContext context) throws XWikiException {
    String createuser = getParam("auth_createuser", context);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Create user param is " + createuser);
    }
    if (createuser != null) {
        String wikiname = context.getWiki().clearName(user, true, true, context);
        XWikiDocument userdoc = context.getWiki().getDocument(new DocumentReference(context.getWikiId(), "XWiki", wikiname), context);
        if (userdoc.isNew()) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("User page does not exist for user " + user);
            }
            if ("empty".equals(createuser)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Creating emptry user for user " + user);
                }
                context.getWiki().createEmptyUser(wikiname, "edit", context);
            }
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("User page already exists for user " + user);
            }
        }
        return wikiname;
    }
    return user;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 40 with XWikiDocument

use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.

the class XWikiAuthServiceImpl method checkPassword.

protected boolean checkPassword(String username, String password, XWikiContext context) throws XWikiException {
    long time = System.currentTimeMillis();
    try {
        boolean result = false;
        final XWikiDocument doc = context.getWiki().getDocument(username, context);
        final BaseObject userObject = doc.getXObject(USERCLASS_REFERENCE);
        // We only allow empty password from users having a XWikiUsers object.
        if (userObject != null) {
            final String stored = userObject.getStringValue("password");
            result = new PasswordClass().getEquivalentPassword(stored, password).equals(stored);
        }
        if (LOGGER.isDebugEnabled()) {
            if (result) {
                LOGGER.debug("Password check for user " + username + " successful");
            } else {
                LOGGER.debug("Password check for user " + username + " failed");
            }
            LOGGER.debug((System.currentTimeMillis() - time) + " milliseconds spent validating password.");
        }
        return result;
    } catch (Throwable e) {
        LOGGER.error("Failed to check password", e);
        return false;
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PasswordClass(com.xpn.xwiki.objects.classes.PasswordClass) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23