1
|
package eu.dnetlib.openaire.user.utils;
|
2
|
|
3
|
import eu.dnetlib.openaire.user.UserVerification;
|
4
|
import eu.dnetlib.openaire.user.dao.UserVerificationDAO;
|
5
|
import eu.dnetlib.openaire.user.store.DataSourceConnector;
|
6
|
import org.apache.log4j.Logger;
|
7
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
import org.springframework.stereotype.Component;
|
9
|
|
10
|
import java.sql.SQLException;
|
11
|
import java.sql.Timestamp;
|
12
|
import java.time.LocalDateTime;
|
13
|
import java.time.temporal.ChronoUnit;
|
14
|
|
15
|
/**
|
16
|
* Created by kiatrop on 2/10/2017.
|
17
|
*/
|
18
|
@Component(value = "verificationActions")
|
19
|
public class VerificationActions {
|
20
|
|
21
|
@Autowired
|
22
|
private DataSourceConnector dataSourceConnector;
|
23
|
|
24
|
private Logger logger = Logger.getLogger(VerificationActions.class);
|
25
|
|
26
|
@Autowired
|
27
|
private UserVerificationDAO userVerificationDAO;
|
28
|
|
29
|
/**
|
30
|
* Adds new row in the verification table
|
31
|
*/
|
32
|
public void addVerificationEntry(String username, String verificationCode, Timestamp date) {
|
33
|
|
34
|
try {
|
35
|
UserVerification userVerification = new UserVerification(username);
|
36
|
logger.info("verificationCode: " + verificationCode);
|
37
|
userVerification.setVerificationCode(verificationCode);
|
38
|
userVerification.setDate(date);
|
39
|
|
40
|
userVerificationDAO.insert(userVerification);
|
41
|
|
42
|
logger.info("Insert user: " + username);
|
43
|
|
44
|
} catch (SQLException e) {;
|
45
|
logger.error("Fail to insert user.", e);
|
46
|
}
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Updates the row of verification table with the new date for the given username
|
51
|
*/
|
52
|
public void updateVerificationEntry(String username, String verificationCode, Timestamp date) {
|
53
|
|
54
|
try {
|
55
|
|
56
|
// Get userId to update user
|
57
|
UserVerification userVerificationOld = userVerificationDAO.fetchByUsername(username);
|
58
|
UserVerification userVerification = new UserVerification(username);
|
59
|
userVerification.setVerificationCode(verificationCode);
|
60
|
userVerification.setDate(date);
|
61
|
userVerification.setId(userVerificationOld.getId());
|
62
|
|
63
|
userVerificationDAO.update(userVerification);
|
64
|
|
65
|
//logger.info("Update user: " + username + " with verification code " + verificationCode);
|
66
|
|
67
|
} catch (SQLException e) {
|
68
|
logger.error("Fail to update user.", e);
|
69
|
}
|
70
|
}
|
71
|
|
72
|
|
73
|
/**
|
74
|
* Checks if the verification row exists for the given username
|
75
|
*/
|
76
|
public boolean verificationEntryExists(String username) {
|
77
|
|
78
|
try {
|
79
|
UserVerification userVerification = userVerificationDAO.fetchByUsername(username);
|
80
|
|
81
|
if (userVerification == null) {
|
82
|
logger.info("There is no user with username: " + username);
|
83
|
return false;
|
84
|
}
|
85
|
else {
|
86
|
logger.info("User: " + username + " was found!");
|
87
|
return true;
|
88
|
}
|
89
|
} catch (SQLException e) {
|
90
|
logger.error("Fail to search user.", e);
|
91
|
}
|
92
|
return true;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* Checks if the verification row exists for the given username
|
97
|
*/
|
98
|
public boolean verificationCodeIsCorrect(String username, String verificationCode) {
|
99
|
|
100
|
try {
|
101
|
UserVerification userVerification = userVerificationDAO.fetchByUsername(username);
|
102
|
|
103
|
if (userVerification == null) {
|
104
|
logger.info("There is no user with username: " + username);
|
105
|
return false;
|
106
|
}
|
107
|
else {
|
108
|
logger.info("User: " + username + " was found!");
|
109
|
|
110
|
if (verificationCode.equals(userVerification.getVerificationCode())) {
|
111
|
logger.info("Verification Code is correct!");
|
112
|
//logger.info("... and verification code: " + verificationCode + " is correct!");
|
113
|
return true;
|
114
|
}
|
115
|
}
|
116
|
} catch (SQLException e) {
|
117
|
logger.error("Fail to search user.", e);
|
118
|
}
|
119
|
return false;
|
120
|
}
|
121
|
|
122
|
public boolean verificationCodeHasExpired(String username){
|
123
|
|
124
|
try {
|
125
|
UserVerification userVerification = userVerificationDAO.fetchByUsername(username);
|
126
|
LocalDateTime date = userVerification.getDate().toLocalDateTime();
|
127
|
|
128
|
long nHours = ChronoUnit.HOURS.between(date, LocalDateTime.now());
|
129
|
|
130
|
if (nHours > (long)24)
|
131
|
return true;
|
132
|
else
|
133
|
return false;
|
134
|
|
135
|
} catch (SQLException e) {
|
136
|
logger.error("Fail to search user.", e);
|
137
|
}
|
138
|
return true;
|
139
|
}
|
140
|
|
141
|
public DataSourceConnector getDataSourceConnector() {
|
142
|
return dataSourceConnector;
|
143
|
}
|
144
|
|
145
|
public void setDataSourceConnector(DataSourceConnector dataSourceConnector) {
|
146
|
this.dataSourceConnector = dataSourceConnector;
|
147
|
}
|
148
|
}
|