1 |
35019
|
antonis.le
|
package eu.dnetlib.goldoa.service;
|
2 |
|
|
|
3 |
45403
|
panagiotis
|
|
4 |
45438
|
panagiotis
|
import eu.dnetlib.goldoa.domain.*;
|
5 |
45011
|
panagiotis
|
import eu.dnetlib.goldoa.service.dao.UserDAO;
|
6 |
35749
|
antonis.le
|
import org.apache.commons.codec.digest.DigestUtils;
|
7 |
35478
|
antonis.le
|
import org.springframework.beans.factory.annotation.Autowired;
|
8 |
45074
|
panagiotis
|
import org.springframework.stereotype.Service;
|
9 |
35159
|
antonis.le
|
import org.springframework.transaction.annotation.Transactional;
|
10 |
35043
|
antonis.le
|
|
11 |
45438
|
panagiotis
|
import javax.mail.MessagingException;
|
12 |
35163
|
antonis.le
|
import java.math.BigInteger;
|
13 |
|
|
import java.security.SecureRandom;
|
14 |
45438
|
panagiotis
|
import java.util.ArrayList;
|
15 |
35156
|
antonis.le
|
import java.util.List;
|
16 |
35160
|
antonis.le
|
import java.util.UUID;
|
17 |
36198
|
antonis.le
|
import java.util.concurrent.ExecutorService;
|
18 |
35019
|
antonis.le
|
|
19 |
45403
|
panagiotis
|
//import eu.dnetlib.goldoa.service.dao.OrganizationDAO;
|
20 |
|
|
//import eu.dnetlib.goldoa.service.dao.ProjectDAO;
|
21 |
|
|
//import eu.dnetlib.goldoa.service.utils.EmailUtils;
|
22 |
45083
|
panagiotis
|
|
23 |
35019
|
antonis.le
|
/**
|
24 |
|
|
* Created by antleb on 3/4/15.
|
25 |
|
|
*/
|
26 |
45074
|
panagiotis
|
@Service
|
27 |
45080
|
panagiotis
|
@Transactional
|
28 |
45011
|
panagiotis
|
public class UserManagerImpl implements UserManager {
|
29 |
35019
|
antonis.le
|
|
30 |
39076
|
antonis.le
|
@Autowired
|
31 |
45011
|
panagiotis
|
private UserDAO userDAO;
|
32 |
45206
|
panagiotis
|
|
33 |
45049
|
panagiotis
|
/*@Autowired
|
34 |
35343
|
antonis.le
|
private ProjectDAO projectDAO;
|
35 |
39076
|
antonis.le
|
@Autowired
|
36 |
|
|
private OrganizationDAO organizationDAO;
|
37 |
|
|
@Autowired
|
38 |
|
|
private PublisherManager publisherManager;
|
39 |
|
|
@Autowired
|
40 |
|
|
private OrganizationManager organizationManager;
|
41 |
45057
|
panagiotis
|
@Autowired
|
42 |
45049
|
panagiotis
|
private EmailUtils emailUtils;*/
|
43 |
45057
|
panagiotis
|
|
44 |
39076
|
antonis.le
|
@Autowired
|
45 |
|
|
ExecutorService executorService;
|
46 |
35343
|
antonis.le
|
|
47 |
35163
|
antonis.le
|
private int tokenTTL = 30;
|
48 |
35019
|
antonis.le
|
|
49 |
39076
|
antonis.le
|
public static int generateId(Affiliation affiliation) {
|
50 |
|
|
StringBuilder sb = new StringBuilder();
|
51 |
36089
|
antonis.le
|
|
52 |
45438
|
panagiotis
|
if (affiliation.getPk().getUser() != null)
|
53 |
|
|
sb.append(affiliation.getPk().getUser().getId());
|
54 |
|
|
if (affiliation.getPk().getOrganization() != null)
|
55 |
|
|
sb.append(affiliation.getPk().getOrganization().getId());
|
56 |
36089
|
antonis.le
|
|
57 |
39076
|
antonis.le
|
return DigestUtils.md5Hex(sb.toString()).hashCode();
|
58 |
|
|
}
|
59 |
36089
|
antonis.le
|
|
60 |
39076
|
antonis.le
|
@Override
|
61 |
45011
|
panagiotis
|
public User getById(String personId) throws PersonManagerException {
|
62 |
45028
|
panagiotis
|
return userDAO.getUserById(personId);
|
63 |
39076
|
antonis.le
|
}
|
64 |
35749
|
antonis.le
|
|
65 |
39076
|
antonis.le
|
@Override
|
66 |
45011
|
panagiotis
|
public User getByEmail(final String email) throws PersonManagerException {
|
67 |
45028
|
panagiotis
|
return userDAO.getUserByEmail(email);
|
68 |
35156
|
antonis.le
|
}
|
69 |
35041
|
antonis.le
|
|
70 |
39076
|
antonis.le
|
@Override
|
71 |
45438
|
panagiotis
|
// register or update
|
72 |
45045
|
panagiotis
|
public User saveUser(final User user) throws PersonManagerException {
|
73 |
45438
|
panagiotis
|
final List<UserRole> previousRoles;
|
74 |
35749
|
antonis.le
|
|
75 |
45011
|
panagiotis
|
if (user.getId() == null) {
|
76 |
|
|
user.setId(generateId(user));
|
77 |
|
|
user.setSource("portal");
|
78 |
36234
|
stefania.m
|
|
79 |
45438
|
panagiotis
|
previousRoles = new ArrayList<UserRole>();
|
80 |
39076
|
antonis.le
|
} else {
|
81 |
45438
|
panagiotis
|
previousRoles = user.getRoles();
|
82 |
|
|
}
|
83 |
45040
|
panagiotis
|
|
84 |
45045
|
panagiotis
|
userDAO.saveUser(user);
|
85 |
35749
|
antonis.le
|
|
86 |
45438
|
panagiotis
|
executorService.submit(new Runnable() {
|
87 |
|
|
@Override
|
88 |
|
|
public void run() {
|
89 |
|
|
/*try {
|
90 |
|
|
List<Role> roles = new ArrayList<>();
|
91 |
36234
|
stefania.m
|
|
92 |
45438
|
panagiotis
|
for (UserRole pRole : user.getRoles()) {
|
93 |
|
|
if (!previousRoles.contains(pRole) && !pRole.isApproved())
|
94 |
|
|
roles.add(pRole.getPk().getRole());
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
if (roles.size() > 0) {
|
98 |
|
|
List<User> moderators = getModerators();
|
99 |
|
|
|
100 |
|
|
emailUtils.sendUserRoleRequestedEmail(user, roles);
|
101 |
|
|
|
102 |
|
|
for (User moderator : moderators)
|
103 |
|
|
emailUtils.sendModeratorRoleRequestedEmail(moderator, user, roles);
|
104 |
|
|
}
|
105 |
|
|
} catch (MessagingException e) {
|
106 |
|
|
e.printStackTrace();
|
107 |
|
|
}*/
|
108 |
|
|
}
|
109 |
|
|
});
|
110 |
|
|
|
111 |
45028
|
panagiotis
|
return user;
|
112 |
39076
|
antonis.le
|
}
|
113 |
35749
|
antonis.le
|
|
114 |
35156
|
antonis.le
|
@Override
|
115 |
45011
|
panagiotis
|
public void register(final User user) throws PersonManagerException {
|
116 |
35343
|
antonis.le
|
final String token = UUID.randomUUID().toString();
|
117 |
45049
|
panagiotis
|
|
118 |
45082
|
panagiotis
|
try {
|
119 |
45083
|
panagiotis
|
User u = userDAO.getUserByEmail(user.getEmail());
|
120 |
|
|
if (u != null)
|
121 |
|
|
throw new PersonManagerException(PersonManagerException.ErrorCause.ALREADY_EXISTS);
|
122 |
39076
|
antonis.le
|
} catch (PersonManagerException e) {
|
123 |
|
|
throw e;
|
124 |
|
|
} catch (RuntimeException e) {
|
125 |
|
|
e.printStackTrace();
|
126 |
|
|
throw e;
|
127 |
45082
|
panagiotis
|
}
|
128 |
45074
|
panagiotis
|
|
129 |
45049
|
panagiotis
|
this.saveUser(user);
|
130 |
45083
|
panagiotis
|
userDAO.saveToken(user.getEmail(), token, tokenTTL);
|
131 |
36431
|
antonis.le
|
|
132 |
39076
|
antonis.le
|
executorService.submit(new Runnable() {
|
133 |
|
|
@Override
|
134 |
|
|
public void run() {
|
135 |
45206
|
panagiotis
|
/*try {
|
136 |
|
|
emailUtils.sendActivationEmail(user, token);
|
137 |
|
|
} catch (MessagingException e) {
|
138 |
|
|
e.printStackTrace();
|
139 |
|
|
}*/
|
140 |
39076
|
antonis.le
|
}
|
141 |
|
|
});
|
142 |
35019
|
antonis.le
|
}
|
143 |
|
|
|
144 |
35156
|
antonis.le
|
@Override
|
145 |
45011
|
panagiotis
|
public User login(String email, String password) throws PersonManagerException {
|
146 |
35160
|
antonis.le
|
|
147 |
45011
|
panagiotis
|
if (userDAO.verifyLogin(email, password)) {
|
148 |
|
|
User user = getByEmail(email);
|
149 |
|
|
if (user.isActive())
|
150 |
|
|
return user;
|
151 |
35160
|
antonis.le
|
else
|
152 |
|
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_ACTIVATED);
|
153 |
|
|
} else {
|
154 |
|
|
throw new PersonManagerException(PersonManagerException.ErrorCause.WRONG_EMAIL_OR_PASSWORD);
|
155 |
|
|
}
|
156 |
35041
|
antonis.le
|
}
|
157 |
|
|
|
158 |
35019
|
antonis.le
|
@Override
|
159 |
45011
|
panagiotis
|
public User activate(final String email, final String token) throws PersonManagerException {
|
160 |
35163
|
antonis.le
|
|
161 |
45011
|
panagiotis
|
if (!userDAO.verifyToken(email, token))
|
162 |
35163
|
antonis.le
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
163 |
39076
|
antonis.le
|
else {
|
164 |
45040
|
panagiotis
|
User user = userDAO.getUserByEmail(email);
|
165 |
|
|
userDAO.activateUser(user.getEmail());
|
166 |
|
|
userDAO.deleteToken(user.getEmail(), token);
|
167 |
39076
|
antonis.le
|
}
|
168 |
35163
|
antonis.le
|
return getByEmail(email);
|
169 |
35019
|
antonis.le
|
}
|
170 |
|
|
|
171 |
|
|
@Override
|
172 |
35163
|
antonis.le
|
public void resetPassword(final String email) throws PersonManagerException {
|
173 |
|
|
SecureRandom random = new SecureRandom();
|
174 |
|
|
final String newPassword = new BigInteger(50, random).toString(32);
|
175 |
35156
|
antonis.le
|
|
176 |
45011
|
panagiotis
|
if (!userDAO.updatePassword(newPassword, email)) {
|
177 |
35163
|
antonis.le
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
178 |
|
|
}
|
179 |
45206
|
panagiotis
|
executorService.submit(new Runnable() {
|
180 |
39076
|
antonis.le
|
@Override
|
181 |
|
|
public void run() {
|
182 |
45028
|
panagiotis
|
User user = userDAO.getUserByEmail(email);
|
183 |
35163
|
antonis.le
|
|
184 |
45206
|
panagiotis
|
/*try {
|
185 |
45011
|
panagiotis
|
emailUtils.sendResetPasswordEmail(user, newPassword);
|
186 |
39076
|
antonis.le
|
} catch (MessagingException e) {
|
187 |
|
|
e.printStackTrace();
|
188 |
45206
|
panagiotis
|
}*/
|
189 |
39076
|
antonis.le
|
}
|
190 |
45206
|
panagiotis
|
});
|
191 |
35019
|
antonis.le
|
}
|
192 |
|
|
|
193 |
|
|
@Override
|
194 |
35156
|
antonis.le
|
public List<Role> getRoles() {
|
195 |
45011
|
panagiotis
|
return userDAO.getRoles();
|
196 |
35019
|
antonis.le
|
}
|
197 |
|
|
|
198 |
39076
|
antonis.le
|
@Override
|
199 |
45011
|
panagiotis
|
public List<User> getModerators() {
|
200 |
|
|
return userDAO.getModerators();
|
201 |
39076
|
antonis.le
|
}
|
202 |
35686
|
antonis.le
|
|
203 |
39076
|
antonis.le
|
@Override
|
204 |
45011
|
panagiotis
|
public List<User> getAccountingOfficers() {
|
205 |
|
|
return userDAO.getAccountingOfficers();
|
206 |
39076
|
antonis.le
|
}
|
207 |
36834
|
stefania.m
|
|
208 |
39076
|
antonis.le
|
@Override
|
209 |
45085
|
panagiotis
|
public void activateUser(String email) {
|
210 |
|
|
userDAO.activateUser(email);
|
211 |
39076
|
antonis.le
|
}
|
212 |
36025
|
antonis.le
|
|
213 |
39076
|
antonis.le
|
@Override
|
214 |
|
|
public void activateUsers(List<String> userIds) {
|
215 |
|
|
for (String userId : userIds) {
|
216 |
45011
|
panagiotis
|
userDAO.activateUser(userId);
|
217 |
39076
|
antonis.le
|
}
|
218 |
|
|
}
|
219 |
36052
|
antonis.le
|
|
220 |
39076
|
antonis.le
|
@Override
|
221 |
45043
|
panagiotis
|
public void deactivateUser(String email) {
|
222 |
|
|
userDAO.deactivateUser(email);
|
223 |
39076
|
antonis.le
|
}
|
224 |
36025
|
antonis.le
|
|
225 |
39076
|
antonis.le
|
@Override
|
226 |
45043
|
panagiotis
|
public void deactivateUsers(List<String> emails) {
|
227 |
|
|
for (String email : emails) {
|
228 |
|
|
userDAO.deactivateUser(email);
|
229 |
39076
|
antonis.le
|
}
|
230 |
|
|
}
|
231 |
36052
|
antonis.le
|
|
232 |
39076
|
antonis.le
|
@Override
|
233 |
45043
|
panagiotis
|
public void acceptUserRole(final String email, final String roleId) {
|
234 |
|
|
userDAO.acceptUserRole(email, roleId);
|
235 |
36233
|
antonis.le
|
|
236 |
45206
|
panagiotis
|
executorService.submit(new Runnable() {
|
237 |
39076
|
antonis.le
|
@Override
|
238 |
|
|
public void run() {
|
239 |
45206
|
panagiotis
|
/*User user = userDAO.getUserByEmail(email);
|
240 |
45011
|
panagiotis
|
Role role = userDAO.getRole(roleId);
|
241 |
|
|
List<User> moderators = getModerators();
|
242 |
36233
|
antonis.le
|
|
243 |
39076
|
antonis.le
|
try {
|
244 |
45011
|
panagiotis
|
emailUtils.sendUserRoleAcceptedEmail(user, role);
|
245 |
36233
|
antonis.le
|
|
246 |
45011
|
panagiotis
|
for (User moderator : moderators)
|
247 |
|
|
emailUtils.sendModeratorRoleAcceptedEmail(moderator, user, role);
|
248 |
39076
|
antonis.le
|
} catch (MessagingException e) {
|
249 |
|
|
e.printStackTrace();
|
250 |
45206
|
panagiotis
|
}*/
|
251 |
39076
|
antonis.le
|
}
|
252 |
45206
|
panagiotis
|
});
|
253 |
39076
|
antonis.le
|
}
|
254 |
36025
|
antonis.le
|
|
255 |
39076
|
antonis.le
|
@Override
|
256 |
45043
|
panagiotis
|
public void rejectRole(final String email, final String roleId) {
|
257 |
|
|
userDAO.rejectUserRole(email, roleId);
|
258 |
36233
|
antonis.le
|
|
259 |
45206
|
panagiotis
|
executorService.submit(new Runnable() {
|
260 |
39076
|
antonis.le
|
@Override
|
261 |
|
|
public void run() {
|
262 |
45206
|
panagiotis
|
/*User user = userDAO.getUserByEmail(email);
|
263 |
45011
|
panagiotis
|
Role role = userDAO.getRole(roleId);
|
264 |
|
|
List<User> moderators = getModerators();
|
265 |
36233
|
antonis.le
|
|
266 |
39076
|
antonis.le
|
try {
|
267 |
45011
|
panagiotis
|
emailUtils.sendUserRoleRejectedEmail(user, role);
|
268 |
36233
|
antonis.le
|
|
269 |
45011
|
panagiotis
|
for (User moderator : moderators)
|
270 |
|
|
emailUtils.sendModeratorRoleRejectedEmail(moderator, user, role);
|
271 |
39076
|
antonis.le
|
} catch (MessagingException e) {
|
272 |
|
|
e.printStackTrace();
|
273 |
45206
|
panagiotis
|
}*/
|
274 |
39076
|
antonis.le
|
}
|
275 |
45206
|
panagiotis
|
});
|
276 |
39076
|
antonis.le
|
}
|
277 |
36025
|
antonis.le
|
|
278 |
39076
|
antonis.le
|
@Override
|
279 |
45011
|
panagiotis
|
public List<User> getUsers() {
|
280 |
45206
|
panagiotis
|
return userDAO.getUsers();
|
281 |
39076
|
antonis.le
|
}
|
282 |
36089
|
antonis.le
|
|
283 |
39076
|
antonis.le
|
@Override
|
284 |
45011
|
panagiotis
|
public String generateId(User user) {
|
285 |
39076
|
antonis.le
|
StringBuilder sb = new StringBuilder();
|
286 |
36089
|
antonis.le
|
|
287 |
45011
|
panagiotis
|
sb.append(user.getFirstname()).append(user.getLastname()).append(user.getEmail());
|
288 |
39076
|
antonis.le
|
return "portal::" + DigestUtils.md5Hex(sb.toString());
|
289 |
|
|
}
|
290 |
|
|
|
291 |
35160
|
antonis.le
|
public int getTokenTTL() {
|
292 |
|
|
return tokenTTL;
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
public void setTokenTTL(int tokenTTL) {
|
296 |
|
|
this.tokenTTL = tokenTTL;
|
297 |
|
|
}
|
298 |
35019
|
antonis.le
|
}
|