Search in sources :

Example 26 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class XWikiRightServiceImpl method checkAccess.

@Override
public boolean checkAccess(String action, XWikiDocument doc, XWikiContext context) throws XWikiException {
    LOGGER.debug("checkAccess for [{}], [{}]", action, doc);
    String username = null;
    XWikiUser user = null;
    boolean needsAuth = false;
    String right = getRight(action);
    if (right.equals("login")) {
        user = context.getWiki().checkAuth(context);
        if (user == null) {
            username = XWikiRightService.GUEST_USER_FULLNAME;
        } else {
            username = user.getUser();
        }
        // Save the user
        context.setUser(username);
        logAllow(username, doc.getFullName(), action, "login/logout pages");
        return true;
    }
    if (right.equals("delete")) {
        user = context.getWiki().checkAuth(context);
        String creator = doc.getCreator();
        if ((user != null) && (user.getUser() != null) && (creator != null)) {
            if (user.getUser().equals(creator)) {
                context.setUser(user.getUser());
                return true;
            }
        }
    }
    // We do not need to authenticate twice
    // This seems to cause a problem in virtual wikis
    user = context.getXWikiUser();
    if (user == null) {
        needsAuth = needsAuth(right, context);
        try {
            if (context.getMode() != XWikiContext.MODE_XMLRPC) {
                user = context.getWiki().checkAuth(context);
            } else {
                user = new XWikiUser(context.getUser());
            }
            if ((user == null) && (needsAuth)) {
                logDeny("unauthentified", doc.getFullName(), action, "Authentication needed");
                if (context.getRequest() != null) {
                    if (!context.getWiki().Param("xwiki.hidelogin", "false").equalsIgnoreCase("true")) {
                        context.getWiki().getAuthService().showLogin(context);
                    }
                }
                return false;
            }
        } catch (XWikiException e) {
            if (needsAuth) {
                throw e;
            }
        }
        if (user == null) {
            username = XWikiRightService.GUEST_USER_FULLNAME;
        } else {
            username = user.getUser();
        }
        // Save the user
        context.setUser(username);
    } else {
        username = user.getUser();
    }
    // Check Rights
    try {
        // Verify access rights and return if ok
        String docname;
        if (context.getWikiId() != null) {
            docname = context.getWikiId() + ":" + doc.getFullName();
            if (username.indexOf(":") == -1) {
                username = context.getWikiId() + ":" + username;
            }
        } else {
            docname = doc.getFullName();
        }
        if (context.getWiki().getRightService().hasAccessLevel(right, username, docname, context)) {
            logAllow(username, docname, action, "access manager granted right");
            return true;
        }
    } catch (Exception e) {
        // This should not happen..
        logDeny(username, doc.getFullName(), action, "access manager exception " + e.getMessage());
        e.printStackTrace();
        return false;
    }
    if (user == null) {
        // Denied Guest need to be authenticated
        logDeny("unauthentified", doc.getFullName(), action, "Guest has been denied");
        if (context.getRequest() != null && !context.getWiki().Param("xwiki.hidelogin", "false").equalsIgnoreCase("true")) {
            context.getWiki().getAuthService().showLogin(context);
        }
        return false;
    } else {
        logDeny(username, doc.getFullName(), action, "access manager denied right");
        return false;
    }
}
Also used : XWikiUser(com.xpn.xwiki.user.api.XWikiUser) XWikiException(com.xpn.xwiki.XWikiException) XWikiException(com.xpn.xwiki.XWikiException) XWikiRightNotFoundException(com.xpn.xwiki.user.api.XWikiRightNotFoundException)

