1
|
package eu.dnetlib.repo.manager.server;
|
2
|
|
3
|
import eu.dnetlib.domain.functionality.UserProfile;
|
4
|
import eu.dnetlib.repo.manager.client.UserService;
|
5
|
import eu.dnetlib.repo.manager.server.utils.EmailUtils;
|
6
|
import eu.dnetlib.repo.manager.shared.UserAccessException;
|
7
|
import eu.dnetlib.users.UserApi;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.stereotype.Service;
|
11
|
|
12
|
import javax.servlet.ServletConfig;
|
13
|
import javax.servlet.ServletException;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.List;
|
16
|
import java.util.regex.Pattern;
|
17
|
|
18
|
/**
|
19
|
* Created by nikonas on 12/7/15.
|
20
|
*/
|
21
|
@Service("userService")
|
22
|
public class UserServiceImpl extends SpringGwtRemoteServiceServlet implements UserService {
|
23
|
|
24
|
private static final Logger LOGGER = Logger
|
25
|
.getLogger(UserServiceImpl.class);
|
26
|
|
27
|
@Autowired
|
28
|
private UserApi userAPI;
|
29
|
|
30
|
@Autowired
|
31
|
private EmailUtils emailUtils;
|
32
|
|
33
|
|
34
|
public void init(ServletConfig config) throws ServletException {
|
35
|
|
36
|
LOGGER.info("initializing user service impl ");
|
37
|
super.init(config);
|
38
|
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public UserProfile login(String email_username, String password) throws UserAccessException {
|
43
|
LOGGER.info("Checking credentials for user " + email_username);
|
44
|
try {
|
45
|
|
46
|
String email = email_username;
|
47
|
|
48
|
Pattern rfc2822 = Pattern.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
|
49
|
if (!rfc2822.matcher(email_username.trim().toLowerCase()).matches()) {
|
50
|
LOGGER.debug("user logged in using username");
|
51
|
email = this.userAPI.getEmailFromUsername(email_username);
|
52
|
}
|
53
|
if (email == null) {
|
54
|
throw new UserAccessException("login.userNotExists", UserAccessException.ErrorCode.INVALID_USERNAME);
|
55
|
}
|
56
|
if (!this.userAPI.userExists(email)) {
|
57
|
throw new UserAccessException("login.userNotExists", UserAccessException.ErrorCode.INVALID_USERNAME);
|
58
|
}
|
59
|
if (!this.userAPI.isUserActivated(email)) {
|
60
|
throw new UserAccessException("login.notActivated", UserAccessException.ErrorCode.NOT_ACTIVATED);
|
61
|
}
|
62
|
if (!this.userAPI.correctCreds(email, password)) {
|
63
|
throw new UserAccessException("login.InvalidPassword", UserAccessException.ErrorCode.INVALID_PASSWORD);
|
64
|
}
|
65
|
|
66
|
return this.userAPI.getUser(email);
|
67
|
|
68
|
} catch (Exception e) {
|
69
|
LOGGER.error("An error occurred while checking credentials for user " + email_username, e);
|
70
|
emailUtils.reportException(e);
|
71
|
|
72
|
if (e instanceof UserAccessException) {
|
73
|
throw (UserAccessException) e;
|
74
|
}
|
75
|
else {
|
76
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
77
|
}
|
78
|
}
|
79
|
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
public UserProfile getUserByEmail(String email) throws UserAccessException {
|
84
|
LOGGER.info("Getting user with email " + email);
|
85
|
try {
|
86
|
|
87
|
return this.userAPI.getUser(email);
|
88
|
|
89
|
} catch (Exception e) {
|
90
|
LOGGER.error("An error occurred while getting user with email " + email, e);
|
91
|
emailUtils.reportException(e);
|
92
|
|
93
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
@Override
|
98
|
public void register(UserProfile userProfile) throws UserAccessException {
|
99
|
|
100
|
try {
|
101
|
LOGGER.info("Registering user " + userProfile.getEmail());
|
102
|
|
103
|
Pattern rfc2822 = Pattern.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
|
104
|
if (!rfc2822.matcher(userProfile.getEmail().trim().toLowerCase()).matches()) {
|
105
|
throw new UserAccessException("login.notValidEmail", UserAccessException.ErrorCode.INVALID_EMAIL_FORMAT);
|
106
|
}
|
107
|
|
108
|
if (this.userAPI.usernameExists(userProfile.getUsername())) {
|
109
|
throw new UserAccessException("login.usernameAlreadyExists", UserAccessException.ErrorCode.USERNAME_ALREADY_EXISTS);
|
110
|
}
|
111
|
if (this.userAPI.userExists(userProfile.getEmail())) {
|
112
|
throw new UserAccessException("login.mailAlreadyExists", UserAccessException.ErrorCode.MAIL_ALREADY_EXISTS);
|
113
|
}
|
114
|
|
115
|
// String activationId = "TEST";
|
116
|
String activationId = this.userAPI.addUser(userProfile.getUsername(), userProfile.getEmail(), userProfile.getPassword(), userProfile.getFirstname(), userProfile.getLastname(), userProfile.getInstitution());
|
117
|
|
118
|
emailUtils.sendActivationEmail(userProfile, activationId);
|
119
|
|
120
|
} catch (Exception e) {
|
121
|
LOGGER.error("Error while registering user " + userProfile.getEmail(), e);
|
122
|
emailUtils.reportException(e);
|
123
|
|
124
|
if (e instanceof UserAccessException)
|
125
|
throw (UserAccessException) e;
|
126
|
else
|
127
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
128
|
}
|
129
|
|
130
|
}
|
131
|
|
132
|
@Override
|
133
|
public void activateUser(String activationId) throws UserAccessException {
|
134
|
try {
|
135
|
LOGGER.info("Activating user with activation with activation id " + activationId);
|
136
|
|
137
|
if (!this.userAPI.activateUser(activationId))
|
138
|
throw new UserAccessException("registration.okAccountAlreadyActivation", UserAccessException.ErrorCode.ALREADY_ACTIVATED);
|
139
|
} catch (Exception e) {
|
140
|
LOGGER.error("Error while activating user account with activation id " + activationId, e);
|
141
|
emailUtils.reportException(e);
|
142
|
|
143
|
if (e instanceof UserAccessException)
|
144
|
throw (UserAccessException) e;
|
145
|
else
|
146
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
147
|
}
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public void updateUser(UserProfile userProfile) throws UserAccessException {
|
152
|
try {
|
153
|
LOGGER.info("Editing user " + userProfile.getUsername());
|
154
|
Pattern rfc2822 = Pattern.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
|
155
|
if (!rfc2822.matcher(userProfile.getEmail().trim().toLowerCase()).matches()) {
|
156
|
throw new UserAccessException("login.notValidEmail", UserAccessException.ErrorCode.INVALID_EMAIL_FORMAT);
|
157
|
}
|
158
|
|
159
|
String currentEmail = this.userAPI.getEmailFromUsername(userProfile.getUsername());
|
160
|
if (!userProfile.getEmail().equalsIgnoreCase(currentEmail)) {
|
161
|
if (this.userAPI.userExists(userProfile.getEmail())) {
|
162
|
throw new UserAccessException("login.mailAlreadyExists", UserAccessException.ErrorCode.MAIL_ALREADY_EXISTS);
|
163
|
}
|
164
|
}
|
165
|
|
166
|
this.userAPI.editUser(userProfile);
|
167
|
|
168
|
} catch (Exception e) {
|
169
|
LOGGER.error("Error while editing user " + userProfile.getUsername(), e);
|
170
|
if (e instanceof UserAccessException)
|
171
|
throw (UserAccessException) e;
|
172
|
else
|
173
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public void prepareResetPassword(String email) throws UserAccessException {
|
179
|
|
180
|
try {
|
181
|
LOGGER.debug("Sending password recovery to user " + email);
|
182
|
if (!this.userAPI.userExists(email)) {
|
183
|
throw new UserAccessException("login.userNotExists", UserAccessException.ErrorCode.INVALID_USERNAME);
|
184
|
}
|
185
|
List<String> to = new ArrayList<String>();
|
186
|
to.add(email);
|
187
|
String securityCode = this.userAPI.prepareResetPassword(email);
|
188
|
|
189
|
emailUtils.sendResetPasswordEmail(email, securityCode);
|
190
|
|
191
|
} catch (Exception e) {
|
192
|
LOGGER.error("Error while sending password recovery to user " + email, e);
|
193
|
emailUtils.reportException(e);
|
194
|
|
195
|
if (e instanceof UserAccessException)
|
196
|
throw (UserAccessException) e;
|
197
|
else
|
198
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
199
|
}
|
200
|
}
|
201
|
|
202
|
@Override
|
203
|
public void resetPassword(String securityCode, String password) throws UserAccessException {
|
204
|
try {
|
205
|
LOGGER.debug("Reseting password with security code " + securityCode);
|
206
|
|
207
|
if (securityCode.length() == 0) {
|
208
|
throw new UserAccessException("resetPassword.wrongSecurityCode", UserAccessException.ErrorCode.WRONG_SECURITY_CODE);
|
209
|
}
|
210
|
|
211
|
this.userAPI.resetPassword(securityCode, password);
|
212
|
|
213
|
} catch (Exception e) {
|
214
|
LOGGER.error("Error while reseting password with security code " + securityCode);
|
215
|
emailUtils.reportException(e);
|
216
|
|
217
|
if (e instanceof UserAccessException)
|
218
|
throw (UserAccessException) e;
|
219
|
else
|
220
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
221
|
}
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public void resendActivation(String email) throws UserAccessException {
|
226
|
|
227
|
}
|
228
|
|
229
|
}
|