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);
}
}
Aggregations