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