Project

General

Profile

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