1
|
|
|
package com.osomapps.pt.tokenemail; |
2
|
|
|
|
3
|
|
|
import com.osomapps.pt.UnauthorizedException; |
4
|
|
|
import com.osomapps.pt.token.InUser; |
5
|
|
|
import com.osomapps.pt.token.InUserLogin; |
6
|
|
|
import com.osomapps.pt.token.InUserLoginRepository; |
7
|
|
|
import com.osomapps.pt.token.InUserRepository; |
8
|
|
|
import java.time.LocalDateTime; |
9
|
|
|
import java.util.Arrays; |
10
|
|
|
import java.util.Collections; |
11
|
|
|
import java.util.HashMap; |
12
|
|
|
import java.util.List; |
13
|
|
|
import java.util.Optional; |
14
|
|
|
import java.util.UUID; |
15
|
|
|
import org.apache.commons.lang3.RandomStringUtils; |
16
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder; |
17
|
|
|
import org.springframework.stereotype.Service; |
18
|
|
|
import org.springframework.validation.MapBindingResult; |
19
|
|
|
|
20
|
|
|
@Service |
21
|
|
|
class TokenEmailSignupService { |
22
|
|
|
|
23
|
|
|
private final SendEmailService sendEmailService; |
24
|
|
|
private final InUserEmailRepository inUserEmailRepository; |
25
|
|
|
private final EmailValidator emailValidator; |
26
|
|
|
private final PasswordEncoder passwordEncoder; |
27
|
|
|
private final InUserRepository inUserRepository; |
28
|
|
|
private final InUserLoginRepository inUserLoginRepository; |
29
|
|
|
private final DataurlValidator dataurlValidator; |
30
|
|
|
|
31
|
|
|
TokenEmailSignupService( |
32
|
|
|
SendEmailService sendEmailService, |
33
|
|
|
InUserEmailRepository inUserEmailRepository, |
34
|
|
|
EmailValidator emailValidator, |
35
|
|
|
PasswordEncoder passwordEncoder, |
36
|
|
|
InUserRepository inUserRepository, |
37
|
|
|
InUserLoginRepository inUserLoginRepository, |
38
|
|
|
DataurlValidator dataurlValidator) { |
39
|
|
|
this.sendEmailService = sendEmailService; |
40
|
|
|
this.inUserEmailRepository = inUserEmailRepository; |
41
|
|
|
this.emailValidator = emailValidator; |
42
|
|
|
this.passwordEncoder = passwordEncoder; |
43
|
|
|
this.inUserRepository = inUserRepository; |
44
|
|
|
this.inUserLoginRepository = inUserLoginRepository; |
45
|
|
|
this.dataurlValidator = dataurlValidator; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
InUserEmail createInUserEmail(TokenEmailSignupRequestDTO tokenEmailSignupRequestDTO) { |
49
|
|
|
final InUserEmail inUserEmail; |
50
|
|
|
final String email = tokenEmailSignupRequestDTO.getUser().getEmail().toLowerCase().trim(); |
51
|
|
|
final List<InUserEmail> inUserEmails = inUserEmailRepository.findByLogin(email); |
52
|
|
|
if (inUserEmails.isEmpty()) { |
53
|
|
|
inUserEmail = new InUserEmail(); |
54
|
|
|
final MapBindingResult errors = |
55
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
56
|
|
|
emailValidator.validate(email, errors); |
57
|
|
|
if (errors.hasErrors()) { |
58
|
|
|
throw new UnauthorizedException(errors.getAllErrors().get(0).getDefaultMessage()); |
59
|
|
|
} |
60
|
|
|
final MapBindingResult errorsDataurl = |
61
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
62
|
|
|
dataurlValidator.validate( |
63
|
|
|
tokenEmailSignupRequestDTO.getUser().getAvatar_dataurl(), errorsDataurl); |
64
|
|
|
if (errorsDataurl.hasErrors()) { |
65
|
|
|
throw new UnauthorizedException( |
66
|
|
|
errorsDataurl.getAllErrors().get(0).getDefaultMessage()); |
67
|
|
|
} |
68
|
|
|
inUserEmail.setLogin(email); |
69
|
|
|
inUserEmail.setUser_name(tokenEmailSignupRequestDTO.getUser().getName()); |
70
|
|
|
inUserEmail.setDevice_id(tokenEmailSignupRequestDTO.getDevice_id()); |
71
|
|
|
inUserEmail.setPassword( |
72
|
|
|
passwordEncoder.encode(tokenEmailSignupRequestDTO.getPassword())); |
73
|
|
|
} else { |
74
|
|
|
throw new UnauthorizedException("User already registered"); |
75
|
|
|
} |
76
|
|
|
return inUserEmail; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
TokenEmailSignupResponseDTO createNewToken( |
80
|
|
|
TokenEmailSignupRequestDTO tokenEmailSignupRequestDTO, String remoteAddr) { |
81
|
|
|
final InUserEmail inUserEmail = createInUserEmail(tokenEmailSignupRequestDTO); |
82
|
|
|
final InUserLogin inUserLogin = new InUserLogin(); |
83
|
|
|
final InUser inUser = new InUser(); |
84
|
|
|
inUser.setInUserEmails(Arrays.asList(inUserEmail)); |
85
|
|
|
inUser.setInUserLogins(Arrays.asList(inUserLogin)); |
86
|
|
|
inUser.setAvatar_dataurl(tokenEmailSignupRequestDTO.getUser().getAvatar_dataurl()); |
87
|
|
|
final InUser savedInUser = inUserRepository.save(inUser); |
88
|
|
|
inUserLogin.setInUser(savedInUser); |
89
|
|
|
inUserLogin.setIp_address(remoteAddr); |
90
|
|
|
inUserLoginRepository.saveAndFlush(inUserLogin); |
91
|
|
|
inUserEmail.setInUser(savedInUser); |
92
|
|
|
inUserEmailRepository.save(inUserEmail); |
93
|
|
|
final UserSignupResponseDTO user = new UserSignupResponseDTO(); |
94
|
|
|
user.setId(inUserEmail.getInUser().getId()); |
95
|
|
|
user.setName(inUserEmail.getUser_name()); |
96
|
|
|
user.setEmail(inUserEmail.getLogin()); |
97
|
|
|
user.setGender(inUserEmail.getInUser().getD_sex()); |
98
|
|
|
user.setAge( |
99
|
|
|
inUserEmail.getInUser().getAge() == null |
100
|
|
|
? null |
101
|
|
|
: inUserEmail.getInUser().getAge().intValue()); |
102
|
|
|
user.setBirthday(inUserEmail.getInUser().getBirthday()); |
103
|
|
|
user.setAvatar_dataurl(inUserEmail.getInUser().getAvatar_dataurl()); |
104
|
|
|
user.setGoals(Collections.emptyList()); |
105
|
|
|
return new TokenEmailSignupResponseDTO().setToken(inUserLogin.getToken()).setUser(user); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
boolean confirmToken(String confirmToken) { |
109
|
|
|
final List<InUserEmail> inUserEmails = |
110
|
|
|
inUserEmailRepository.findByConfirmToken(confirmToken); |
111
|
|
|
if (inUserEmails.isEmpty()) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
if (!inUserEmails.get(0).getIs_confirmed()) { |
115
|
|
|
inUserEmails.get(0).setIs_confirmed(Boolean.TRUE); |
116
|
|
|
inUserEmails.get(0).setConfirmed(LocalDateTime.now()); |
117
|
|
|
inUserEmailRepository.save(inUserEmails.get(0)); |
118
|
|
|
} |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
void forgotPassword(ForgotPasswordRequestDTO forgotPasswordRequestDTO, String remoteAddr) { |
|
|
|
|
123
|
|
|
final String email = forgotPasswordRequestDTO.getEmail().toLowerCase().trim(); |
124
|
|
|
List<InUserEmail> inUserEmails = inUserEmailRepository.findByLogin(email); |
125
|
|
|
if (inUserEmails.isEmpty()) { |
126
|
|
|
throw new UnauthorizedException("Email not found: " + email); |
127
|
|
|
} else { |
128
|
|
|
inUserEmails |
129
|
|
|
.get(0) |
130
|
|
|
.setResetToken("re-" + UUID.randomUUID().toString().replace("-", "")); |
131
|
|
|
inUserEmailRepository.save(inUserEmails.get(0)); |
132
|
|
|
new Thread( |
133
|
|
|
() -> { |
|
|
|
|
134
|
|
|
sendEmailService.sendForgotPassword(inUserEmails.get(0)); |
135
|
|
|
}, |
136
|
|
|
"Reset-email") |
137
|
|
|
.start(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
Optional<String> resetToken(String resetToken) { |
142
|
|
|
final List<InUserEmail> inUserEmails = inUserEmailRepository.findByResetToken(resetToken); |
143
|
|
|
if (inUserEmails.isEmpty()) { |
144
|
|
|
return Optional.empty(); |
145
|
|
|
} |
146
|
|
|
String newPassword = |
147
|
|
|
RandomStringUtils.randomAlphabetic(3).toUpperCase() |
148
|
|
|
+ RandomStringUtils.randomNumeric(3); |
149
|
|
|
inUserEmails.get(0).setIs_reseted(Boolean.TRUE); |
150
|
|
|
inUserEmails.get(0).setReseted(LocalDateTime.now()); |
151
|
|
|
inUserEmails.get(0).setPassword(passwordEncoder.encode(newPassword)); |
152
|
|
|
inUserEmails.get(0).setResetToken(""); |
153
|
|
|
inUserEmailRepository.save(inUserEmails.get(0)); |
154
|
|
|
return Optional.of(newPassword); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|