Search in sources :

Example 21 with XWikiRequest

use of com.xpn.xwiki.web.XWikiRequest in project xwiki-platform by xwiki.

the class XWiki method createUser.

public int createUser(boolean withValidation, String userRights, XWikiContext context) throws XWikiException {
    try {
        XWikiRequest request = context.getRequest();
        Map<String, String[]> map = Util.getObject(request, "register");
        String content = "";
        Syntax syntax = getDefaultDocumentSyntaxInternal();
        // Read the values from the request.
        String xwikiname = request.getParameter("xwikiname");
        String password2 = request.getParameter("register2_password");
        String password = (map.get("password"))[0];
        String email = (map.get("email"))[0];
        String template = request.getParameter("template");
        String parent = request.getParameter("parent");
        String validkey = null;
        // Validate the values.
        if (XWikiRightService.SUPERADMIN_USER.equalsIgnoreCase(xwikiname)) {
            return -8;
        }
        try {
            if (!context.getUtil().match(getConfiguration().getProperty("xwiki.validusername", "/^[a-zA-Z0-9_]+$/"), xwikiname)) {
                return -4;
            }
        } catch (RuntimeException ex) {
            LOGGER.warn("Invalid regular expression for xwiki.validusername", ex);
            if (!context.getUtil().match("/^[a-zA-Z0-9_]+$/", xwikiname)) {
                return -4;
            }
        }
        if ((!password.equals(password2)) || (password.trim().equals(""))) {
            // TODO: throw wrong password exception
            return -2;
        }
        if ((template != null) && (!template.equals(""))) {
            XWikiDocument tdoc = getDocument(template, context);
            if ((!tdoc.isNew())) {
                // FIXME: This ignores template objects, attachments, etc.
                content = tdoc.getContent();
                syntax = tdoc.getSyntax();
            }
        }
        if ((parent == null) || (parent.equals(""))) {
            parent = "XWiki.XWikiUsers";
        }
        // Mark the user as active or waiting email validation.
        if (withValidation) {
            map.put("active", new String[] { "0" });
            validkey = generateValidationKey(16);
            map.put("validkey", new String[] { validkey });
        } else {
            // Mark user active
            map.put("active", new String[] { "1" });
        }
        // Create the user.
        int result = createUser(xwikiname, map, getRelativeEntityReferenceResolver().resolve(parent, EntityType.DOCUMENT), content, syntax, userRights, context);
        // Send validation mail, if needed.
        if ((result > 0) && (withValidation)) {
            // Send the validation email
            try {
                sendValidationEmail(xwikiname, password, email, validkey, "validation_email_content", context);
            } catch (XWikiException e) {
                LOGGER.warn("User created. Failed to send the mail to the created user.", e);
                return -11;
            }
        }
        return result;
    } catch (XWikiException e) {
        LOGGER.error(e.getMessage(), e);
        throw e;
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new XWikiException(XWikiException.MODULE_XWIKI_APP, XWikiException.ERROR_XWIKI_APP_CREATE_USER, "Exception while creating user", e, null);
    }
}
Also used : XWikiRequest(com.xpn.xwiki.web.XWikiRequest) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString) Syntax(org.xwiki.rendering.syntax.Syntax) 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)

Aggregations

XWikiRequest (com.xpn.xwiki.web.XWikiRequest)21 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWiki (com.xpn.xwiki.XWiki)4 IOException (java.io.IOException)4 Cookie (javax.servlet.http.Cookie)4 XWikiException (com.xpn.xwiki.XWikiException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)3 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)3 BaseObject (com.xpn.xwiki.objects.BaseObject)3 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)3 Date (java.util.Date)3 Test (org.junit.Test)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 FileNotFoundException (java.io.FileNotFoundException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 NamingException (javax.naming.NamingException)2 HttpSession (javax.servlet.http.HttpSession)2 URIException (org.apache.commons.httpclient.URIException)2