// public functions @Override @Transactional public CommandProcessingResult createPublicUser(final JsonCommand command) { try { this.userDataValidator.validateCreate(command.getJsonCommand()); User user = User.fromJson(command, false, true); generateKeyUsedForPasswordSalting(user); final String encodePassword = this.applicationPasswordEncoder.encode(user); user.updatePassword(encodePassword); this.userRepository.save(user); final JsonElement element = this.fromJsonHelper.parse(command.getJsonCommand()); final String returnUrl = this.fromJsonHelper.extractStringNamed(ReturnUrlParamName, element); final String email = user.getUsername(); final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); final String nowDate = sf.format(DateTime.now().toDate()); final String text = nowDate + email + nowDate + Math.random(); final String otp = new String(Base64.encode(text.getBytes())); final UserOtp userOtp = UserOtp.createOtp(user, email, otp.substring(3, otp.length() - 5), returnUrl); this.userOtpRepository.save(userOtp); final String finalOtp = userOtp.getOtp(); final String verificationLink = DefaultAppUrl + "userapi/activate" + "?e=" + email + "&uas=" + finalOtp; String toEmails[] = new String[] {email}; this.emailSenderService.sendEmail( toEmails, null, null, EmailTemplates.activateUserEmailSubject(), EmailTemplates.activateUserEmailTemplate(user.getName(), verificationLink)); return new CommandProcessingResultBuilder().withSuccessStatus().build(); } catch (DataIntegrityViolationException ex) { ex.printStackTrace(); final Throwable realCause = ex.getCause(); if (realCause.getMessage().toLowerCase().contains("email")) { throw new PlatformDataIntegrityException( "error.msg.email.already.exist", "The email provided already exitst in the system." + realCause.getMessage()); } throw new PlatformDataIntegrityException( "error.msg.unknown.data.integrity.issue", "Unknown data integrity issue with resource: " + realCause.getMessage()); } }
@Override public String activateUser(final String email, final String otp) { try { final UserOtp userOtp = this.userOtpRepository.findUserOtpByUserNameAndOtp(email, otp); if (null == userOtp) { return null; // throw new ResourceNotFoundException( // "error.entity.user.found", "User with given details does not exist"); } final User user = userOtp.getThisUser(); user.activate(); this.userRepository.saveAndFlush(user); // after successful activation delete otp this.userOtpRepository.delete(userOtp); return userOtp.getReturnUrl(); } catch (Exception e) { e.printStackTrace(); return null; } }