cleanup work
This commit is contained in:
parent
e667993c18
commit
b4c54fdb71
|
@ -19,7 +19,7 @@ public interface IUserService {
|
|||
|
||||
VerificationToken getVerificationToken(String VerificationToken);
|
||||
|
||||
VerificationToken updateVerificationToken(String token);
|
||||
VerificationToken generateNewVerificationToken(String token);
|
||||
|
||||
void createPasswordResetTokenForUser(User user, String token);
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ public class UserService implements IUserService {
|
|||
// API
|
||||
|
||||
@Override
|
||||
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
|
||||
public User registerNewUserAccount(final UserDto accountDto) throws EmailExistsException {
|
||||
if (emailExist(accountDto.getEmail())) {
|
||||
throw new EmailExistsException("There is an account with that email adress: " + accountDto.getEmail());
|
||||
}
|
||||
|
@ -54,67 +54,74 @@ public class UserService implements IUserService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public User getUser(String verificationToken) {
|
||||
User user = tokenRepository.findByToken(verificationToken).getUser();
|
||||
public User getUser(final String verificationToken) {
|
||||
final User user = tokenRepository.findByToken(verificationToken).getUser();
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public VerificationToken getVerificationToken(String VerificationToken) {
|
||||
public VerificationToken getVerificationToken(final String VerificationToken) {
|
||||
return tokenRepository.findByToken(VerificationToken);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveRegisteredUser(User user) {
|
||||
public void saveRegisteredUser(final User user) {
|
||||
repository.save(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteUser(User user) {
|
||||
public void deleteUser(final User user) {
|
||||
repository.delete(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createVerificationTokenForUser(User user, String token) {
|
||||
VerificationToken myToken = new VerificationToken(token, user);
|
||||
public void createVerificationTokenForUser(final User user, final String token) {
|
||||
final VerificationToken myToken = new VerificationToken(token, user);
|
||||
tokenRepository.save(myToken);
|
||||
}
|
||||
|
||||
public VerificationToken updateVerificationToken(String verificationToken) {
|
||||
VerificationToken vToken = tokenRepository.findByToken(verificationToken);
|
||||
@Override
|
||||
public VerificationToken generateNewVerificationToken(final String existingVerificationToken) {
|
||||
VerificationToken vToken = tokenRepository.findByToken(existingVerificationToken);
|
||||
vToken.updateToken(UUID.randomUUID().toString());
|
||||
vToken = tokenRepository.save(vToken);
|
||||
return vToken;
|
||||
}
|
||||
|
||||
public void createPasswordResetTokenForUser(User user, String token) {
|
||||
PasswordResetToken myToken = new PasswordResetToken(token, user);
|
||||
@Override
|
||||
public void createPasswordResetTokenForUser(final User user, final String token) {
|
||||
final PasswordResetToken myToken = new PasswordResetToken(token, user);
|
||||
passwordTokenRepository.save(myToken);
|
||||
}
|
||||
|
||||
public User findUserByEmail(String email) {
|
||||
@Override
|
||||
public User findUserByEmail(final String email) {
|
||||
return repository.findByEmail(email);
|
||||
}
|
||||
|
||||
public PasswordResetToken getPasswordResetToken(String token) {
|
||||
@Override
|
||||
public PasswordResetToken getPasswordResetToken(final String token) {
|
||||
return passwordTokenRepository.findByToken(token);
|
||||
}
|
||||
|
||||
public User getUserByPasswordResetToken(String token) {
|
||||
@Override
|
||||
public User getUserByPasswordResetToken(final String token) {
|
||||
return passwordTokenRepository.findByToken(token).getUser();
|
||||
}
|
||||
|
||||
public User getUserByID(long id) {
|
||||
@Override
|
||||
public User getUserByID(final long id) {
|
||||
return repository.findOne(id);
|
||||
}
|
||||
|
||||
public void changeUserPassword(User user, String password) {
|
||||
@Override
|
||||
public void changeUserPassword(final User user, final String password) {
|
||||
user.setPassword(passwordEncoder.encode(password));
|
||||
repository.save(user);
|
||||
}
|
||||
|
||||
private boolean emailExist(String email) {
|
||||
User user = repository.findByEmail(email);
|
||||
private boolean emailExist(final String email) {
|
||||
final User user = repository.findByEmail(email);
|
||||
if (user != null) {
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -44,7 +44,7 @@ public class RegistrationController {
|
|||
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@Autowired
|
||||
private IUserService service;
|
||||
private IUserService userService;
|
||||
|
||||
@Autowired
|
||||
private MessageSource messages;
|
||||
|
@ -62,28 +62,30 @@ public class RegistrationController {
|
|||
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@RequestMapping(value = "/user/registration", method = RequestMethod.GET)
|
||||
public String showRegistrationForm(WebRequest request, Model model) {
|
||||
public String showRegistrationForm(final WebRequest request, final Model model) {
|
||||
LOGGER.debug("Rendering registration page.");
|
||||
UserDto accountDto = new UserDto();
|
||||
final UserDto accountDto = new UserDto();
|
||||
model.addAttribute("user", accountDto);
|
||||
return "registration";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/regitrationConfirm", method = RequestMethod.GET)
|
||||
public String confirmRegistration(WebRequest request, Model model, @RequestParam("token") String token) {
|
||||
Locale locale = request.getLocale();
|
||||
public String confirmRegistration(final WebRequest request, final Model model, @RequestParam("token") final String token) {
|
||||
final Locale locale = request.getLocale();
|
||||
|
||||
VerificationToken verificationToken = service.getVerificationToken(token);
|
||||
final VerificationToken verificationToken = userService.getVerificationToken(token);
|
||||
if (verificationToken == null) {
|
||||
String message = messages.getMessage("auth.message.invalidToken", null, locale);
|
||||
final String message = messages.getMessage("auth.message.invalidToken", null, locale);
|
||||
model.addAttribute("message", message);
|
||||
return "redirect:/badUser.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
User user = verificationToken.getUser();
|
||||
Calendar cal = Calendar.getInstance();
|
||||
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
|
||||
final User user = verificationToken.getUser();
|
||||
final Calendar cal = Calendar.getInstance();
|
||||
final DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
|
||||
System.out.println(df.format(verificationToken.getExpiryDate()));
|
||||
System.out.println(df.format(cal.getTime()));
|
||||
|
||||
|
@ -95,46 +97,40 @@ public class RegistrationController {
|
|||
}
|
||||
|
||||
user.setEnabled(true);
|
||||
service.saveRegisteredUser(user);
|
||||
userService.saveRegisteredUser(user);
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/registration", method = RequestMethod.POST)
|
||||
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid UserDto accountDto, BindingResult result, WebRequest request, Errors errors) {
|
||||
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid final UserDto accountDto, final BindingResult result, final WebRequest request, final Errors errors) {
|
||||
LOGGER.debug("Registering user account with information: {}", accountDto);
|
||||
if (result.hasErrors()) {
|
||||
return new ModelAndView("registration", "user", accountDto);
|
||||
}
|
||||
|
||||
User registered = createUserAccount(accountDto);
|
||||
final User registered = createUserAccount(accountDto);
|
||||
if (registered == null) {
|
||||
result.rejectValue("email", "message.regError");
|
||||
}
|
||||
try {
|
||||
String appUrl = request.getContextPath();
|
||||
final String appUrl = request.getContextPath();
|
||||
eventPublisher.publishEvent(new OnRegistrationCompleteEvent(registered, request.getLocale(), appUrl));
|
||||
} catch (Exception me) {
|
||||
} catch (final Exception me) {
|
||||
return new ModelAndView("emailError", "user", accountDto);
|
||||
}
|
||||
return new ModelAndView("successRegister", "user", accountDto);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/user/resendRegistrationToken", method = RequestMethod.GET)
|
||||
public String resendRegistrationToken(WebRequest request, Model model, @RequestParam("token") String token) {
|
||||
Locale locale = request.getLocale();
|
||||
VerificationToken newToken = service.updateVerificationToken(token);
|
||||
User user = service.getUser(newToken.getToken());
|
||||
public String resendRegistrationToken(final WebRequest request, final Model model, @RequestParam("token") final String existingToken) {
|
||||
final Locale locale = request.getLocale();
|
||||
final VerificationToken newToken = userService.generateNewVerificationToken(existingToken);
|
||||
final User user = userService.getUser(newToken.getToken());
|
||||
try {
|
||||
String confirmationUrl = request.getContextPath() + "/regitrationConfirm.html?token=" + newToken.getToken();
|
||||
String message = messages.getMessage("message.resendToken", null, request.getLocale());
|
||||
SimpleMailMessage email = new SimpleMailMessage();
|
||||
email.setTo(user.getEmail());
|
||||
email.setSubject("Resend Registration Token");
|
||||
email.setText(message + " \r\n" + "http://localhost:8080" + confirmationUrl);
|
||||
System.out.println(email.getText());
|
||||
final SimpleMailMessage email = constructResetVerificationTokenEmail(request.getContextPath(), request.getLocale(), newToken, user);
|
||||
mailSender.send(email);
|
||||
System.out.println(email.getText());
|
||||
} catch (Exception e) {
|
||||
} catch (final Exception e) {
|
||||
// MailException
|
||||
return "redirect:/emailError.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
model.addAttribute("message", messages.getMessage("message.resendToken", null, locale));
|
||||
|
@ -142,27 +138,26 @@ public class RegistrationController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/user/resetPassword", method = RequestMethod.POST)
|
||||
public String resetPassword(WebRequest request, Model model, @RequestParam("email") String userEmail) {
|
||||
|
||||
User user = service.findUserByEmail(userEmail);
|
||||
public String resetPassword(final WebRequest request, final Model model, @RequestParam("email") final String userEmail) {
|
||||
final User user = userService.findUserByEmail(userEmail);
|
||||
if (user == null) {
|
||||
model.addAttribute("message", messages.getMessage("auth.message.expired", null, request.getLocale()));
|
||||
return "redirect:/login.html?lang=" + request.getLocale().getLanguage();
|
||||
}
|
||||
|
||||
String token = UUID.randomUUID().toString();
|
||||
service.createPasswordResetTokenForUser(user, token);
|
||||
final String token = UUID.randomUUID().toString();
|
||||
userService.createPasswordResetTokenForUser(user, token);
|
||||
try {
|
||||
String url = request.getContextPath() + "/user/changePassword?id=" + user.getId() + "&token=" + token;
|
||||
String message = messages.getMessage("message.resetPassword", null, request.getLocale());
|
||||
SimpleMailMessage email = new SimpleMailMessage();
|
||||
final String url = request.getContextPath() + "/user/changePassword?id=" + user.getId() + "&token=" + token;
|
||||
final String message = messages.getMessage("message.resetPassword", null, request.getLocale());
|
||||
final SimpleMailMessage email = new SimpleMailMessage();
|
||||
email.setTo(user.getEmail());
|
||||
email.setSubject("Reset Password");
|
||||
email.setText(message + " \r\n" + "http://localhost:8080" + url);
|
||||
System.out.println(email.getText());
|
||||
mailSender.send(email);
|
||||
System.out.println(email.getText());
|
||||
} catch (Exception e) {
|
||||
} catch (final Exception e) {
|
||||
return "redirect:/emailError.html?lang=" + request.getLocale().getLanguage();
|
||||
}
|
||||
model.addAttribute("message", messages.getMessage("message.resetPassword", null, request.getLocale()));
|
||||
|
@ -170,26 +165,26 @@ public class RegistrationController {
|
|||
}
|
||||
|
||||
@RequestMapping(value = "/user/changePassword", method = RequestMethod.GET)
|
||||
public String changePassword(WebRequest request, Model model, @RequestParam("id") long id, @RequestParam("token") String token) {
|
||||
Locale locale = request.getLocale();
|
||||
public String changePassword(final WebRequest request, final Model model, @RequestParam("id") final long id, @RequestParam("token") final String token) {
|
||||
final Locale locale = request.getLocale();
|
||||
|
||||
PasswordResetToken passToken = service.getPasswordResetToken(token);
|
||||
User user = passToken.getUser();
|
||||
final PasswordResetToken passToken = userService.getPasswordResetToken(token);
|
||||
final User user = passToken.getUser();
|
||||
if (passToken == null || user.getId() != id) {
|
||||
String message = messages.getMessage("auth.message.invalidToken", null, locale);
|
||||
final String message = messages.getMessage("auth.message.invalidToken", null, locale);
|
||||
model.addAttribute("message", message);
|
||||
System.out.println(id);
|
||||
System.out.println(passToken);
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
Calendar cal = Calendar.getInstance();
|
||||
final Calendar cal = Calendar.getInstance();
|
||||
if ((passToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
|
||||
model.addAttribute("message", messages.getMessage("auth.message.expired", null, locale));
|
||||
return "redirect:/login.html?lang=" + locale.getLanguage();
|
||||
}
|
||||
|
||||
Authentication auth = new UsernamePasswordAuthenticationToken(user, null, userDetailsService.loadUserByUsername(user.getEmail()).getAuthorities());
|
||||
final Authentication auth = new UsernamePasswordAuthenticationToken(user, null, userDetailsService.loadUserByUsername(user.getEmail()).getAuthorities());
|
||||
SecurityContextHolder.getContext().setAuthentication(auth);
|
||||
|
||||
return "redirect:/updatePassword.html?lang=" + locale.getLanguage();
|
||||
|
@ -197,22 +192,36 @@ public class RegistrationController {
|
|||
|
||||
@RequestMapping(value = "/user/savePassword", method = RequestMethod.POST)
|
||||
@PreAuthorize("hasRole('READ_PRIVILEGE')")
|
||||
public String savePassword(WebRequest request, Model model, @RequestParam("password") String password) {
|
||||
Locale locale = request.getLocale();
|
||||
public String savePassword(final WebRequest request, final Model model, @RequestParam("password") final String password) {
|
||||
final Locale locale = request.getLocale();
|
||||
|
||||
User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
service.changeUserPassword(user, password);
|
||||
final User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
userService.changeUserPassword(user, password);
|
||||
|
||||
return "redirect:/login.html?lang=" + locale;
|
||||
}
|
||||
|
||||
private User createUserAccount(UserDto accountDto) {
|
||||
// NON-API
|
||||
|
||||
private final SimpleMailMessage constructResetVerificationTokenEmail(final String contextPath, final Locale locale, final VerificationToken newToken, final User user) {
|
||||
final String confirmationUrl = contextPath + "/regitrationConfirm.html?token=" + newToken.getToken();
|
||||
final String message = messages.getMessage("message.resendToken", null, locale);
|
||||
final SimpleMailMessage email = new SimpleMailMessage();
|
||||
email.setSubject("Resend Registration Token");
|
||||
email.setText(message + " \r\n" + "http://localhost:8080" + confirmationUrl);
|
||||
|
||||
email.setTo(user.getEmail());
|
||||
return email;
|
||||
}
|
||||
|
||||
private User createUserAccount(final UserDto accountDto) {
|
||||
User registered = null;
|
||||
try {
|
||||
registered = service.registerNewUserAccount(accountDto);
|
||||
} catch (EmailExistsException e) {
|
||||
registered = userService.registerNewUserAccount(accountDto);
|
||||
} catch (final EmailExistsException e) {
|
||||
return null;
|
||||
}
|
||||
return registered;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue