1
|
package eu.dnetlib.goldoa.service;
|
2
|
|
3
|
import eu.dnetlib.goldoa.domain.Affiliation;
|
4
|
import eu.dnetlib.goldoa.domain.Person;
|
5
|
import eu.dnetlib.goldoa.domain.PersonManagerException;
|
6
|
import eu.dnetlib.goldoa.domain.Project;
|
7
|
import eu.dnetlib.goldoa.domain.Role;
|
8
|
import eu.dnetlib.goldoa.service.dao.OrganizationDAO;
|
9
|
import eu.dnetlib.goldoa.service.dao.PersonDAO;
|
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.transaction.annotation.Transactional;
|
16
|
|
17
|
import javax.mail.MessagingException;
|
18
|
import java.math.BigInteger;
|
19
|
import java.security.SecureRandom;
|
20
|
import java.util.ArrayList;
|
21
|
import java.util.List;
|
22
|
import java.util.UUID;
|
23
|
|
24
|
/**
|
25
|
* Created by antleb on 3/4/15.
|
26
|
*/
|
27
|
@Transactional
|
28
|
public class PersonManagerImpl implements PersonManager {
|
29
|
|
30
|
@Autowired
|
31
|
private PersonDAO personDAO;
|
32
|
@Autowired
|
33
|
private ProjectDAO projectDAO;
|
34
|
@Autowired
|
35
|
private OrganizationDAO organizationDAO;
|
36
|
@Autowired
|
37
|
private EmailUtils emailUtils;
|
38
|
|
39
|
private int tokenTTL = 30;
|
40
|
|
41
|
public static int generateId(Affiliation affiliation) {
|
42
|
StringBuilder sb = new StringBuilder();
|
43
|
|
44
|
if (affiliation.getPerson()!= null)
|
45
|
sb.append(affiliation.getPerson().getId());
|
46
|
if (affiliation.getOrganization() != null)
|
47
|
sb.append(affiliation.getOrganization().getId());
|
48
|
|
49
|
return DigestUtils.md5Hex(sb.toString()).hashCode();
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public Person getById(String personId) throws PersonManagerException {
|
54
|
try {
|
55
|
Person person = personDAO.getPersonById(personId);
|
56
|
|
57
|
loadPersonRelations(person);
|
58
|
|
59
|
return person;
|
60
|
} catch (EmptyResultDataAccessException e) {
|
61
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
62
|
}
|
63
|
}
|
64
|
|
65
|
@Override
|
66
|
public Person getByEmail(final String email) throws PersonManagerException {
|
67
|
try {
|
68
|
Person person = personDAO.getPersonByEmail(email);
|
69
|
|
70
|
loadPersonRelations(person);
|
71
|
|
72
|
return person;
|
73
|
} catch (EmptyResultDataAccessException e) {
|
74
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
@Override
|
79
|
public Person savePerson(final Person person) throws PersonManagerException {
|
80
|
|
81
|
if (person.getId() == null) {
|
82
|
person.setId(generateId(person));
|
83
|
person.setSource("portal");
|
84
|
}
|
85
|
|
86
|
personDAO.savePerson(person);
|
87
|
deletePersonRelations(person);
|
88
|
savePersonRelations(person);
|
89
|
|
90
|
loadPersonRelations(person);
|
91
|
|
92
|
return person;
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
public void register(final Person person) throws PersonManagerException {
|
97
|
final String token = UUID.randomUUID().toString();
|
98
|
|
99
|
this.savePerson(person);
|
100
|
personDAO.saveToken(person.getId(), token, tokenTTL);
|
101
|
|
102
|
try {
|
103
|
emailUtils.sendActivationEmail(person.getName(), person.getEmail(), token);
|
104
|
} catch (MessagingException e) {
|
105
|
throw new PersonManagerException(PersonManagerException.ErrorCause.EMAIL_ERROR);
|
106
|
}
|
107
|
}
|
108
|
|
109
|
@Override
|
110
|
public Person login(String email, String password) throws PersonManagerException {
|
111
|
|
112
|
if (personDAO.verifyLogin(email, password)) {
|
113
|
Person person = getByEmail(email);
|
114
|
|
115
|
if (person.isActive())
|
116
|
return person;
|
117
|
else
|
118
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_ACTIVATED);
|
119
|
} else {
|
120
|
throw new PersonManagerException(PersonManagerException.ErrorCause.WRONG_EMAIL_OR_PASSWORD);
|
121
|
}
|
122
|
}
|
123
|
|
124
|
@Override
|
125
|
public Person activate(final String email, final String token) throws PersonManagerException {
|
126
|
|
127
|
if (!personDAO.verifyToken(email, token))
|
128
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
129
|
else {
|
130
|
Person person = personDAO.getPersonByEmail(email);
|
131
|
|
132
|
personDAO.activateUser(person.getId());
|
133
|
personDAO.deleteToken(person.getId(), token);
|
134
|
}
|
135
|
|
136
|
return getByEmail(email);
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public void resetPassword(final String email) throws PersonManagerException {
|
141
|
SecureRandom random = new SecureRandom();
|
142
|
final String newPassword = new BigInteger(50, random).toString(32);
|
143
|
|
144
|
if (!personDAO.updatePassword(newPassword, email)) {
|
145
|
throw new PersonManagerException(PersonManagerException.ErrorCause.NOT_EXISTS);
|
146
|
}
|
147
|
|
148
|
Person person = personDAO.getPersonByEmail(email);
|
149
|
|
150
|
try {
|
151
|
emailUtils.sendResetPasswordEmail(person.getName(), email, newPassword);
|
152
|
} catch (MessagingException e) {
|
153
|
throw new PersonManagerException(PersonManagerException.ErrorCause.EMAIL_ERROR);
|
154
|
}
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public List<Role> getRoles() {
|
159
|
return personDAO.getRoles();
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
public List<Person> getModerators() {
|
164
|
return personDAO.getModerators();
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public void activateUser(String userId) {
|
169
|
personDAO.activateUser(userId);
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
public void activateUsers(List<String> userIds) {
|
174
|
for (String userId:userIds) {
|
175
|
personDAO.activateUser(userId);
|
176
|
}
|
177
|
}
|
178
|
|
179
|
@Override
|
180
|
public void deactivateUser(String userId) {
|
181
|
personDAO.deactivateUser(userId);
|
182
|
}
|
183
|
|
184
|
@Override
|
185
|
public void deactivateUsers(List<String> userIds) {
|
186
|
for (String userId:userIds) {
|
187
|
personDAO.deactivateUser(userId);
|
188
|
}
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public void acceptUserRole(String userId, String roleId) {
|
193
|
personDAO.acceptUserRole(userId, roleId);
|
194
|
}
|
195
|
|
196
|
@Override
|
197
|
public void rejectRole(String userId, String roleId) {
|
198
|
personDAO.rejectUserRole(userId, roleId);
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public List<Person> getUsers() {
|
203
|
List<Person> persons = personDAO.getUsers();
|
204
|
|
205
|
for (Person person:persons) {
|
206
|
loadPersonRelations(person);
|
207
|
}
|
208
|
|
209
|
return persons;
|
210
|
}
|
211
|
|
212
|
@Override
|
213
|
public String generateId(Person person) {
|
214
|
StringBuilder sb = new StringBuilder();
|
215
|
|
216
|
sb.append(person.getName()).append(person.getLastname()).append(person.getEmail());
|
217
|
return "portal::" + DigestUtils.md5Hex(sb.toString());
|
218
|
}
|
219
|
|
220
|
private void deletePersonRelations(final Person person) {
|
221
|
personDAO.deleteAffiliations(person.getId());
|
222
|
personDAO.deletePersonRoles(person.getId());
|
223
|
personDAO.removeProjectCoordinators(person.getId());
|
224
|
}
|
225
|
|
226
|
private void savePersonRelations(final Person person) {
|
227
|
List<String> projectIds = new ArrayList<String>();
|
228
|
|
229
|
if (person.getCoordinatedProjects() != null)
|
230
|
for (Project project:person.getCoordinatedProjects())
|
231
|
projectIds.add(project.getId());
|
232
|
|
233
|
personDAO.saveProjectCoordinators(person.getId(), projectIds);
|
234
|
personDAO.savePersonRoles(person.getId(), person.getRoles());
|
235
|
personDAO.saveAffiliations(person, person.getAffiliations());
|
236
|
}
|
237
|
|
238
|
private void loadPersonRelations(Person person) {
|
239
|
person.setCoordinatedProjects(projectDAO.getProjectsCoordinatedBy(person.getId()));
|
240
|
|
241
|
if (person.getAffiliations() != null) {
|
242
|
for (Affiliation affiliation : person.getAffiliations()) {
|
243
|
affiliation.setOrganization(organizationDAO.getOrganization(affiliation.getOrganization().getId()));
|
244
|
}
|
245
|
}
|
246
|
}
|
247
|
|
248
|
public int getTokenTTL() {
|
249
|
return tokenTTL;
|
250
|
}
|
251
|
|
252
|
public void setTokenTTL(int tokenTTL) {
|
253
|
this.tokenTTL = tokenTTL;
|
254
|
}
|
255
|
|
256
|
public EmailUtils getEmailUtils() {
|
257
|
return emailUtils;
|
258
|
}
|
259
|
|
260
|
public void setEmailUtils(EmailUtils emailUtils) {
|
261
|
this.emailUtils = emailUtils;
|
262
|
}
|
263
|
|
264
|
public PersonDAO getPersonDAO() {
|
265
|
return personDAO;
|
266
|
}
|
267
|
|
268
|
public void setPersonDAO(PersonDAO personDAO) {
|
269
|
this.personDAO = personDAO;
|
270
|
}
|
271
|
|
272
|
public ProjectDAO getProjectDAO() {
|
273
|
return projectDAO;
|
274
|
}
|
275
|
|
276
|
public void setProjectDAO(ProjectDAO projectDAO) {
|
277
|
this.projectDAO = projectDAO;
|
278
|
}
|
279
|
|
280
|
public OrganizationDAO getOrganizationDAO() {
|
281
|
return organizationDAO;
|
282
|
}
|
283
|
|
284
|
public void setOrganizationDAO(OrganizationDAO organizationDAO) {
|
285
|
this.organizationDAO = organizationDAO;
|
286
|
}
|
287
|
}
|