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