|
1
|
|
|
package com.osomapps.pt.tokenemail; |
|
2
|
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.core.type.TypeReference; |
|
4
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
5
|
|
|
import com.osomapps.pt.UnauthorizedException; |
|
6
|
|
|
import com.osomapps.pt.token.InUser; |
|
7
|
|
|
import com.osomapps.pt.token.InUserLogin; |
|
8
|
|
|
import com.osomapps.pt.token.InUserLoginRepository; |
|
9
|
|
|
import com.osomapps.pt.token.InUserLogout; |
|
10
|
|
|
import com.osomapps.pt.token.InUserLogoutRepository; |
|
11
|
|
|
import com.osomapps.pt.token.InUserRepository; |
|
12
|
|
|
import com.osomapps.pt.user.UserGoalResponseDTO; |
|
13
|
|
|
import com.osomapps.pt.user.UserLevel; |
|
14
|
|
|
import java.io.IOException; |
|
15
|
|
|
import java.util.List; |
|
16
|
|
|
import java.util.Map; |
|
17
|
|
|
import java.util.stream.Collectors; |
|
18
|
|
|
import org.springframework.data.util.Pair; |
|
19
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder; |
|
20
|
|
|
import org.springframework.stereotype.Service; |
|
21
|
|
|
|
|
22
|
|
|
@Service |
|
23
|
|
|
class TokenEmailService { |
|
24
|
|
|
|
|
25
|
|
|
private final InUserRepository inUserRepository; |
|
26
|
|
|
private final InUserEmailRepository inUserEmailRepository; |
|
27
|
|
|
private final InUserLoginRepository inUserLoginRepository; |
|
28
|
|
|
private final InUserLogoutRepository inUserLogoutRepository; |
|
29
|
|
|
private final PasswordEncoder passwordEncoder; |
|
30
|
|
|
|
|
31
|
|
|
TokenEmailService( |
|
32
|
|
|
InUserRepository inUserRepository, |
|
33
|
|
|
InUserEmailRepository inUserEmailRepository, |
|
34
|
|
|
InUserLoginRepository inUserLoginRepository, |
|
35
|
|
|
InUserLogoutRepository inUserLogoutRepository, |
|
36
|
|
|
PasswordEncoder passwordEncoder) { |
|
37
|
|
|
this.inUserRepository = inUserRepository; |
|
38
|
|
|
this.inUserEmailRepository = inUserEmailRepository; |
|
39
|
|
|
this.inUserLoginRepository = inUserLoginRepository; |
|
40
|
|
|
this.inUserLogoutRepository = inUserLogoutRepository; |
|
41
|
|
|
this.passwordEncoder = passwordEncoder; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
Pair<Boolean, InUserEmail> readOrCreateInUserEmail(TokenEmailRequestDTO tokenEmailRequest) { |
|
45
|
|
|
final InUserEmail inUserEmail; |
|
46
|
|
|
final boolean isNewLogin; |
|
47
|
|
|
final String email = tokenEmailRequest.getEmail().toLowerCase().trim(); |
|
48
|
|
|
final List<InUserEmail> inUserEmails = inUserEmailRepository.findByLogin(email); |
|
49
|
|
|
if (inUserEmails.isEmpty()) { |
|
50
|
|
|
throw new UnauthorizedException("E-mail is not registered"); |
|
51
|
|
|
} else { |
|
52
|
|
|
inUserEmail = inUserEmails.get(inUserEmails.size() - 1); |
|
53
|
|
|
if (!passwordEncoder.matches( |
|
54
|
|
|
tokenEmailRequest.getPassword(), inUserEmail.getPassword())) { |
|
55
|
|
|
throw new UnauthorizedException("Wrong password"); |
|
56
|
|
|
} |
|
57
|
|
|
final List<InUserLogout> inUserLogouts = |
|
58
|
|
|
inUserLogoutRepository.findByToken( |
|
59
|
|
|
inUserEmail |
|
60
|
|
|
.getInUser() |
|
61
|
|
|
.getInUserLogins() |
|
62
|
|
|
.get(inUserEmail.getInUser().getInUserLogins().size() - 1) |
|
63
|
|
|
.getToken()); |
|
64
|
|
|
isNewLogin = !inUserLogouts.isEmpty(); |
|
65
|
|
|
} |
|
66
|
|
|
return Pair.of(isNewLogin, inUserEmail); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
TokenEmailResponseDTO createOrReadNewToken( |
|
70
|
|
|
TokenEmailRequestDTO tokenRequest, String remoteAddr) { |
|
71
|
|
|
final Pair<Boolean, InUserEmail> inUserEmailData = readOrCreateInUserEmail(tokenRequest); |
|
72
|
|
|
final boolean isNewLogin = inUserEmailData.getFirst(); |
|
73
|
|
|
final InUserEmail inUserEmail = inUserEmailData.getSecond(); |
|
74
|
|
|
final InUserLogin inUserLogin; |
|
75
|
|
|
if (isNewLogin) { |
|
76
|
|
|
inUserLogin = new InUserLogin(); |
|
77
|
|
|
} else { |
|
78
|
|
|
inUserLogin = |
|
79
|
|
|
inUserEmail |
|
80
|
|
|
.getInUser() |
|
81
|
|
|
.getInUserLogins() |
|
82
|
|
|
.get(inUserEmail.getInUser().getInUserLogins().size() - 1); |
|
83
|
|
|
} |
|
84
|
|
|
final InUser inUser = inUserEmail.getInUser(); |
|
85
|
|
|
inUser.getInUserEmails().add(inUserEmail); |
|
86
|
|
|
inUser.getInUserLogins().add(inUserLogin); |
|
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 TokenEmailResponseDTO tokenEmailResponseDTO = new TokenEmailResponseDTO(); |
|
94
|
|
|
tokenEmailResponseDTO.setToken(inUserLogin.getToken()); |
|
95
|
|
|
final UserSignupResponseDTO user = new UserSignupResponseDTO(); |
|
96
|
|
|
user.setId(inUserEmail.getInUser().getId()); |
|
97
|
|
|
user.setName(inUserEmail.getUser_name()); |
|
98
|
|
|
user.setEmail(inUserEmail.getLogin()); |
|
99
|
|
|
user.setGender(inUserEmail.getInUser().getD_sex()); |
|
100
|
|
|
user.setAge( |
|
101
|
|
|
inUserEmail.getInUser().getAge() == null |
|
102
|
|
|
? null |
|
103
|
|
|
: inUserEmail.getInUser().getAge().intValue()); |
|
104
|
|
|
user.setBirthday(inUserEmail.getInUser().getBirthday()); |
|
105
|
|
|
user.setAvatar_dataurl(inUserEmail.getInUser().getAvatar_dataurl()); |
|
106
|
|
|
if (inUser.getD_level() != null) { |
|
107
|
|
|
user.setLevel(UserLevel.of(Integer.parseInt(inUser.getD_level()))); |
|
108
|
|
|
} |
|
109
|
|
|
user.setGoals( |
|
110
|
|
|
inUser.getInUserGoals().stream() |
|
111
|
|
|
.map( |
|
112
|
|
|
inUserGoal -> { |
|
113
|
|
|
Map<String, Integer> map = null; |
|
114
|
|
|
try { |
|
115
|
|
|
map = |
|
116
|
|
|
inUserGoal.getGoal_value() == null |
|
117
|
|
|
? null |
|
118
|
|
|
: new ObjectMapper() |
|
119
|
|
|
.readValue( |
|
120
|
|
|
inUserGoal.getGoal_value(), |
|
121
|
|
|
new TypeReference< |
|
122
|
|
|
Map< |
|
123
|
|
|
String, |
|
124
|
|
|
Integer>>() {}); |
|
125
|
|
|
} catch (IOException ex) { |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
return new UserGoalResponseDTO() |
|
128
|
|
|
.setId(inUserGoal.getGoalId()) |
|
129
|
|
|
.setValues(map); |
|
130
|
|
|
}) |
|
131
|
|
|
.collect(Collectors.toList())); |
|
132
|
|
|
user.setHeight(inUser.getHeight() == null ? null : inUser.getHeight().longValue()); |
|
133
|
|
|
user.setWeight(inUser.getWeight() == null ? null : inUser.getWeight().longValue()); |
|
134
|
|
|
tokenEmailResponseDTO.setUser(user); |
|
135
|
|
|
return tokenEmailResponseDTO; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
View Code Duplication |
void deleteToken(String token, String remoteAddr) { |
|
|
|
|
|
|
139
|
|
|
final List<InUserLogin> inUserLogins = inUserLoginRepository.findByToken(token); |
|
140
|
|
|
if (!inUserLogins.isEmpty()) { |
|
141
|
|
|
final List<InUserLogout> inUserLogouts = inUserLogoutRepository.findByToken(token); |
|
142
|
|
|
if (!inUserLogouts.isEmpty()) { |
|
143
|
|
|
throw new UnauthorizedException("Invalid token"); |
|
144
|
|
|
} |
|
145
|
|
|
InUserLogout inUserLogout = new InUserLogout(); |
|
146
|
|
|
inUserLogout.setToken(token); |
|
147
|
|
|
inUserLogout.setInUser(inUserLogins.get(inUserLogins.size() - 1).getInUser()); |
|
148
|
|
|
inUserLogout.setIp_address(remoteAddr); |
|
149
|
|
|
inUserLogoutRepository.saveAndFlush(inUserLogout); |
|
150
|
|
|
} else { |
|
151
|
|
|
throw new UnauthorizedException("Token not found"); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|