1
|
package eu.dnetlib.goldoa.service;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Affiliation;
|
4
|
import eu.dnetlib.goldoa.domain.PersonManagerException;
|
5
|
import eu.dnetlib.goldoa.domain.Role;
|
6
|
import eu.dnetlib.goldoa.domain.User;
|
7
|
import eu.dnetlib.goldoa.domain.UserRole;
|
8
|
//import eu.dnetlib.goldoa.service.dao.OrganizationDAO;
|
9
|
import eu.dnetlib.goldoa.service.dao.UserDAO;
|
10
|
//import eu.dnetlib.goldoa.service.dao.ProjectDAO;
|
11
|
//import eu.dnetlib.goldoa.service.utils.EmailUtils;
|
12
|
import org.apache.commons.codec.digest.DigestUtils;
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
14
|
import org.springframework.dao.EmptyResultDataAccessException;
|
15
|
import org.springframework.stereotype.Repository;
|
16
|
import org.springframework.transaction.annotation.Transactional;
|
17
|
|
18
|
//import javax.mail.MessagingException;
|
19
|
import java.math.BigInteger;
|
20
|
import java.security.SecureRandom;
|
21
|
import java.util.ArrayList;
|
22
|
import java.util.List;
|
23
|
import java.util.UUID;
|
24
|
import java.util.concurrent.ExecutorService;
|
25
|
|
26
|
/**
|
27
|
* Created by antleb on 3/4/15.
|
28
|
*/
|
29
|
|
30
|
public class UserManagerImpl implements UserManager {
|
31
|
|
32
|
@Autowired
|
33
|
private UserDAO userDAO;
|
34
|
/*@Autowired
|
35
|
private ProjectDAO projectDAO;
|
36
|
@Autowired
|
37
|
private OrganizationDAO organizationDAO;
|
38
|
@Autowired
|
39
|
private PublisherManager publisherManager;
|
40
|
@Autowired
|
41
|
private OrganizationManager organizationManager;
|
42
|
@Autowired
|
43
|
private EmailUtils emailUtils;*/
|
44
|
|
45
|
@Autowired
|
46
|
ExecutorService executorService;
|
47
|
|
48
|
private int tokenTTL = 30;
|
49
|
|
50
|
public static int generateId(Affiliation affiliation) {
|
51
|
StringBuilder sb = new StringBuilder();
|
52
|
|
53
|
if (affiliation.getPerson() != null)
|
54
|
sb.append(affiliation.getPerson().getId());
|
55
|
if (affiliation.getOrganization() != null)
|
56
|
sb.append(affiliation.getOrganization().getId());
|
57
|
|
58
|
return DigestUtils.md5Hex(sb.toString()).hashCode();
|
59
|
}
|
60
|
|
61
|
@Override
|
62
|
public User getById(String personId) throws PersonManagerException {
|
63
|
return userDAO.getUserById(personId);
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public User getByEmail(final String email) throws PersonManagerException {
|
68
|
return userDAO.getUserByEmail(email);
|
69
|
}
|
70
|
|
71
|
@Override
|
72
|
public User saveUser(final User user) throws PersonManagerException {
|
73
|
final List<UserRole> previousRoles;
|
74
|
|
75
|
if (user.getId() == null) {
|
76
|
user.setId(generateId(user));
|
77
|
user.setSource("portal");
|
78
|
|
79
|
previousRoles = new ArrayList<UserRole>();
|
80
|
} else {
|
81
|
previousRoles = userDAO.getUserById(user.getId()).getRoles();
|
82
|
}
|
83
|
|
84
|
userDAO.saveUser(user);
|
85
|
|
86
|
// deletePersonRelations(user);
|
87
|
// savePersonRelations(user);
|
88
|
//
|
89
|
// loadPersonRelations(user);
|
90
|
|
91
|
// executorService.submit(new Runnable() {
|
92
|
// @Override
|
93
|
// public void run() {
|
94
|
// try {
|
95
|
// List<Role> roles = new ArrayList<Role>();
|
96
|
//
|
97
|
// for (UserRole pRole : user.getRoles()) {
|
98
|
// if (!previousRoles.contains(pRole) && !pRole.isApproved())
|
99
|
// roles.add(pRole.getRole());
|
100
|
// }
|
101
|
//
|
102
|
// if (roles.size() > 0) {
|
103
|
// List<User> moderators = getModerators();
|
104
|
//
|
105
|
// emailUtils.sendUserRoleRequestedEmail(user, roles);
|
106
|
//
|
107
|
// for (User moderator : moderators)
|
108
|
// emailUtils.sendModeratorRoleRequestedEmail(moderator, user, roles);
|
109
|
// }
|
110
|
// } catch (MessagingException e) {
|
111
|
// e.printStackTrace();
|
112
|
// }
|
113
|
// }
|
114
|
// });
|
115
|
|
116
|
return user;
|
117
|
}
|
118
|
|
119
|
@Override
|
120
|
public void register(final User user) throws PersonManagerException {
|
121
|
final String token = UUID.randomUUID().toString();
|
122
|
|
123
|
/*try {
|
124
|
User u = userDAO.getUserByEmail(user.getEmail());
|
125
|
if (u != null)
|
126
|
throw new PersonManagerException(PersonManagerException.ErrorCause.ALREADY_EXISTS);
|
127
|
} catch (EmptyResultDataAccessException e) {
|
128
|
} catch (PersonManagerException e) {
|
129
|
throw e;
|
130
|
} catch (RuntimeException e) {
|
131
|
e.printStackTrace();
|
132
|
throw e;
|
133
|
}
|
134
|
*/
|
135
|
this.saveUser(user);
|
136
|
userDAO.saveToken(user.getEmail(), token, tokenTTL);
|
137
|
|
138
|
executorService.submit(new Runnable() {
|
139
|
@Override
|
140
|
public void run() {
|
141
|
// try {
|
142
|
// emailUtils.sendActivationEmail(user, token);
|
143
|
// } catch (MessagingException e) {
|
144
|
// e.printStackTrace();
|
145
|
// }
|
146
|
}
|
147
|
});
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public User login(String email, String password) throws PersonManagerException {
|
152
|
|
153
|
if (userDAO.verifyLogin(email, password)) {
|
154
|
User user = getByEmail(email);
|
155
|
|
156
|
if (user.isActive())
|
157
|
return user;
|
158
|
else
|
159
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_ACTIVATED);
|
160
|
} else {
|
161
|
throw new PersonManagerException(PersonManagerException.ErrorCause.WRONG_EMAIL_OR_PASSWORD);
|
162
|
}
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
public User activate(final String email, final String token) throws PersonManagerException {
|
167
|
|
168
|
if (!userDAO.verifyToken(email, token))
|
169
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
170
|
else {
|
171
|
User user = userDAO.getUserByEmail(email);
|
172
|
|
173
|
userDAO.activateUser(user.getEmail());
|
174
|
userDAO.deleteToken(user.getEmail(), token);
|
175
|
}
|
176
|
|
177
|
return getByEmail(email);
|
178
|
}
|
179
|
|
180
|
@Override
|
181
|
public void resetPassword(final String email) throws PersonManagerException {
|
182
|
SecureRandom random = new SecureRandom();
|
183
|
final String newPassword = new BigInteger(50, random).toString(32);
|
184
|
|
185
|
if (!userDAO.updatePassword(newPassword, email)) {
|
186
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
187
|
}
|
188
|
|
189
|
/* executorService.submit(new Runnable() {
|
190
|
@Override
|
191
|
public void run() {
|
192
|
User user = userDAO.getUserByEmail(email);
|
193
|
|
194
|
try {
|
195
|
emailUtils.sendResetPasswordEmail(user, newPassword);
|
196
|
} catch (MessagingException e) {
|
197
|
e.printStackTrace();
|
198
|
}
|
199
|
}
|
200
|
});*/
|
201
|
}
|
202
|
|
203
|
@Override
|
204
|
public List<Role> getRoles() {
|
205
|
return userDAO.getRoles();
|
206
|
}
|
207
|
|
208
|
@Override
|
209
|
public List<User> getModerators() {
|
210
|
return userDAO.getModerators();
|
211
|
}
|
212
|
|
213
|
@Override
|
214
|
public List<User> getAccountingOfficers() {
|
215
|
return userDAO.getAccountingOfficers();
|
216
|
}
|
217
|
|
218
|
@Override
|
219
|
public void activateUser(String userId) {
|
220
|
userDAO.activateUser(userId);
|
221
|
}
|
222
|
|
223
|
@Override
|
224
|
public void activateUsers(List<String> userIds) {
|
225
|
for (String userId : userIds) {
|
226
|
userDAO.activateUser(userId);
|
227
|
}
|
228
|
}
|
229
|
|
230
|
@Override
|
231
|
public void deactivateUser(String email) {
|
232
|
userDAO.deactivateUser(email);
|
233
|
}
|
234
|
|
235
|
@Override
|
236
|
public void deactivateUsers(List<String> emails) {
|
237
|
for (String email : emails) {
|
238
|
userDAO.deactivateUser(email);
|
239
|
}
|
240
|
}
|
241
|
|
242
|
@Override
|
243
|
public void acceptUserRole(final String email, final String roleId) {
|
244
|
userDAO.acceptUserRole(email, roleId);
|
245
|
|
246
|
/*executorService.submit(new Runnable() {
|
247
|
@Override
|
248
|
public void run() {
|
249
|
User user = userDAO.getUserByEmail(email);
|
250
|
Role role = userDAO.getRole(roleId);
|
251
|
List<User> moderators = getModerators();
|
252
|
|
253
|
try {
|
254
|
emailUtils.sendUserRoleAcceptedEmail(user, role);
|
255
|
|
256
|
for (User moderator : moderators)
|
257
|
emailUtils.sendModeratorRoleAcceptedEmail(moderator, user, role);
|
258
|
} catch (MessagingException e) {
|
259
|
e.printStackTrace();
|
260
|
}
|
261
|
}
|
262
|
});*/
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
public void rejectRole(final String email, final String roleId) {
|
267
|
userDAO.rejectUserRole(email, roleId);
|
268
|
|
269
|
/*executorService.submit(new Runnable() {
|
270
|
@Override
|
271
|
public void run() {
|
272
|
User user = userDAO.getUserByEmail(email);
|
273
|
Role role = userDAO.getRole(roleId);
|
274
|
List<User> moderators = getModerators();
|
275
|
|
276
|
try {
|
277
|
emailUtils.sendUserRoleRejectedEmail(user, role);
|
278
|
|
279
|
for (User moderator : moderators)
|
280
|
emailUtils.sendModeratorRoleRejectedEmail(moderator, user, role);
|
281
|
} catch (MessagingException e) {
|
282
|
e.printStackTrace();
|
283
|
}
|
284
|
}
|
285
|
});*/
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
public List<User> getUsers() {
|
290
|
List<User> users = userDAO.getUsers();
|
291
|
|
292
|
for (User user : users) {
|
293
|
loadPersonRelations(user);
|
294
|
}
|
295
|
|
296
|
return users;
|
297
|
}
|
298
|
|
299
|
@Override
|
300
|
public String generateId(User user) {
|
301
|
StringBuilder sb = new StringBuilder();
|
302
|
|
303
|
sb.append(user.getFirstname()).append(user.getLastname()).append(user.getEmail());
|
304
|
return "portal::" + DigestUtils.md5Hex(sb.toString());
|
305
|
}
|
306
|
|
307
|
private void deletePersonRelations(final User user) {
|
308
|
userDAO.deleteAffiliations(user.getEmail());
|
309
|
userDAO.deletePersonRoles(user.getEmail());
|
310
|
userDAO.removeProjectCoordinators(user.getEmail());
|
311
|
}
|
312
|
|
313
|
private void savePersonRelations(final User user) throws PersonManagerException {
|
314
|
// List<String> projectIds = new ArrayList<String>();
|
315
|
|
316
|
// if (user.getProjectCoordinators() != null)
|
317
|
// for (Project project : user.getProjectCoordinators())
|
318
|
// projectIds.add(project.getId());
|
319
|
|
320
|
// if (user.getPublisher() != null && user.getPublisher().getId() == null)
|
321
|
// publisherManager.savePublisher(user.getPublisher());
|
322
|
//
|
323
|
// for (Affiliation affiliation : user.getAffiliations()) {
|
324
|
// if (affiliation.getOrganization() != null && affiliation.getOrganization().getId() == null)
|
325
|
// try {
|
326
|
// organizationManager.saveOrganization(affiliation.getOrganization());
|
327
|
// } catch (OrganizationManagerException e) {
|
328
|
// e.printStackTrace();
|
329
|
// }
|
330
|
// }
|
331
|
// userDAO.saveProjectCoordinators(user.getEmail(), user.getProjectCoordinators());
|
332
|
// userDAO.saveUserRoles(user.getEmail(), user.getRoles());
|
333
|
// userDAO.saveAffiliations(user, user.getAffiliations());
|
334
|
//savePerson(user);
|
335
|
|
336
|
}
|
337
|
|
338
|
private void loadPersonRelations(User user) {
|
339
|
//
|
340
|
// if (user.getPublisher() != null && user.getPublisher().getId() != null) {
|
341
|
// user.setPublisher(publisherManager.getPublisher(user.getPublisher().getId()));
|
342
|
// } else {
|
343
|
// user.setPublisher(null);
|
344
|
// }
|
345
|
|
346
|
// user.setCoordinatedProjects(projectDAO.getProjectsCoordinatedBy(user.getId()));
|
347
|
|
348
|
// if (user.getAffiliations() != null) {
|
349
|
// for (Affiliation affiliation : user.getAffiliations()) {
|
350
|
// if (affiliation.getOrganization() != null)
|
351
|
// affiliation.setOrganization(organizationDAO.getOrganization(affiliation.getOrganization().getId()));
|
352
|
// }
|
353
|
// }
|
354
|
//
|
355
|
// if (user.getRoles() != null)
|
356
|
// for (int i = 0; i < user.getRoles().size(); i++) {
|
357
|
// user.getRoles().get(i).setRole(userDAO.getRole(user.getRoles().get(i).getRole().getId()));
|
358
|
// }
|
359
|
}
|
360
|
|
361
|
public int getTokenTTL() {
|
362
|
return tokenTTL;
|
363
|
}
|
364
|
|
365
|
public void setTokenTTL(int tokenTTL) {
|
366
|
this.tokenTTL = tokenTTL;
|
367
|
}
|
368
|
}
|