1 |
41049
|
nikon.gasp
|
package eu.dnetlib.repo.manager.server.services;
|
2 |
40232
|
stefania.m
|
|
3 |
|
|
import eu.dnetlib.domain.functionality.UserProfile;
|
4 |
41617
|
stefania.m
|
import eu.dnetlib.gwt.server.service.SpringGwtRemoteServiceServlet;
|
5 |
41093
|
stefania.m
|
import eu.dnetlib.repo.manager.client.services.UserService;
|
6 |
40367
|
nikon.gasp
|
import eu.dnetlib.repo.manager.server.utils.EmailUtils;
|
7 |
49691
|
antonis.le
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
8 |
40251
|
nikon.gasp
|
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 |
40464
|
nikon.gasp
|
import org.springframework.stereotype.Service;
|
13 |
40232
|
stefania.m
|
|
14 |
|
|
import javax.servlet.ServletConfig;
|
15 |
|
|
import javax.servlet.ServletException;
|
16 |
40268
|
nikon.gasp
|
import java.util.ArrayList;
|
17 |
49691
|
antonis.le
|
import java.util.Arrays;
|
18 |
40309
|
nikon.gasp
|
import java.util.List;
|
19 |
40251
|
nikon.gasp
|
import java.util.regex.Pattern;
|
20 |
40232
|
stefania.m
|
|
21 |
|
|
/**
|
22 |
40251
|
nikon.gasp
|
* Created by nikonas on 12/7/15.
|
23 |
40232
|
stefania.m
|
*/
|
24 |
40464
|
nikon.gasp
|
@Service("userService")
|
25 |
|
|
public class UserServiceImpl extends SpringGwtRemoteServiceServlet implements UserService {
|
26 |
40232
|
stefania.m
|
|
27 |
40251
|
nikon.gasp
|
private static final Logger LOGGER = Logger
|
28 |
|
|
.getLogger(UserServiceImpl.class);
|
29 |
40232
|
stefania.m
|
|
30 |
40265
|
nikon.gasp
|
@Autowired
|
31 |
40251
|
nikon.gasp
|
private UserApi userAPI;
|
32 |
|
|
|
33 |
40268
|
nikon.gasp
|
@Autowired
|
34 |
40367
|
nikon.gasp
|
private EmailUtils emailUtils;
|
35 |
40251
|
nikon.gasp
|
|
36 |
40268
|
nikon.gasp
|
|
37 |
40232
|
stefania.m
|
public void init(ServletConfig config) throws ServletException {
|
38 |
|
|
|
39 |
40251
|
nikon.gasp
|
LOGGER.info("initializing user service impl ");
|
40 |
40232
|
stefania.m
|
super.init(config);
|
41 |
40262
|
nikon.gasp
|
|
42 |
40232
|
stefania.m
|
}
|
43 |
|
|
|
44 |
40251
|
nikon.gasp
|
@Override
|
45 |
49691
|
antonis.le
|
public Tuple<UserProfile, String> login(String email_username, String password) throws UserAccessException {
|
46 |
40251
|
nikon.gasp
|
LOGGER.info("Checking credentials for user " + email_username);
|
47 |
|
|
try {
|
48 |
40232
|
stefania.m
|
|
49 |
40251
|
nikon.gasp
|
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 |
40265
|
nikon.gasp
|
throw new UserAccessException("login.InvalidPassword", UserAccessException.ErrorCode.INVALID_PASSWORD);
|
67 |
40251
|
nikon.gasp
|
}
|
68 |
|
|
|
69 |
49691
|
antonis.le
|
UserProfile userProfile = this.userAPI.getUser(email);
|
70 |
|
|
String role = "";
|
71 |
40251
|
nikon.gasp
|
|
72 |
49691
|
antonis.le
|
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 |
40251
|
nikon.gasp
|
} catch (Exception e) {
|
79 |
|
|
LOGGER.error("An error occurred while checking credentials for user " + email_username, e);
|
80 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
81 |
|
|
|
82 |
|
|
if (e instanceof UserAccessException) {
|
83 |
40251
|
nikon.gasp
|
throw (UserAccessException) e;
|
84 |
40367
|
nikon.gasp
|
}
|
85 |
|
|
else {
|
86 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
87 |
|
|
}
|
88 |
40251
|
nikon.gasp
|
}
|
89 |
|
|
|
90 |
40232
|
stefania.m
|
}
|
91 |
40259
|
stefania.m
|
|
92 |
|
|
@Override
|
93 |
49691
|
antonis.le
|
public Tuple<UserProfile, String> getUserByEmail(String email) throws UserAccessException {
|
94 |
40268
|
nikon.gasp
|
LOGGER.info("Getting user with email " + email);
|
95 |
|
|
try {
|
96 |
40259
|
stefania.m
|
|
97 |
49691
|
antonis.le
|
UserProfile userProfile = this.userAPI.getUser(email);
|
98 |
|
|
String role = "";
|
99 |
40268
|
nikon.gasp
|
|
100 |
49691
|
antonis.le
|
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 |
40268
|
nikon.gasp
|
} catch (Exception e) {
|
107 |
|
|
LOGGER.error("An error occurred while getting user with email " + email, e);
|
108 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
109 |
|
|
|
110 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
111 |
40268
|
nikon.gasp
|
}
|
112 |
40259
|
stefania.m
|
}
|
113 |
|
|
|
114 |
|
|
@Override
|
115 |
40268
|
nikon.gasp
|
public void register(UserProfile userProfile) throws UserAccessException {
|
116 |
40262
|
nikon.gasp
|
|
117 |
40268
|
nikon.gasp
|
try {
|
118 |
|
|
LOGGER.info("Registering user " + userProfile.getEmail());
|
119 |
|
|
|
120 |
40308
|
nikon.gasp
|
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 |
40268
|
nikon.gasp
|
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 |
40367
|
nikon.gasp
|
emailUtils.sendActivationEmail(userProfile, activationId);
|
136 |
40268
|
nikon.gasp
|
|
137 |
|
|
} catch (Exception e) {
|
138 |
|
|
LOGGER.error("Error while registering user " + userProfile.getEmail(), e);
|
139 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
140 |
|
|
|
141 |
40268
|
nikon.gasp
|
if (e instanceof UserAccessException)
|
142 |
|
|
throw (UserAccessException) e;
|
143 |
|
|
else
|
144 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
145 |
|
|
}
|
146 |
|
|
|
147 |
40262
|
nikon.gasp
|
}
|
148 |
|
|
|
149 |
|
|
@Override
|
150 |
40268
|
nikon.gasp
|
public void activateUser(String activationId) throws UserAccessException {
|
151 |
40265
|
nikon.gasp
|
try {
|
152 |
40268
|
nikon.gasp
|
LOGGER.info("Activating user with activation with activation id " + activationId);
|
153 |
40265
|
nikon.gasp
|
|
154 |
40268
|
nikon.gasp
|
if (!this.userAPI.activateUser(activationId))
|
155 |
|
|
throw new UserAccessException("registration.okAccountAlreadyActivation", UserAccessException.ErrorCode.ALREADY_ACTIVATED);
|
156 |
40265
|
nikon.gasp
|
} catch (Exception e) {
|
157 |
40268
|
nikon.gasp
|
LOGGER.error("Error while activating user account with activation id " + activationId, e);
|
158 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
159 |
|
|
|
160 |
40268
|
nikon.gasp
|
if (e instanceof UserAccessException)
|
161 |
|
|
throw (UserAccessException) e;
|
162 |
|
|
else
|
163 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
164 |
40265
|
nikon.gasp
|
}
|
165 |
40259
|
stefania.m
|
}
|
166 |
40262
|
nikon.gasp
|
|
167 |
|
|
@Override
|
168 |
40268
|
nikon.gasp
|
public void updateUser(UserProfile userProfile) throws UserAccessException {
|
169 |
40309
|
nikon.gasp
|
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 |
40268
|
nikon.gasp
|
|
176 |
40309
|
nikon.gasp
|
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 |
40268
|
nikon.gasp
|
}
|
193 |
|
|
|
194 |
|
|
@Override
|
195 |
40309
|
nikon.gasp
|
public void prepareResetPassword(String email) throws UserAccessException {
|
196 |
40262
|
nikon.gasp
|
|
197 |
40309
|
nikon.gasp
|
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 |
40367
|
nikon.gasp
|
emailUtils.sendResetPasswordEmail(email, securityCode);
|
207 |
|
|
|
208 |
40309
|
nikon.gasp
|
} catch (Exception e) {
|
209 |
|
|
LOGGER.error("Error while sending password recovery to user " + email, e);
|
210 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
211 |
|
|
|
212 |
40309
|
nikon.gasp
|
if (e instanceof UserAccessException)
|
213 |
|
|
throw (UserAccessException) e;
|
214 |
|
|
else
|
215 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
216 |
|
|
}
|
217 |
40262
|
nikon.gasp
|
}
|
218 |
|
|
|
219 |
|
|
@Override
|
220 |
40309
|
nikon.gasp
|
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 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
233 |
|
|
|
234 |
40309
|
nikon.gasp
|
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 |
40262
|
nikon.gasp
|
public void resendActivation(String email) throws UserAccessException {
|
243 |
|
|
|
244 |
|
|
}
|
245 |
40268
|
nikon.gasp
|
|
246 |
40232
|
stefania.m
|
}
|