use of org.olat.user.UserModule in project OpenOLAT by OpenOLAT.
the class RegistrationWebService method register.
/**
* Register with the specified email
* @response.representation.200.doc Registration successful
* @response.representation.304.doc Already registered, HTTP-Header location set to redirect
* @response.representation.400.doc Email address not allowed
* @param email The email address
* @param request The HTTP Request
* @return
*/
@PUT
public Response register(@QueryParam("email") String email, @Context HttpServletRequest request) {
if (!CoreSpringFactory.getImpl(RegistrationModule.class).isSelfRegistrationEnabled()) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
ResponseBuilder response;
Locale locale = getLocale(request);
Translator translator = getTranslator(locale);
MailManager mailM = CoreSpringFactory.getImpl(MailManager.class);
UserManager userManager = UserManager.getInstance();
RegistrationManager rm = CoreSpringFactory.getImpl(RegistrationManager.class);
boolean foundUser = userManager.findUniqueIdentityByEmail(email) != null;
boolean noNewUserWithEmail = !userManager.isEmailAllowed(email);
String serverpath = Settings.getServerContextPathURI();
if (foundUser && noNewUserWithEmail) {
// redirect
URI redirectUri = UriBuilder.fromUri(Settings.getServerContextPathURI()).build();
response = Response.ok().status(Status.NOT_MODIFIED).location(redirectUri);
} else if (userManager.isEmailAllowed(email)) {
String ip = request.getRemoteAddr();
TemporaryKey tk = null;
UserModule userModule = CoreSpringFactory.getImpl(UserModule.class);
if (userModule.isEmailUnique()) {
tk = rm.loadTemporaryKeyByEmail(email);
}
if (tk == null) {
tk = rm.loadOrCreateTemporaryKeyByEmail(email, ip, RegistrationManager.REGISTRATION);
}
String today = DateFormat.getDateInstance(DateFormat.LONG, locale).format(new Date());
String[] bodyAttrs = new String[] { serverpath, tk.getRegistrationKey(), CoreSpringFactory.getImpl(I18nModule.class).getLocaleKey(locale) };
String[] whereFromAttrs = new String[] { serverpath, today, ip };
String body = translator.translate("reg.body", bodyAttrs) + SEPARATOR + translator.translate("reg.wherefrom", whereFromAttrs);
try {
MailBundle bundle = new MailBundle();
bundle.setTo(email);
bundle.setContent(translator.translate("reg.subject"), body);
MailerResult result = mailM.sendExternMessage(bundle, null, true);
if (result.isSuccessful()) {
response = Response.ok();
} else {
response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
log.error("", e);
}
} else {
response = Response.serverError().status(Status.BAD_REQUEST);
}
return response.build();
}
use of org.olat.user.UserModule in project openolat by klemens.
the class RegistrationWebService method register.
/**
* Register with the specified email
* @response.representation.200.doc Registration successful
* @response.representation.304.doc Already registered, HTTP-Header location set to redirect
* @response.representation.400.doc Email address not allowed
* @param email The email address
* @param request The HTTP Request
* @return
*/
@PUT
public Response register(@QueryParam("email") String email, @Context HttpServletRequest request) {
if (!CoreSpringFactory.getImpl(RegistrationModule.class).isSelfRegistrationEnabled()) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
ResponseBuilder response;
Locale locale = getLocale(request);
Translator translator = getTranslator(locale);
MailManager mailM = CoreSpringFactory.getImpl(MailManager.class);
UserManager userManager = UserManager.getInstance();
RegistrationManager rm = CoreSpringFactory.getImpl(RegistrationManager.class);
boolean foundUser = userManager.findUniqueIdentityByEmail(email) != null;
boolean noNewUserWithEmail = !userManager.isEmailAllowed(email);
String serverpath = Settings.getServerContextPathURI();
if (foundUser && noNewUserWithEmail) {
// redirect
URI redirectUri = UriBuilder.fromUri(Settings.getServerContextPathURI()).build();
response = Response.ok().status(Status.NOT_MODIFIED).location(redirectUri);
} else if (userManager.isEmailAllowed(email)) {
String ip = request.getRemoteAddr();
TemporaryKey tk = null;
UserModule userModule = CoreSpringFactory.getImpl(UserModule.class);
if (userModule.isEmailUnique()) {
tk = rm.loadTemporaryKeyByEmail(email);
}
if (tk == null) {
tk = rm.loadOrCreateTemporaryKeyByEmail(email, ip, RegistrationManager.REGISTRATION);
}
String today = DateFormat.getDateInstance(DateFormat.LONG, locale).format(new Date());
String[] bodyAttrs = new String[] { serverpath, tk.getRegistrationKey(), CoreSpringFactory.getImpl(I18nModule.class).getLocaleKey(locale) };
String[] whereFromAttrs = new String[] { serverpath, today, ip };
String body = translator.translate("reg.body", bodyAttrs) + SEPARATOR + translator.translate("reg.wherefrom", whereFromAttrs);
try {
MailBundle bundle = new MailBundle();
bundle.setTo(email);
bundle.setContent(translator.translate("reg.subject"), body);
MailerResult result = mailM.sendExternMessage(bundle, null, true);
if (result.isSuccessful()) {
response = Response.ok();
} else {
response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
}
} catch (Exception e) {
response = Response.serverError().status(Status.INTERNAL_SERVER_ERROR);
log.error("", e);
}
} else {
response = Response.serverError().status(Status.BAD_REQUEST);
}
return response.build();
}
Aggregations