Example 27 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class EditAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    try {
        XWikiDocument editedDocument = prepareEditedDocument(context);
        maybeLockDocument(editedDocument, context);
    } catch (XWikiException e) {
        if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) {
            context.put("exception", e);
            return "docalreadyexists";
        } else {
            throw e;
        }
    }
    // Make sure object property fields are displayed in edit mode.
    // See XWikiDocument#display(String, BaseObject, XWikiContext)
    // TODO: Revisit the display mode after the inline action is removed. Is the display mode still needed when
    // there is only one edit action?
    context.put("display", "edit");
    return "edit";
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiException(com.xpn.xwiki.XWikiException)

Example 28 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class ExportAction method render.

@Override
public String render(XWikiContext context) throws XWikiException {
    String defaultPage;
    try {
        XWikiRequest request = context.getRequest();
        String format = request.get("format");
        if ((format == null) || (format.equals("xar"))) {
            defaultPage = exportXAR(context);
        } else if (format.equals("html")) {
            defaultPage = exportHTML(context);
        } else {
            defaultPage = export(format, context);
        }
    } catch (Exception e) {
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_EXPORT, "Exception while exporting", e);
    }
    return defaultPage;
}
Also used : XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException) XWikiException(com.xpn.xwiki.XWikiException)

Example 29 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class ExportAction method exportXAR.

