1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
package org.directdemocracyportal.democracy.service; |
6 | |
|
7 | |
import java.util.Date; |
8 | |
import java.util.List; |
9 | |
|
10 | |
import org.directdemocracyportal.democracy.model.application.Action; |
11 | |
import org.directdemocracyportal.democracy.model.application.Authority; |
12 | |
import org.directdemocracyportal.democracy.model.application.Event; |
13 | |
import org.directdemocracyportal.democracy.model.application.Portal; |
14 | |
import org.directdemocracyportal.democracy.model.application.SecurityRoleType; |
15 | |
import org.directdemocracyportal.democracy.model.application.User; |
16 | |
import org.directdemocracyportal.democracy.model.application.UserSession; |
17 | |
import org.directdemocracyportal.democracy.model.world.Person; |
18 | |
import org.directdemocracyportal.democracy.model.world.Region; |
19 | |
import org.directdemocracyportal.democracy.model.world.RegionType; |
20 | |
import org.directdemocracyportal.democracy.service.command.RegisterCommand; |
21 | |
import org.directdemocracyportal.democracy.service.dao.PersonDAO; |
22 | |
import org.directdemocracyportal.democracy.service.dao.PortalDAO; |
23 | |
import org.directdemocracyportal.democracy.service.dao.UserDAO; |
24 | |
import org.directdemocracyportal.democracy.service.dao.UserSessionDAO; |
25 | |
import org.springframework.mail.MailException; |
26 | |
import org.springframework.mail.SimpleMailMessage; |
27 | |
import org.springframework.mail.javamail.JavaMailSenderImpl; |
28 | |
import org.springframework.security.authentication.encoding.Md5PasswordEncoder; |
29 | |
import org.springframework.security.authentication.encoding.PasswordEncoder; |
30 | |
import org.springframework.transaction.annotation.Propagation; |
31 | |
import org.springframework.transaction.annotation.Transactional; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
@Transactional(propagation = Propagation.REQUIRED) |
37 | |
public class UserServiceImpl implements UserService |
38 | |
{ |
39 | |
|
40 | |
|
41 | |
final private JavaMailSenderImpl javaMailSenderImpl; |
42 | |
|
43 | |
|
44 | |
final private SimpleMailMessage simpleMailMessage; |
45 | |
|
46 | |
|
47 | |
final private UserDAO userDAO; |
48 | |
|
49 | |
|
50 | |
final private PortalDAO portalDAO; |
51 | |
|
52 | |
|
53 | |
final private PersonDAO personDAO; |
54 | |
|
55 | |
|
56 | |
final private UserSessionDAO userSessionDAO; |
57 | |
|
58 | |
|
59 | |
private final PasswordEncoder passwordEncoder; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public UserServiceImpl(UserDAO userDAO, PortalDAO portalDAO, |
72 | |
PersonDAO personDAO, UserSessionDAO userSessionDAO, |
73 | |
JavaMailSenderImpl javaMailSenderImpl, |
74 | 4 | SimpleMailMessage simpleMailMessage) { |
75 | 4 | this.userDAO = userDAO; |
76 | 4 | this.portalDAO = portalDAO; |
77 | 4 | this.personDAO = personDAO; |
78 | 4 | this.userSessionDAO = userSessionDAO; |
79 | 4 | this.javaMailSenderImpl = javaMailSenderImpl; |
80 | 4 | this.simpleMailMessage = simpleMailMessage; |
81 | 4 | this.passwordEncoder = new Md5PasswordEncoder(); |
82 | 4 | } |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private void addMember(Portal portal, Person person) { |
91 | 0 | portal.addMember(person); |
92 | 0 | personDAO.save(person); |
93 | 0 | portalDAO.save(portal); |
94 | 0 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public User register(RegisterCommand command, String sessionId) |
103 | |
throws UserAlreadyExistException { |
104 | 1 | if (this.userDAO.findByUsername(command.getUsername()) == null) { |
105 | |
|
106 | 0 | Person person = new Person(command.getName()); |
107 | 0 | this.personDAO.save(person); |
108 | |
|
109 | 0 | Portal localPortal = this.portalDAO.load(command.getPortalId()); |
110 | 0 | if (localPortal != null) { |
111 | 0 | addMember(localPortal, person); |
112 | |
} |
113 | |
|
114 | 0 | Region region = localPortal.getRegion().getParent(); |
115 | 0 | if (region.getRegionType().equals(RegionType.Regional)) { |
116 | 0 | Portal regionalPortal = portalDAO.findRegionalByRegion(region); |
117 | 0 | if (regionalPortal != null) { |
118 | 0 | addMember(regionalPortal, person); |
119 | |
} |
120 | |
} |
121 | |
|
122 | 0 | Portal nationalPortal = portalDAO |
123 | |
.findNationalByCountry(localPortal.getCountry()); |
124 | 0 | if (nationalPortal != null) { |
125 | 0 | addMember(nationalPortal, person); |
126 | |
} |
127 | |
|
128 | 0 | Portal globalPortal = portalDAO |
129 | |
.findByName("Direct Democracy Portal"); |
130 | 0 | if (globalPortal != null) { |
131 | 0 | addMember(globalPortal, person); |
132 | |
} |
133 | |
|
134 | 0 | String encodedPassword = passwordEncoder.encodePassword(command |
135 | |
.getPassword(), null); |
136 | |
|
137 | 0 | User newUser = new User(new Date(), command.getUsername(), |
138 | |
encodedPassword,command.getEmail(), person); |
139 | 0 | newUser.setAccountNonExpired(true); |
140 | 0 | newUser.setCredentialsNonExpired(true); |
141 | 0 | newUser.setEnabled(true); |
142 | 0 | newUser.setAccountNonLocked(true); |
143 | 0 | newUser.getEvents().add( |
144 | |
new Event(newUser, Action.Register, new Date())); |
145 | |
|
146 | 0 | Authority authority = new Authority(SecurityRoleType.ROLE_USER); |
147 | 0 | newUser.getGrantedAuthorities().add(authority); |
148 | |
|
149 | 0 | UserSession userSession = new UserSession(newUser, sessionId, |
150 | |
new Date()); |
151 | 0 | userSession.setActive(true); |
152 | 0 | this.userSessionDAO.save(userSession); |
153 | |
|
154 | 0 | return newUser; |
155 | |
} else { |
156 | 1 | throw new UserAlreadyExistException(); |
157 | |
} |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
public void sendEmailConfirmationRequest(RegisterCommand command) { |
166 | 0 | SimpleMailMessage msg = new SimpleMailMessage(this.simpleMailMessage); |
167 | 0 | msg.setTo(command.getEmail()); |
168 | 0 | msg.setText("Hello " + command.getName() |
169 | |
+ ", thank you for registering. confirm with link below "); |
170 | |
try { |
171 | 0 | this.javaMailSenderImpl.send(msg); |
172 | 0 | } catch (MailException ex) { |
173 | |
|
174 | 0 | System.err.println(ex.getMessage()); |
175 | 0 | } |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
public List<User> getUsers() { |
184 | 1 | return this.userDAO.getAll(); |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public User login(String username, String password, String sessionId) { |
194 | 0 | String encodedPassword = passwordEncoder.encodePassword(password, null); |
195 | 0 | User user = userDAO.findByUsername(username); |
196 | |
|
197 | 0 | if (user.getPassword().equals(encodedPassword)) { |
198 | 0 | if (this.userSessionDAO.findByUserId(user.getId()) == null) { |
199 | 0 | user.getEvents().add(new Event(user, Action.Login, new Date())); |
200 | 0 | this.userSessionDAO.save(new UserSession(user, sessionId, |
201 | |
new Date())); |
202 | 0 | userDAO.save(user); |
203 | |
} |
204 | |
} |
205 | 0 | return user; |
206 | |
} |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void logout(User user) { |
214 | 1 | UserSession userSession = this.userSessionDAO |
215 | |
.findByUserId(user.getId()); |
216 | 1 | if (userSession != null) { |
217 | 1 | userSession.getUser().getEvents() |
218 | |
.add( |
219 | |
new Event(userSession.getUser(), Action.Logout, |
220 | |
new Date())); |
221 | 1 | userSession.setEndedAt(new Date()); |
222 | 1 | userSession.setActive(false); |
223 | 1 | this.userSessionDAO.save(userSession); |
224 | 1 | this.userDAO.save(userSession.getUser()); |
225 | |
} |
226 | 1 | } |
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
public List<Portal> getPortals() { |
234 | 0 | return this.portalDAO.getAll(); |
235 | |
} |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public void endActiveUserSessions() { |
243 | 0 | this.userSessionDAO.endActiveSessions(); |
244 | 0 | } |
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public User getUser(Long userId) { |
252 | 0 | return this.userDAO.load(userId); |
253 | |
} |
254 | |
} |