|
1 | | -package org.woehlke.simpleworklist.oodm.services.impl; |
2 | | - |
3 | | -import org.slf4j.Logger; |
4 | | -import org.slf4j.LoggerFactory; |
5 | | -import org.springframework.beans.factory.annotation.Autowired; |
6 | | -import org.springframework.mail.MailException; |
7 | | -import org.springframework.mail.SimpleMailMessage; |
8 | | -import org.springframework.mail.javamail.JavaMailSender; |
9 | | -import org.springframework.scheduling.annotation.Async; |
10 | | -import org.springframework.stereotype.Service; |
11 | | -import org.springframework.transaction.annotation.Propagation; |
12 | | -import org.springframework.transaction.annotation.Transactional; |
13 | | -import org.woehlke.simpleworklist.config.ApplicationProperties; |
14 | | -import org.woehlke.simpleworklist.oodm.entities.UserPasswordRecovery; |
15 | | -import org.woehlke.simpleworklist.oodm.enumerations.UserPasswordRecoveryStatus; |
16 | | -import org.woehlke.simpleworklist.oodm.repository.UserPasswordRecoveryRepository; |
17 | | -import org.woehlke.simpleworklist.model.services.TokenGeneratorService; |
18 | | -import org.woehlke.simpleworklist.oodm.services.UserPasswordRecoveryService; |
19 | | - |
20 | | -import java.util.Date; |
21 | | - |
22 | | -@Service |
23 | | -@Transactional(propagation = Propagation.REQUIRED, readOnly = true) |
24 | | -public class UserPasswordRecoveryServiceImpl implements UserPasswordRecoveryService { |
25 | | - |
26 | | - private final UserPasswordRecoveryRepository userPasswordRecoveryRepository; |
27 | | - |
28 | | - private final ApplicationProperties applicationProperties; |
29 | | - |
30 | | - private final TokenGeneratorService tokenGeneratorService; |
31 | | - |
32 | | - private final JavaMailSender mailSender; |
33 | | - |
34 | | - private static final Logger LOGGER = LoggerFactory.getLogger(UserPasswordRecoveryServiceImpl.class); |
35 | | - |
36 | | - @Autowired |
37 | | - public UserPasswordRecoveryServiceImpl(UserPasswordRecoveryRepository userPasswordRecoveryRepository, ApplicationProperties applicationProperties, TokenGeneratorService tokenGeneratorService, JavaMailSender mailSender) { |
38 | | - this.userPasswordRecoveryRepository = userPasswordRecoveryRepository; |
39 | | - this.applicationProperties = applicationProperties; |
40 | | - this.tokenGeneratorService = tokenGeneratorService; |
41 | | - this.mailSender = mailSender; |
42 | | - } |
43 | | - |
44 | | - @Override |
45 | | - public UserPasswordRecovery findByToken(String token) { |
46 | | - return userPasswordRecoveryRepository.findByToken(token); |
47 | | - } |
48 | | - |
49 | | - @Override |
50 | | - public boolean passwordRecoveryIsRetryAndMaximumNumberOfRetries(String email) { |
51 | | - UserPasswordRecovery earlierOptIn = userPasswordRecoveryRepository.findByEmail(email); |
52 | | - return earlierOptIn == null?false:earlierOptIn.getNumberOfRetries() >= applicationProperties.getRegistration().getMaxRetries(); |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public void passwordRecoveryCheckIfResponseIsInTime(String email) { |
57 | | - UserPasswordRecovery earlierOptIn = userPasswordRecoveryRepository.findByEmail(email); |
58 | | - if (earlierOptIn != null) { |
59 | | - Date now = new Date(); |
60 | | - if ((applicationProperties.getRegistration().getTtlEmailVerificationRequest() + earlierOptIn.getRowCreatedAt().getTime()) < now.getTime()) { |
61 | | - userPasswordRecoveryRepository.delete(earlierOptIn); |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - @Async |
67 | | - @Override |
68 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
69 | | - public void passwordRecoverySendEmailTo(String email) { |
70 | | - UserPasswordRecovery earlierOptIn = userPasswordRecoveryRepository.findByEmail(email); |
71 | | - UserPasswordRecovery o = new UserPasswordRecovery(); |
72 | | - if (earlierOptIn != null) { |
73 | | - o = earlierOptIn; |
74 | | - o.increaseNumberOfRetries(); |
75 | | - } |
76 | | - o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_SAVED_EMAIL); |
77 | | - o.setEmail(email); |
78 | | - String token = tokenGeneratorService.getToken(); |
79 | | - o.setToken(token); |
80 | | - LOGGER.info("To be saved: " + o.toString()); |
81 | | - o = userPasswordRecoveryRepository.saveAndFlush(o); |
82 | | - LOGGER.info("Saved: " + o.toString()); |
83 | | - this.sendEmailForPasswordReset(o); |
84 | | - } |
85 | | - |
86 | | - @Override |
87 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
88 | | - public void passwordRecoverySentEmail(UserPasswordRecovery o) { |
89 | | - o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_SENT_EMAIL); |
90 | | - LOGGER.info("about to save: " + o.toString()); |
91 | | - userPasswordRecoveryRepository.saveAndFlush(o); |
92 | | - } |
93 | | - |
94 | | - @Override |
95 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
96 | | - public void passwordRecoveryClickedInEmail(UserPasswordRecovery o) { |
97 | | - o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_CLICKED_IN_MAIL); |
98 | | - userPasswordRecoveryRepository.saveAndFlush(o); |
99 | | - } |
100 | | - |
101 | | - @Override |
102 | | - @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
103 | | - public void passwordRecoveryDone(UserPasswordRecovery o) { |
104 | | - o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_STORED_CHANGED); |
105 | | - o = userPasswordRecoveryRepository.saveAndFlush(o); |
106 | | - userPasswordRecoveryRepository.delete(o); |
107 | | - } |
108 | | - |
109 | | - private void sendEmailForPasswordReset(UserPasswordRecovery o) { |
110 | | - String urlHost = applicationProperties.getRegistration().getUrlHost(); |
111 | | - String mailFrom= applicationProperties.getRegistration().getMailFrom(); |
112 | | - boolean success = true; |
113 | | - SimpleMailMessage msg = new SimpleMailMessage(); |
114 | | - msg.setTo(o.getEmail()); |
115 | | - msg.setText( |
116 | | - "Dear User, " |
117 | | - + "for Password Reset at SimpleWorklist, " |
118 | | - + "Please go to URL: \nhttp://" + urlHost + "/passwordResetConfirm/" + o.getToken() |
119 | | - + "\n\nSincerely Yours, The Team"); |
120 | | - msg.setSubject("Password Reset at Simple Worklist"); |
121 | | - msg.setFrom(mailFrom); |
122 | | - try { |
123 | | - this.mailSender.send(msg); |
124 | | - } catch (MailException ex) { |
125 | | - LOGGER.warn(ex.getMessage() + " for " + o.toString()); |
126 | | - success = false; |
127 | | - } |
128 | | - if (success) { |
129 | | - this.passwordRecoverySentEmail(o); |
130 | | - } |
131 | | - LOGGER.info("Sent MAIL: " + o.toString()); |
132 | | - } |
133 | | -} |
| 1 | +package org.woehlke.simpleworklist.oodm.services.impl; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.mail.MailException; |
| 7 | +import org.springframework.mail.SimpleMailMessage; |
| 8 | +import org.springframework.mail.javamail.JavaMailSender; |
| 9 | +import org.springframework.scheduling.annotation.Async; |
| 10 | +import org.springframework.stereotype.Service; |
| 11 | +import org.springframework.transaction.annotation.Propagation; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | +import org.woehlke.simpleworklist.config.ApplicationProperties; |
| 14 | +import org.woehlke.simpleworklist.oodm.entities.UserPasswordRecovery; |
| 15 | +import org.woehlke.simpleworklist.oodm.enumerations.UserPasswordRecoveryStatus; |
| 16 | +import org.woehlke.simpleworklist.oodm.repository.UserPasswordRecoveryRepository; |
| 17 | +import org.woehlke.simpleworklist.user.token.TokenGeneratorService; |
| 18 | +import org.woehlke.simpleworklist.oodm.services.UserPasswordRecoveryService; |
| 19 | + |
| 20 | +import java.util.Date; |
| 21 | + |
| 22 | +@Service |
| 23 | +@Transactional(propagation = Propagation.REQUIRED, readOnly = true) |
| 24 | +public class UserPasswordRecoveryServiceImpl implements UserPasswordRecoveryService { |
| 25 | + |
| 26 | + private final UserPasswordRecoveryRepository userPasswordRecoveryRepository; |
| 27 | + |
| 28 | + private final ApplicationProperties applicationProperties; |
| 29 | + |
| 30 | + private final TokenGeneratorService tokenGeneratorService; |
| 31 | + |
| 32 | + private final JavaMailSender mailSender; |
| 33 | + |
| 34 | + private static final Logger LOGGER = LoggerFactory.getLogger(UserPasswordRecoveryServiceImpl.class); |
| 35 | + |
| 36 | + @Autowired |
| 37 | + public UserPasswordRecoveryServiceImpl(UserPasswordRecoveryRepository userPasswordRecoveryRepository, ApplicationProperties applicationProperties, TokenGeneratorService tokenGeneratorService, JavaMailSender mailSender) { |
| 38 | + this.userPasswordRecoveryRepository = userPasswordRecoveryRepository; |
| 39 | + this.applicationProperties = applicationProperties; |
| 40 | + this.tokenGeneratorService = tokenGeneratorService; |
| 41 | + this.mailSender = mailSender; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public UserPasswordRecovery findByToken(String token) { |
| 46 | + return userPasswordRecoveryRepository.findByToken(token); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean passwordRecoveryIsRetryAndMaximumNumberOfRetries(String email) { |
| 51 | + UserPasswordRecovery earlierOptIn = userPasswordRecoveryRepository.findByEmail(email); |
| 52 | + return earlierOptIn == null?false:earlierOptIn.getNumberOfRetries() >= applicationProperties.getRegistration().getMaxRetries(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void passwordRecoveryCheckIfResponseIsInTime(String email) { |
| 57 | + UserPasswordRecovery earlierOptIn = userPasswordRecoveryRepository.findByEmail(email); |
| 58 | + if (earlierOptIn != null) { |
| 59 | + Date now = new Date(); |
| 60 | + if ((applicationProperties.getRegistration().getTtlEmailVerificationRequest() + earlierOptIn.getRowCreatedAt().getTime()) < now.getTime()) { |
| 61 | + userPasswordRecoveryRepository.delete(earlierOptIn); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Async |
| 67 | + @Override |
| 68 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 69 | + public void passwordRecoverySendEmailTo(String email) { |
| 70 | + UserPasswordRecovery earlierOptIn = userPasswordRecoveryRepository.findByEmail(email); |
| 71 | + UserPasswordRecovery o = new UserPasswordRecovery(); |
| 72 | + if (earlierOptIn != null) { |
| 73 | + o = earlierOptIn; |
| 74 | + o.increaseNumberOfRetries(); |
| 75 | + } |
| 76 | + o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_SAVED_EMAIL); |
| 77 | + o.setEmail(email); |
| 78 | + String token = tokenGeneratorService.getToken(); |
| 79 | + o.setToken(token); |
| 80 | + LOGGER.info("To be saved: " + o.toString()); |
| 81 | + o = userPasswordRecoveryRepository.saveAndFlush(o); |
| 82 | + LOGGER.info("Saved: " + o.toString()); |
| 83 | + this.sendEmailForPasswordReset(o); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 88 | + public void passwordRecoverySentEmail(UserPasswordRecovery o) { |
| 89 | + o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_SENT_EMAIL); |
| 90 | + LOGGER.info("about to save: " + o.toString()); |
| 91 | + userPasswordRecoveryRepository.saveAndFlush(o); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 96 | + public void passwordRecoveryClickedInEmail(UserPasswordRecovery o) { |
| 97 | + o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_CLICKED_IN_MAIL); |
| 98 | + userPasswordRecoveryRepository.saveAndFlush(o); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false) |
| 103 | + public void passwordRecoveryDone(UserPasswordRecovery o) { |
| 104 | + o.setDoubleOptInStatus(UserPasswordRecoveryStatus.PASSWORD_RECOVERY_STORED_CHANGED); |
| 105 | + o = userPasswordRecoveryRepository.saveAndFlush(o); |
| 106 | + userPasswordRecoveryRepository.delete(o); |
| 107 | + } |
| 108 | + |
| 109 | + private void sendEmailForPasswordReset(UserPasswordRecovery o) { |
| 110 | + String urlHost = applicationProperties.getRegistration().getUrlHost(); |
| 111 | + String mailFrom= applicationProperties.getRegistration().getMailFrom(); |
| 112 | + boolean success = true; |
| 113 | + SimpleMailMessage msg = new SimpleMailMessage(); |
| 114 | + msg.setTo(o.getEmail()); |
| 115 | + msg.setText( |
| 116 | + "Dear User, " |
| 117 | + + "for Password Reset at SimpleWorklist, " |
| 118 | + + "Please go to URL: \nhttp://" + urlHost + "/passwordResetConfirm/" + o.getToken() |
| 119 | + + "\n\nSincerely Yours, The Team"); |
| 120 | + msg.setSubject("Password Reset at Simple Worklist"); |
| 121 | + msg.setFrom(mailFrom); |
| 122 | + try { |
| 123 | + this.mailSender.send(msg); |
| 124 | + } catch (MailException ex) { |
| 125 | + LOGGER.warn(ex.getMessage() + " for " + o.toString()); |
| 126 | + success = false; |
| 127 | + } |
| 128 | + if (success) { |
| 129 | + this.passwordRecoverySentEmail(o); |
| 130 | + } |
| 131 | + LOGGER.info("Sent MAIL: " + o.toString()); |
| 132 | + } |
| 133 | +} |
0 commit comments