private String exportXAR(XWikiContext context) throws XWikiException, IOException, FilterException {
    XWikiRequest request = context.getRequest();
    boolean history = Boolean.valueOf(request.get("history"));
    boolean backup = Boolean.valueOf(request.get("backup"));
    String author = request.get("author");
    String description = request.get("description");
    String licence = request.get("licence");
    String version = request.get("version");
    String name = request.get("name");
    String[] pages = request.getParameterValues("pages");
    boolean all = ArrayUtils.isEmpty(pages);
    if (!context.getWiki().getRightService().hasWikiAdminRights(context)) {
        context.put("message", "needadminrights");
        return "exception";
    }
    if (StringUtils.isEmpty(name)) {
        if (all) {
            name = "backup";
        } else {
            name = "export";
        }
    }
    if (context.getWiki().ParamAsLong("xwiki.action.export.xar.usefilter", 1) == 1) {
        // Create input wiki stream
        DocumentInstanceInputProperties inputProperties = new DocumentInstanceInputProperties();
        // We don't want to log the details
        inputProperties.setVerbose(false);
        inputProperties.setWithJRCSRevisions(history);
        inputProperties.setWithRevisions(false);
        EntityReferenceSet entities = new EntityReferenceSet();
        if (all) {
            entities.includes(new WikiReference(context.getWikiId()));
        } else {
            // Find all page references and add them for processing
            Collection<DocumentReference> pageList = resolvePagesToExport(pages, context);
            for (DocumentReference page : pageList) {
                entities.includes(page);
            }
        }
        inputProperties.setEntities(entities);
        InputFilterStreamFactory inputFilterStreamFactory = Utils.getComponent(InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
        InputFilterStream inputFilterStream = inputFilterStreamFactory.createInputFilterStream(inputProperties);
        // Create output wiki stream
        XAROutputProperties xarProperties = new XAROutputProperties();
        // We don't want to log the details
        xarProperties.setVerbose(false);
        XWikiResponse response = context.getResponse();
        xarProperties.setTarget(new DefaultOutputStreamOutputTarget(response.getOutputStream()));
        xarProperties.setPackageName(name);
        if (description != null) {
            xarProperties.setPackageDescription(description);
        }
        if (licence != null) {
            xarProperties.setPackageLicense(licence);
        }
        if (author != null) {
            xarProperties.setPackageAuthor(author);
        }
        if (version != null) {
            xarProperties.setPackageVersion(version);
        }
        xarProperties.setPackageBackupPack(backup);
        xarProperties.setPreserveVersion(backup || history);
        BeanOutputFilterStreamFactory<XAROutputProperties> xarFilterStreamFactory = Utils.getComponent((Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize());
        OutputFilterStream outputFilterStream = xarFilterStreamFactory.createOutputFilterStream(xarProperties);
        // Export
        response.setContentType("application/zip");
        response.addHeader("Content-disposition", "attachment; filename=" + Util.encodeURI(name, context) + ".xar");
        inputFilterStream.read(outputFilterStream.getFilter());
        inputFilterStream.close();
        outputFilterStream.close();
        // Flush
        response.getOutputStream().flush();
        // Indicate that we are done with the response so no need to add anything
        context.setFinished(true);
    } else {
        PackageAPI export = ((PackageAPI) context.getWiki().getPluginApi("package", context));
        if (export == null) {
            // No Packaging plugin configured
            return "exception";
        }
        export.setWithVersions(history);
        if (author != null) {
            export.setAuthorName(author);
        }
        if (description != null) {
            export.setDescription(description);
        }
        if (licence != null) {
            export.setLicence(licence);
        }
        if (version != null) {
            export.setVersion(version);
        }
        export.setBackupPack(backup);
        export.setName(name);
        if (all) {
            export.backupWiki();
        } else {
            if (pages != null) {
                for (String pageName : pages) {
                    String defaultAction = request.get("action_" + pageName);
                    int iAction;
                    try {
                        iAction = Integer.parseInt(defaultAction);
                    } catch (Exception e) {
                        iAction = 0;
                    }
                    export.add(pageName, iAction);
                }
            }
            export.export();
        }
    }
    return null;
}
Also used : PackageAPI(com.xpn.xwiki.plugin.packaging.PackageAPI) OutputFilterStreamFactory(org.xwiki.filter.output.OutputFilterStreamFactory) BeanOutputFilterStreamFactory(org.xwiki.filter.output.BeanOutputFilterStreamFactory) InputFilterStreamFactory(org.xwiki.filter.input.InputFilterStreamFactory) OutputFilterStream(org.xwiki.filter.output.OutputFilterStream) DefaultOutputStreamOutputTarget(org.xwiki.filter.output.DefaultOutputStreamOutputTarget) InputFilterStream(org.xwiki.filter.input.InputFilterStream) XAROutputProperties(org.xwiki.filter.xar.output.XAROutputProperties) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) IOException(java.io.IOException) FilterException(org.xwiki.filter.FilterException) EntityReferenceSet(org.xwiki.model.reference.EntityReferenceSet) DocumentInstanceInputProperties(org.xwiki.filter.instance.input.DocumentInstanceInputProperties) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 30 with XWikiException

use of com.xpn.xwiki.XWikiException in project xwiki-platform by xwiki.

the class R40000XWIKI6990DataMigration method getLiquibaseChangeLog.

@Override
public String getLiquibaseChangeLog() throws DataMigrationException {
    final XWikiHibernateBaseStore store = getStore();
    this.configuration = store.getConfiguration();
    final StringBuilder sb = new StringBuilder(12000);
    final List<PersistentClass> classes = new ArrayList<PersistentClass>();
    detectDatabaseProducts(store);
    if (this.logger.isDebugEnabled()) {
        if (this.isOracle) {
            this.logger.debug("Oracle database detected, proceeding to all updates manually with deferred constraints.");
        }
        if (this.isMySQL && !this.isMySQLMyISAM) {
            this.logger.debug("MySQL innoDB database detected, proceeding to simplified updates with cascaded updates.");
        }
        if (this.isMySQLMyISAM) {
            this.logger.debug("MySQL MyISAM database detected, proceeding to all updates manually without constraints.");
        }
        if (this.isMSSQL) {
            this.logger.debug("Microsoft SQL Server database detected, proceeding to simplified updates with cascaded u" + "pdates. During data type changes, Primary Key constraints and indexes are temporarily dropped.");
        }
    }
    // Build the list of classes to check for updates
    classes.add(getClassMapping(BaseObject.class.getName()));
    for (Class<?> klass : PROPERTY_CLASS) {
        classes.add(getClassMapping(klass.getName()));
    }
    for (Class<?> klass : STATS_CLASSES) {
        classes.add(getClassMapping(klass.getName()));
    }
    // Initialize the counter of Change Logs
    this.logCount = 0;
    // do not prevent type changes, we skip all this processing for MySQL table stored using the MyISAM engine.
    if (!this.isMySQLMyISAM) {
        for (PersistentClass klass : classes) {
            this.fkTables.addAll(getForeignKeyTables(klass));
        }
    }
    // Drop all FK constraints
    for (Table table : this.fkTables) {
        appendDropForeignKeyChangeLog(sb, table);
    }
    // Process internal classes
    for (PersistentClass klass : classes) {
        // The same table mapped for StringListProperty and LargeStringProperty
        if (klass.getMappedClass() != StringListProperty.class) {
            // Update key types
            appendDataTypeChangeLogs(sb, klass);
        }
    }
    // Process dynamic and custom mapping
    final XWikiContext context = getXWikiContext();
    try {
        processCustomMappings((XWikiHibernateStore) store, new CustomMappingCallback() {

            @Override
            public void processCustomMapping(XWikiHibernateStore store, String name, String mapping, boolean hasDynamicMapping) throws XWikiException {
                if (INTERNAL.equals(mapping) || hasDynamicMapping) {
                    PersistentClass klass = R40000XWIKI6990DataMigration.this.configuration.getClassMapping(name);
                    if (!R40000XWIKI6990DataMigration.this.isMySQLMyISAM) {
                        List<Table> tables = getForeignKeyTables(klass);
                        for (Table table : tables) {
                            if (!R40000XWIKI6990DataMigration.this.fkTables.contains(table)) {
                                // Drop FK constraints for custom mapped class
                                appendDropForeignKeyChangeLog(sb, table);
                                R40000XWIKI6990DataMigration.this.fkTables.add(table);
                            }
                        }
                    }
                    // Update key types for custom mapped class
                    appendDataTypeChangeLogs(sb, klass);
                }
            }
        }, context);
    } catch (XWikiException e) {
        throw new DataMigrationException("Unable to process custom mapped classes during schema updated", e);
    }
    // Add FK constraints back, activating cascaded updates
    for (Table table : this.fkTables) {
        appendAddForeignKeyChangeLog(sb, table);
    }
    // Oracle doesn't support cascaded updates, so we still need to manually update each table
    if (this.isOracle) {
        this.fkTables.clear();
    }
    logProgress("%d schema updates required.", this.logCount);
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("About to execute this Liquibase XML: {}", sb.toString());
    }
    return sb.toString();
}
Also used : Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) XWikiHibernateBaseStore(com.xpn.xwiki.store.XWikiHibernateBaseStore) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) XWikiException(com.xpn.xwiki.XWikiException) PersistentClass(org.hibernate.mapping.PersistentClass)

Aggregations

XWikiException (com.xpn.xwiki.XWikiException)442 XWikiContext (com.xpn.xwiki.XWikiContext)156 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)147 DocumentReference (org.xwiki.model.reference.DocumentReference)98 BaseObject (com.xpn.xwiki.objects.BaseObject)88 IOException (java.io.IOException)57 QueryException (org.xwiki.query.QueryException)57 ArrayList (java.util.ArrayList)56 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)51 XWiki (com.xpn.xwiki.XWiki)48 XWikiRestException (org.xwiki.rest.XWikiRestException)44 Session (org.hibernate.Session)42 Document (com.xpn.xwiki.api.Document)38 InitializationException (org.xwiki.component.phase.InitializationException)36 WebApplicationException (javax.ws.rs.WebApplicationException)32 SQLException (java.sql.SQLException)31 ObjectNotFoundException (org.hibernate.ObjectNotFoundException)30 MigrationRequiredException (com.xpn.xwiki.store.migration.MigrationRequiredException)29 UnexpectedException (org.xwiki.store.UnexpectedException)29 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)25