Search in sources :

Example 1 with PropertyInterface

use of com.xpn.xwiki.objects.PropertyInterface in project xwiki-platform by xwiki.

the class XWiki method validateUser.

public int validateUser(boolean withConfirmEmail, XWikiContext context) throws XWikiException {
    try {
        XWikiRequest request = context.getRequest();
        // Get the user document
        String username = convertUsername(request.getParameter("xwikiname"), context);
        if (username.indexOf('.') == -1) {
            username = "XWiki." + username;
        }
        XWikiDocument userDocument = getDocument(username, context);
        // Get the stored validation key
        BaseObject userObject = userDocument.getObject("XWiki.XWikiUsers", 0);
        String storedKey = userObject.getStringValue("validkey");
        // Get the validation key from the URL
        String validationKey = request.getParameter("validkey");
        PropertyInterface validationKeyClass = getClass("XWiki.XWikiUsers", context).get("validkey");
        if (validationKeyClass instanceof PasswordClass) {
            validationKey = ((PasswordClass) validationKeyClass).getEquivalentPassword(storedKey, validationKey);
        }
        // Compare the two keys
        if ((!storedKey.equals("") && (storedKey.equals(validationKey)))) {
            userObject.setIntValue("active", 1);
            saveDocument(userDocument, context);
            if (withConfirmEmail) {
                String email = userObject.getStringValue("email");
                String password = userObject.getStringValue("password");
                sendValidationEmail(username, password, email, request.getParameter("validkey"), "confirmation_email_content", context);
            }
            return 0;
        } else {
            return -1;
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_VALIDATE_USER, "Exception while validating user", e, null);
    }
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) PasswordClass(com.xpn.xwiki.objects.classes.PasswordClass) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) IOException(java.io.IOException) JobException(org.xwiki.job.JobException) ParseException(org.xwiki.rendering.parser.ParseException) QueryException(org.xwiki.query.QueryException) URIException(org.apache.commons.httpclient.URIException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) NamingException(javax.naming.NamingException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 2 with PropertyInterface

use of com.xpn.xwiki.objects.PropertyInterface in project xwiki-platform by xwiki.

the class XWikiStats method toXML.

@Override
public // TODO: implement an EntityEventGenerator for XWikiStats
Element toXML(BaseClass bclass) {
    Element oel = new DOMElement(XMLNODE_OBJECT);
    // Add Class
    if (bclass != null) {
        if (bclass.getFieldList().size() > 0) {
            oel.add(bclass.toXML());
        }
    }
    Element el = new DOMElement(XMLNODE_NAME);
    el.addText(getName());
    oel.add(el);
    el = new DOMElement(XMLNODE_NUMBER);
    el.addText(getNumber() + "");
    oel.add(el);
    el = new DOMElement(XMLNODE_CLASSNAME);
    el.addText(getClassName());
    oel.add(el);
    for (Iterator<?> it = getFieldList().iterator(); it.hasNext(); ) {
        Element pel = new DOMElement(XMLNODE_PROPERTY);
        PropertyInterface bprop = (PropertyInterface) it.next();
        pel.add(bprop.toXML());
        oel.add(pel);
    }
    return oel;
}
Also used : PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) Element(org.dom4j.Element) DOMElement(org.dom4j.dom.DOMElement) DOMElement(org.dom4j.dom.DOMElement)

Example 3 with PropertyInterface

use of com.xpn.xwiki.objects.PropertyInterface in project xwiki-platform by xwiki.

the class DefaultClassPropertyValuesProvider method getPropertyType.

private String getPropertyType(ClassPropertyReference propertyReference) throws XWikiRestException {
    try {
        XWikiContext xcontext = this.xcontextProvider.get();
        XWikiDocument document = xcontext.getWiki().getDocument(propertyReference.getParent(), xcontext);
        BaseClass xclass = document.getXClass();
        PropertyInterface xproperty = xclass.get(propertyReference.getName());
        if (xproperty instanceof PropertyClass) {
            return ((PropertyClass) xproperty).getClassType();
        } else {
            throw new XWikiRestException(String.format("No such property [%s].", this.entityReferenceSerializer.serialize(propertyReference)));
        }
    } catch (XWikiException e) {
        throw new XWikiRestException(String.format("Failed to determine the property type for [{}].", this.entityReferenceSerializer.serialize(propertyReference)));
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) XWikiRestException(org.xwiki.rest.XWikiRestException) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiContext(com.xpn.xwiki.XWikiContext) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with PropertyInterface

use of com.xpn.xwiki.objects.PropertyInterface in project xwiki-platform by xwiki.

the class XWikiDocument method merge.

/**
 * Apply a 3 ways merge on the current document based on provided previous and new version of the document.
 * <p>
 * All 3 documents are supposed to have the same document reference and language already since that's what makes
 * them uniques.
 *
 * @param previousDocument the previous version of the document
 * @param newDocument the next version of the document
 * @param configuration the configuration of the merge indicates how to deal with some conflicts use cases, etc.
 * @param context the XWiki context
 * @return a repport of what happen during the merge (errors, etc.)
 * @since 3.2M1
 */
public MergeResult merge(XWikiDocument previousDocument, XWikiDocument newDocument, MergeConfiguration configuration, XWikiContext context) {
    MergeResult mergeResult = new MergeResult();
    // Title
    setTitle(MergeUtils.mergeOject(previousDocument.getTitle(), newDocument.getTitle(), getTitle(), mergeResult));
    // Content
    setContent(MergeUtils.mergeLines(previousDocument.getContent(), newDocument.getContent(), getContent(), mergeResult));
    // Syntax
    setSyntax(MergeUtils.mergeOject(previousDocument.getSyntax(), newDocument.getSyntax(), getSyntax(), mergeResult));
    // Default locale
    setDefaultLocale(MergeUtils.mergeOject(previousDocument.getDefaultLocale(), newDocument.getDefaultLocale(), getDefaultLocale(), mergeResult));
    // Parent
    setParentReference(MergeUtils.mergeOject(previousDocument.getRelativeParentReference(), newDocument.getRelativeParentReference(), getRelativeParentReference(), mergeResult));
    // DefaultTemplate
    setDefaultTemplate(MergeUtils.mergeOject(previousDocument.getDefaultTemplate(), newDocument.getDefaultTemplate(), getDefaultTemplate(), mergeResult));
    // Hidden
    setHidden(MergeUtils.mergeOject(previousDocument.isHidden(), newDocument.isHidden(), isHidden(), mergeResult));
    // CustomClass
    setCustomClass(MergeUtils.mergeLines(previousDocument.getCustomClass(), newDocument.getCustomClass(), getCustomClass(), mergeResult));
    // ValidationScript
    setValidationScript(MergeUtils.mergeLines(previousDocument.getValidationScript(), newDocument.getValidationScript(), getValidationScript(), mergeResult));
    // Objects
    List<List<ObjectDiff>> objectsDiff = previousDocument.getObjectDiff(previousDocument, newDocument, context);
    if (!objectsDiff.isEmpty()) {
        // Apply diff on result
        for (List<ObjectDiff> objectClassDiff : objectsDiff) {
            for (ObjectDiff diff : objectClassDiff) {
                BaseObject objectResult = getXObject(diff.getXClassReference(), diff.getNumber());
                BaseObject previousObject = previousDocument.getXObject(diff.getXClassReference(), diff.getNumber());
                BaseObject newObject = newDocument.getXObject(diff.getXClassReference(), diff.getNumber());
                PropertyInterface propertyResult = objectResult != null ? objectResult.getField(diff.getPropName()) : null;
                PropertyInterface previousProperty = previousObject != null ? previousObject.getField(diff.getPropName()) : null;
                PropertyInterface newProperty = newObject != null ? newObject.getField(diff.getPropName()) : null;
                if (diff.getAction() == ObjectDiff.ACTION_OBJECTADDED) {
                    if (objectResult == null) {
                        setXObject(newObject.getNumber(), configuration.isProvidedVersionsModifiables() ? newObject : newObject.clone());
                        mergeResult.setModified(true);
                    } else {
                        // collision between DB and new: object to add but already exists in the DB
                        mergeResult.getLog().error("Collision found on object [{}]", objectResult.getReference());
                    }
                } else if (diff.getAction() == ObjectDiff.ACTION_OBJECTREMOVED) {
                    if (objectResult != null) {
                        if (objectResult.equals(previousObject)) {
                            removeXObject(objectResult);
                            mergeResult.setModified(true);
                        } else {
                            // collision between DB and new: object to remove but not the same as previous
                            // version
                            mergeResult.getLog().error("Collision found on object [{}]", objectResult.getReference());
                        }
                    } else {
                        // Already removed from DB, lets assume the user is prescient
                        mergeResult.getLog().warn("Object [{}] already removed", previousObject.getReference());
                    }
                } else if (previousObject != null && newObject != null) {
                    if (objectResult != null) {
                        if (diff.getAction() == ObjectDiff.ACTION_PROPERTYADDED) {
                            if (propertyResult == null) {
                                objectResult.safeput(diff.getPropName(), newProperty);
                                mergeResult.setModified(true);
                            } else {
                                // collision between DB and new: property to add but already exists in the DB
                                mergeResult.getLog().error("Collision found on object property [{}]", propertyResult.getReference());
                            }
                        } else if (diff.getAction() == ObjectDiff.ACTION_PROPERTYREMOVED) {
                            if (propertyResult != null) {
                                if (propertyResult.equals(previousProperty)) {
                                    objectResult.removeField(diff.getPropName());
                                    mergeResult.setModified(true);
                                } else {
                                    // collision between DB and new: supposed to be removed but the DB version is
                                    // not the same as the previous version
                                    mergeResult.getLog().error("Collision found on object property [{}]", propertyResult.getReference());
                                }
                            } else {
                                // Already removed from DB, lets assume the user is prescient
                                mergeResult.getLog().warn("Object property [{}] already removed", previousProperty.getReference());
                            }
                        } else if (diff.getAction() == ObjectDiff.ACTION_PROPERTYCHANGED) {
                            if (propertyResult != null) {
                                if (propertyResult.equals(previousProperty)) {
                                    objectResult.safeput(diff.getPropName(), newProperty);
                                    mergeResult.setModified(true);
                                } else {
                                    // Try to apply a 3 ways merge on the property
                                    propertyResult.merge(previousProperty, newProperty, configuration, context, mergeResult);
                                }
                            } else {
                                // collision between DB and new: property to modify but does not exists in DB
                                // Lets assume it's a mistake to fix
                                mergeResult.getLog().warn("Object [{}] does not exists", newProperty.getReference());
                                objectResult.safeput(diff.getPropName(), newProperty);
                                mergeResult.setModified(true);
                            }
                        }
                    } else {
                        // Object explitely removed from the DB, lets assume we don't care about the changes
                        mergeResult.getLog().warn("Object [{}] already removed", previousObject.getReference());
                    }
                }
            }
        }
    }
    // Class
    BaseClass classResult = getXClass();
    BaseClass previousClass = previousDocument.getXClass();
    BaseClass newClass = newDocument.getXClass();
    classResult.merge(previousClass, newClass, configuration, context, mergeResult);
    // Attachments
    List<AttachmentDiff> attachmentsDiff = previousDocument.getAttachmentDiff(previousDocument, newDocument, context);
    if (!attachmentsDiff.isEmpty()) {
        // Apply deleted attachment diff on result (new attachment has already been saved)
        for (AttachmentDiff diff : attachmentsDiff) {
            XWikiAttachment previousAttachment = diff.getOrigAttachment();
            XWikiAttachment nextAttachment = diff.getNewAttachment();
            XWikiAttachment attachment = getAttachment(diff.getFileName());
            switch(diff.getType()) {
                case DELETE:
                    if (attachment != null) {
                        try {
                            if (attachment.equalsData(previousAttachment, context)) {
                                removeAttachment(attachment);
                                mergeResult.setModified(true);
                            } else {
                                // collision between DB and new: attachment modified by user
                                mergeResult.getLog().error("Collision found on attachment [{}]", attachment.getReference());
                            }
                        } catch (XWikiException e) {
                            mergeResult.getLog().error("Failed to compare attachments with reference [{}]", attachment.getReference());
                        }
                    } else {
                        // Already removed from DB, lets assume the user is prescient
                        mergeResult.getLog().warn("Attachment [{}] already removed", previousAttachment.getReference());
                    }
                    break;
                case INSERT:
                    if (attachment != null) {
                        try {
                            if (!attachment.equalsData(nextAttachment, context)) {
                                // collision between DB and new: attachment to add but a different one already
                                // exists in the DB
                                mergeResult.getLog().error("Collision found on attachment [{}]", attachment.getReference());
                            } else {
                                // Already added to the DB, lets assume the user is prescient
                                mergeResult.getLog().warn("Attachment [{}] already added", previousAttachment.getReference());
                            }
                        } catch (XWikiException e) {
                            mergeResult.getLog().error("Failed to compare attachments with reference [{}]", attachment.getReference());
                        }
                    } else {
                        addAttachment(configuration.isProvidedVersionsModifiables() ? nextAttachment : (XWikiAttachment) nextAttachment.clone());
                        mergeResult.setModified(true);
                    }
                    break;
                case CHANGE:
                    if (attachment != null) {
                        attachment.merge(previousAttachment, nextAttachment, configuration, context, mergeResult);
                    } else {
                        // collision between DB and new: attachment modified but does not exist in the DB
                        mergeResult.getLog().error("Collision found on attachment [{}]", previousAttachment.getReference());
                    }
                    break;
                default:
                    break;
            }
        }
    }
    return mergeResult;
}
Also used : ObjectDiff(com.xpn.xwiki.objects.ObjectDiff) PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) MergeResult(com.xpn.xwiki.doc.merge.MergeResult) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) XWikiAttachmentList(com.xpn.xwiki.internal.doc.XWikiAttachmentList) ArrayList(java.util.ArrayList) List(java.util.List) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 5 with PropertyInterface

