diff --git a/chatto/src/main/java/org/ros/chatto/BeanConfigurations.java b/chatto/src/main/java/org/ros/chatto/BeanConfigurations.java index bdbabf5..fab183d 100644 --- a/chatto/src/main/java/org/ros/chatto/BeanConfigurations.java +++ b/chatto/src/main/java/org/ros/chatto/BeanConfigurations.java @@ -1,7 +1,6 @@ package org.ros.chatto; import org.modelmapper.ModelMapper; -import org.ros.chatto.logged.ActiveUserStore; import org.ros.chatto.security.AuthenticationSuccessHandlerImpl; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.MessageSource; @@ -45,11 +44,6 @@ public class BeanConfigurations { return messageSource; } - @Bean - ActiveUserStore activeUserStore() { - return new ActiveUserStore(); - } - // @Bean // public Connection connection() throws SQLException // { diff --git a/chatto/src/main/java/org/ros/chatto/controller/DemoRestController.java b/chatto/src/main/java/org/ros/chatto/controller/DemoRestController.java index b0eeeed..b34b752 100644 --- a/chatto/src/main/java/org/ros/chatto/controller/DemoRestController.java +++ b/chatto/src/main/java/org/ros/chatto/controller/DemoRestController.java @@ -7,7 +7,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.ros.chatto.dto.ActiveUserDTO; -import org.ros.chatto.logged.ActiveUserStore; import org.ros.chatto.model.ChatMessage; import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.MessageCipher; @@ -51,8 +50,6 @@ public class DemoRestController { @Autowired ChatMessageRepository chatMessageRepository; @Autowired - ActiveUserStore activeUserStore; - @Autowired private UserService userService; @GetMapping("/users") public List getAllUsers() { @@ -128,11 +125,6 @@ public class DemoRestController { return "redirect:/users"; } - @GetMapping(value = "/loggedUsers") - public ActiveUserStore getActiveUsers() { - return activeUserStore; - } - @GetMapping(value = "/loggedUsers2") public List getOtherActiveUsers(Principal principal) { diff --git a/chatto/src/main/java/org/ros/chatto/logged/ActiveUser.java b/chatto/src/main/java/org/ros/chatto/logged/ActiveUser.java deleted file mode 100644 index 588b103..0000000 --- a/chatto/src/main/java/org/ros/chatto/logged/ActiveUser.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.ros.chatto.logged; - -import java.time.Instant; - -import lombok.Data; - -@Data -public class ActiveUser { - private String userName; -// private Instant lastActive; - private int numSessions = 0; -} diff --git a/chatto/src/main/java/org/ros/chatto/logged/ActiveUserStore.java b/chatto/src/main/java/org/ros/chatto/logged/ActiveUserStore.java deleted file mode 100644 index b0ac80c..0000000 --- a/chatto/src/main/java/org/ros/chatto/logged/ActiveUserStore.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.ros.chatto.logged; - -import java.util.ArrayList; -import java.util.List; - -import lombok.Getter; -import lombok.Setter; - -@Getter @Setter -public class ActiveUserStore { - - public List users; - - public List activeUsers; - - public ActiveUserStore() { - users = new ArrayList(); - activeUsers = new ArrayList<>(); - } -} \ No newline at end of file diff --git a/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java b/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java index 163a212..dfef103 100644 --- a/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java +++ b/chatto/src/main/java/org/ros/chatto/logged/LoggedUser.java @@ -26,14 +26,11 @@ import lombok.Setter; public class LoggedUser implements HttpSessionBindingListener { private String username; - private ActiveUserStore activeUserStore; - public LoggedUser(String username, ActiveUserStore activeUserStore) { + public LoggedUser(String username) + { this.username = username; - this.activeUserStore = activeUserStore; - } - public LoggedUser() { } @@ -41,27 +38,9 @@ public class LoggedUser implements HttpSessionBindingListener { public void valueBound(HttpSessionBindingEvent event) { UserService userService = getUserService(event); UserSessionRepository userSessionRepository = getUserSessionRepository(event); - List users = activeUserStore.getUsers(); - List activeUsers = activeUserStore.getActiveUsers(); LoggedUser user = (LoggedUser) event.getValue(); - if (!users.contains(user.getUsername())) { - users.add(user.getUsername()); - } Instant instant = Instant.now(); -// Optional activeUserOptional = activeUsers.stream() -// .filter(au -> au.getUserName().equals(user.getUsername())) -// .findFirst(); -// ActiveUser activeUser2 = activeUserOptional.get(); -// activeUser2.setNumSessions(activeUser2.getNumSessions() + 1); -// boolean found = activeUsers.stream().anyMatch(au -> au.getUserName().equals(user.getUsername())); -// if (!found) { -// System.out.println("Test found "); -// ActiveUser activeUser = new ActiveUser(); -// activeUser.setUserName(user.getUsername()); -//// activeUser.setLastActive(instant); -// activeUsers.add(activeUser); -// } ChatUser chatUser = userService.findByUserName(user.getUsername()); UserSession userSession = userSessionRepository.findByUserName(user.getUsername()); @@ -79,27 +58,13 @@ public class LoggedUser implements HttpSessionBindingListener { @Override public void valueUnbound(HttpSessionBindingEvent event) { - List users = activeUserStore.getUsers(); LoggedUser user = (LoggedUser) event.getValue(); - List activeUsers = activeUserStore.getActiveUsers(); UserService userService = getUserService(event); UserSessionRepository userSessionRepository = getUserSessionRepository(event); Instant instant = Instant.now(); - if (users.contains(user.getUsername())) { - users.remove(user.getUsername()); - } - activeUsers.removeIf(au -> { - int numSessions = au.getNumSessions(); - if (au.getUserName().equals(user.getUsername()) && --numSessions == 0) { - au.setNumSessions(numSessions); - return true; - } - - return false; - }); ChatUser chatUser = userService.findByUserName(user.getUsername()); diff --git a/chatto/src/main/java/org/ros/chatto/logged/MySimpleUrlAuthenticationSuccessHandler.java b/chatto/src/main/java/org/ros/chatto/logged/MySimpleUrlAuthenticationSuccessHandler.java index 1f8090a..388953b 100644 --- a/chatto/src/main/java/org/ros/chatto/logged/MySimpleUrlAuthenticationSuccessHandler.java +++ b/chatto/src/main/java/org/ros/chatto/logged/MySimpleUrlAuthenticationSuccessHandler.java @@ -14,16 +14,13 @@ import org.springframework.stereotype.Component; @Component("myAuthenticationSuccessHandler") public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { - @Autowired - ActiveUserStore activeUserStore; - @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { HttpSession session = request.getSession(false); if (session != null) { - LoggedUser user = new LoggedUser(authentication.getName(), activeUserStore); + LoggedUser user = new LoggedUser(authentication.getName()); session.setAttribute("user", user); } response.sendRedirect("/chat"); diff --git a/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java b/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java index f44b86a..9e5a827 100644 --- a/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java +++ b/chatto/src/main/java/org/ros/chatto/service/DBInitializerService.java @@ -13,11 +13,15 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.stream.Collectors; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.ros.chatto.model.ApplicationStatus; +import org.ros.chatto.model.UserSession; +import org.ros.chatto.repository.UserSessionRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.annotation.PropertySource; @@ -58,6 +62,9 @@ public class DBInitializerService { @PersistenceContext EntityManager entityManager; + + @Autowired + private UserSessionRepository userSessionRepository; private final String tablesCreatedKey = "tables_created"; private final String rolesPopulatedKey = "roles_populated"; @@ -162,6 +169,8 @@ public class DBInitializerService { if (getNumTables() == 0) populateDB(); closeConnection(); + + resetAllUserSessions(userSessionRepository.findAll()); } public void populateDB() throws SQLException, IOException { @@ -242,5 +251,15 @@ public class DBInitializerService { public void closeConnection() throws SQLException { connection.close(); } + + private void resetAllUserSessions(List userSessionsList) + { + List userSessionsResetList = userSessionsList.stream().map(us -> { + us.setNumSessions(0); + us.setOnline(false); + return us; + }).collect(Collectors.toList()); + userSessionRepository.saveAll(userSessionsResetList); + } } \ No newline at end of file diff --git a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java index 282fc8a..048a957 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java @@ -1,5 +1,6 @@ package org.ros.chatto.service; +import java.time.Duration; import java.time.Instant; import java.util.ArrayList; import java.util.HashMap; @@ -10,7 +11,6 @@ import javax.transaction.Transactional; import org.ros.chatto.dto.ActiveUserDTO; import org.ros.chatto.dto.UserRegistrationDTO; -import org.ros.chatto.logged.ActiveUserStore; import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.Role; import org.ros.chatto.model.UserRole; @@ -23,8 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; -import java.time.Duration; - @Service public class UserServiceImpl implements UserService { @Autowired @@ -45,8 +43,6 @@ public class UserServiceImpl implements UserService { @Autowired private UserSessionRepository userSessionRepository; - @Autowired - private ActiveUserStore activeUserStore; @Override @Transactional