1 |
40232
|
stefania.m
|
package eu.dnetlib.repo.manager.server;
|
2 |
|
|
|
3 |
|
|
import eu.dnetlib.domain.functionality.UserProfile;
|
4 |
|
|
import eu.dnetlib.repo.manager.client.UserService;
|
5 |
40367
|
nikon.gasp
|
import eu.dnetlib.repo.manager.server.utils.EmailUtils;
|
6 |
40251
|
nikon.gasp
|
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 |
40464
|
nikon.gasp
|
import org.springframework.stereotype.Service;
|
11 |
40232
|
stefania.m
|
|
12 |
|
|
import javax.servlet.ServletConfig;
|
13 |
|
|
import javax.servlet.ServletException;
|
14 |
40268
|
nikon.gasp
|
import java.util.ArrayList;
|
15 |
40309
|
nikon.gasp
|
import java.util.List;
|
16 |
40251
|
nikon.gasp
|
import java.util.regex.Pattern;
|
17 |
40232
|
stefania.m
|
|
18 |
|
|
/**
|
19 |
40251
|
nikon.gasp
|
* Created by nikonas on 12/7/15.
|
20 |
40232
|
stefania.m
|
*/
|
21 |
40464
|
nikon.gasp
|
@Service("userService")
|
22 |
|
|
public class UserServiceImpl extends SpringGwtRemoteServiceServlet implements UserService {
|
23 |
40232
|
stefania.m
|
|
24 |
40251
|
nikon.gasp
|
private static final Logger LOGGER = Logger
|
25 |
|
|
.getLogger(UserServiceImpl.class);
|
26 |
40232
|
stefania.m
|
|
27 |
40265
|
nikon.gasp
|
@Autowired
|
28 |
40251
|
nikon.gasp
|
private UserApi userAPI;
|
29 |
|
|
|
30 |
40268
|
nikon.gasp
|
@Autowired
|
31 |
40367
|
nikon.gasp
|
private EmailUtils emailUtils;
|
32 |
40251
|
nikon.gasp
|
|
33 |
40268
|
nikon.gasp
|
|
34 |
40232
|
stefania.m
|
public void init(ServletConfig config) throws ServletException {
|
35 |
|
|
|
36 |
40251
|
nikon.gasp
|
LOGGER.info("initializing user service impl ");
|
37 |
40232
|
stefania.m
|
super.init(config);
|
38 |
40262
|
nikon.gasp
|
|
39 |
40232
|
stefania.m
|
}
|
40 |
|
|
|
41 |
40251
|
nikon.gasp
|
@Override
|
42 |
|
|
public UserProfile login(String email_username, String password) throws UserAccessException {
|
43 |
|
|
LOGGER.info("Checking credentials for user " + email_username);
|
44 |
|
|
try {
|
45 |
40232
|
stefania.m
|
|
46 |
40251
|
nikon.gasp
|
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 |
40265
|
nikon.gasp
|
throw new UserAccessException("login.InvalidPassword", UserAccessException.ErrorCode.INVALID_PASSWORD);
|
64 |
40251
|
nikon.gasp
|
}
|
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 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
71 |
|
|
|
72 |
|
|
if (e instanceof UserAccessException) {
|
73 |
40251
|
nikon.gasp
|
throw (UserAccessException) e;
|
74 |
40367
|
nikon.gasp
|
}
|
75 |
|
|
else {
|
76 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
77 |
|
|
}
|
78 |
40251
|
nikon.gasp
|
}
|
79 |
|
|
|
80 |
40232
|
stefania.m
|
}
|
81 |
40259
|
stefania.m
|
|
82 |
|
|
@Override
|
83 |
40268
|
nikon.gasp
|
public UserProfile getUserByEmail(String email) throws UserAccessException {
|
84 |
|
|
LOGGER.info("Getting user with email " + email);
|
85 |
|
|
try {
|
86 |
40259
|
stefania.m
|
|
87 |
40268
|
nikon.gasp
|
return this.userAPI.getUser(email);
|
88 |
|
|
|
89 |
|
|
} catch (Exception e) {
|
90 |
|
|
LOGGER.error("An error occurred while getting user with email " + email, e);
|
91 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
92 |
|
|
|
93 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
94 |
40268
|
nikon.gasp
|
}
|
95 |
40259
|
stefania.m
|
}
|
96 |
|
|
|
97 |
|
|
@Override
|
98 |
40268
|
nikon.gasp
|
public void register(UserProfile userProfile) throws UserAccessException {
|
99 |
40262
|
nikon.gasp
|
|
100 |
40268
|
nikon.gasp
|
try {
|
101 |
|
|
LOGGER.info("Registering user " + userProfile.getEmail());
|
102 |
|
|
|
103 |
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])?$");
|
104 |
|
|
if (!rfc2822.matcher(userProfile.getEmail().trim().toLowerCase()).matches()) {
|
105 |
|
|
throw new UserAccessException("login.notValidEmail", UserAccessException.ErrorCode.INVALID_EMAIL_FORMAT);
|
106 |
|
|
}
|
107 |
|
|
|
108 |
40268
|
nikon.gasp
|
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 |
40367
|
nikon.gasp
|
emailUtils.sendActivationEmail(userProfile, activationId);
|
119 |
40268
|
nikon.gasp
|
|
120 |
|
|
} catch (Exception e) {
|
121 |
|
|
LOGGER.error("Error while registering user " + userProfile.getEmail(), e);
|
122 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
123 |
|
|
|
124 |
40268
|
nikon.gasp
|
if (e instanceof UserAccessException)
|
125 |
|
|
throw (UserAccessException) e;
|
126 |
|
|
else
|
127 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
128 |
|
|
}
|
129 |
|
|
|
130 |
40262
|
nikon.gasp
|
}
|
131 |
|
|
|
132 |
|
|
@Override
|
133 |
40268
|
nikon.gasp
|
public void activateUser(String activationId) throws UserAccessException {
|
134 |
40265
|
nikon.gasp
|
try {
|
135 |
40268
|
nikon.gasp
|
LOGGER.info("Activating user with activation with activation id " + activationId);
|
136 |
40265
|
nikon.gasp
|
|
137 |
40268
|
nikon.gasp
|
if (!this.userAPI.activateUser(activationId))
|
138 |
|
|
throw new UserAccessException("registration.okAccountAlreadyActivation", UserAccessException.ErrorCode.ALREADY_ACTIVATED);
|
139 |
40265
|
nikon.gasp
|
} catch (Exception e) {
|
140 |
40268
|
nikon.gasp
|
LOGGER.error("Error while activating user account with activation id " + activationId, e);
|
141 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
142 |
|
|
|
143 |
40268
|
nikon.gasp
|
if (e instanceof UserAccessException)
|
144 |
|
|
throw (UserAccessException) e;
|
145 |
|
|
else
|
146 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
147 |
40265
|
nikon.gasp
|
}
|
148 |
40259
|
stefania.m
|
}
|
149 |
40262
|
nikon.gasp
|
|
150 |
|
|
@Override
|
151 |
40268
|
nikon.gasp
|
public void updateUser(UserProfile userProfile) throws UserAccessException {
|
152 |
40309
|
nikon.gasp
|
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 |
40268
|
nikon.gasp
|
|
159 |
40309
|
nikon.gasp
|
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 |
40268
|
nikon.gasp
|
}
|
176 |
|
|
|
177 |
|
|
@Override
|
178 |
40309
|
nikon.gasp
|
public void prepareResetPassword(String email) throws UserAccessException {
|
179 |
40262
|
nikon.gasp
|
|
180 |
40309
|
nikon.gasp
|
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 |
40367
|
nikon.gasp
|
emailUtils.sendResetPasswordEmail(email, securityCode);
|
190 |
|
|
|
191 |
40309
|
nikon.gasp
|
} catch (Exception e) {
|
192 |
|
|
LOGGER.error("Error while sending password recovery to user " + email, e);
|
193 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
194 |
|
|
|
195 |
40309
|
nikon.gasp
|
if (e instanceof UserAccessException)
|
196 |
|
|
throw (UserAccessException) e;
|
197 |
|
|
else
|
198 |
|
|
throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
|
199 |
|
|
}
|
200 |
40262
|
nikon.gasp
|
}
|
201 |
|
|
|
202 |
|
|
@Override
|
203 |
40309
|
nikon.gasp
|
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 |
40367
|
nikon.gasp
|
emailUtils.reportException(e);
|
216 |
|
|
|
217 |
40309
|
nikon.gasp
|
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 |
40262
|
nikon.gasp
|
public void resendActivation(String email) throws UserAccessException {
|
226 |
|
|
|
227 |
|
|
}
|
228 |
40268
|
nikon.gasp
|
|
229 |
40232
|
stefania.m
|
}
|