1 package net.ramapuram.thomas.service.impl;
2
3 import net.ramapuram.thomas.dao.UserDao;
4 import net.ramapuram.thomas.model.User;
5 import net.ramapuram.thomas.service.UserExistsException;
6 import net.ramapuram.thomas.service.UserManager;
7 import net.ramapuram.thomas.service.UserService;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.dao.DataIntegrityViolationException;
10 import org.springframework.orm.jpa.JpaSystemException;
11 import org.springframework.security.authentication.encoding.PasswordEncoder;
12 import org.springframework.security.core.userdetails.UsernameNotFoundException;
13 import org.springframework.stereotype.Service;
14
15 import javax.jws.WebService;
16 import java.util.List;
17
18
19
20
21
22
23
24 @Service("userManager")
25 @WebService(serviceName = "UserService", endpointInterface = "net.ramapuram.thomas.service.UserService")
26 public class UserManagerImpl extends GenericManagerImpl<User, Long> implements UserManager, UserService {
27 private PasswordEncoder passwordEncoder;
28 private UserDao userDao;
29
30 @Autowired
31 public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
32 this.passwordEncoder = passwordEncoder;
33 }
34
35 @Autowired
36 public void setUserDao(UserDao userDao) {
37 this.dao = userDao;
38 this.userDao = userDao;
39 }
40
41
42
43
44 public User getUser(String userId) {
45 return userDao.get(new Long(userId));
46 }
47
48
49
50
51 public List<User> getUsers() {
52 return userDao.getAllDistinct();
53 }
54
55
56
57
58 public User saveUser(User user) throws UserExistsException {
59
60 if (user.getVersion() == null) {
61
62 user.setUsername(user.getUsername().toLowerCase());
63 }
64
65
66 boolean passwordChanged = false;
67 if (passwordEncoder != null) {
68
69 if (user.getVersion() == null) {
70
71 passwordChanged = true;
72 } else {
73
74 String currentPassword = userDao.getUserPassword(user.getUsername());
75 if (currentPassword == null) {
76 passwordChanged = true;
77 } else {
78 if (!currentPassword.equals(user.getPassword())) {
79 passwordChanged = true;
80 }
81 }
82 }
83
84
85 if (passwordChanged) {
86 user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null));
87 }
88 } else {
89 log.warn("PasswordEncoder not set, skipping password encryption...");
90 }
91
92 try {
93 return userDao.saveUser(user);
94 } catch (DataIntegrityViolationException e) {
95
96 log.warn(e.getMessage());
97 throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
98 } catch (JpaSystemException e) {
99
100 log.warn(e.getMessage());
101 throw new UserExistsException("User '" + user.getUsername() + "' already exists!");
102 }
103 }
104
105
106
107
108 public void removeUser(String userId) {
109 log.debug("removing user: " + userId);
110 userDao.remove(new Long(userId));
111 }
112
113
114
115
116
117
118
119
120 public User getUserByUsername(String username) throws UsernameNotFoundException {
121 return (User) userDao.loadUserByUsername(username);
122 }
123
124
125
126
127 public List<User> search(String searchTerm) {
128 return super.search(searchTerm, User.class);
129 }
130 }