use of com.xpn.xwiki.objects.PropertyInterface in project xwiki-platform by xwiki.

the class XWikiPreferencesDocumentInitializer method updateDocument.

@Override
public boolean updateDocument(XWikiDocument document) {
    boolean needsUpdate = super.updateDocument(document);
    // This one should not be in the prefs
    BaseClass xclass = document.getXClass();
    PropertyInterface baseskinProp = xclass.get("baseskin");
    if (baseskinProp != null) {
        xclass.removeField("baseskin");
        needsUpdate = true;
    }
    return needsUpdate;
}
Also used : PropertyInterface(com.xpn.xwiki.objects.PropertyInterface) BaseClass(com.xpn.xwiki.objects.classes.BaseClass)

Aggregations

PropertyInterface (com.xpn.xwiki.objects.PropertyInterface)9 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)4 XWikiException (com.xpn.xwiki.XWikiException)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ObjectDiff (com.xpn.xwiki.objects.ObjectDiff)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2 MergeResult (com.xpn.xwiki.doc.merge.MergeResult)1 XWikiAttachmentList (com.xpn.xwiki.internal.doc.XWikiAttachmentList)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 PasswordClass (com.xpn.xwiki.objects.classes.PasswordClass)1 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)1 XWikiRequest (com.xpn.xwiki.web.XWikiRequest)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1