application now resets online flag and num_sessions to 0 on startup
This commit is contained in:
parent
2564fdca35
commit
5a79c7026e
@ -1,7 +1,6 @@
|
|||||||
package org.ros.chatto;
|
package org.ros.chatto;
|
||||||
|
|
||||||
import org.modelmapper.ModelMapper;
|
import org.modelmapper.ModelMapper;
|
||||||
import org.ros.chatto.logged.ActiveUserStore;
|
|
||||||
import org.ros.chatto.security.AuthenticationSuccessHandlerImpl;
|
import org.ros.chatto.security.AuthenticationSuccessHandlerImpl;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
@ -45,11 +44,6 @@ public class BeanConfigurations {
|
|||||||
return messageSource;
|
return messageSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
ActiveUserStore activeUserStore() {
|
|
||||||
return new ActiveUserStore();
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Bean
|
// @Bean
|
||||||
// public Connection connection() throws SQLException
|
// public Connection connection() throws SQLException
|
||||||
// {
|
// {
|
||||||
|
@ -7,7 +7,6 @@ import javax.servlet.http.HttpServletRequest;
|
|||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
import org.ros.chatto.dto.ActiveUserDTO;
|
import org.ros.chatto.dto.ActiveUserDTO;
|
||||||
import org.ros.chatto.logged.ActiveUserStore;
|
|
||||||
import org.ros.chatto.model.ChatMessage;
|
import org.ros.chatto.model.ChatMessage;
|
||||||
import org.ros.chatto.model.ChatUser;
|
import org.ros.chatto.model.ChatUser;
|
||||||
import org.ros.chatto.model.MessageCipher;
|
import org.ros.chatto.model.MessageCipher;
|
||||||
@ -51,8 +50,6 @@ public class DemoRestController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
ChatMessageRepository chatMessageRepository;
|
ChatMessageRepository chatMessageRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
ActiveUserStore activeUserStore;
|
|
||||||
@Autowired
|
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
@GetMapping("/users")
|
@GetMapping("/users")
|
||||||
public List<ChatUser> getAllUsers() {
|
public List<ChatUser> getAllUsers() {
|
||||||
@ -128,11 +125,6 @@ public class DemoRestController {
|
|||||||
return "redirect:/users";
|
return "redirect:/users";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/loggedUsers")
|
|
||||||
public ActiveUserStore getActiveUsers() {
|
|
||||||
return activeUserStore;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping(value = "/loggedUsers2")
|
@GetMapping(value = "/loggedUsers2")
|
||||||
public List<ActiveUserDTO> getOtherActiveUsers(Principal principal)
|
public List<ActiveUserDTO> getOtherActiveUsers(Principal principal)
|
||||||
{
|
{
|
||||||
|
@ -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;
|
|
||||||
}
|
|
@ -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<String> users;
|
|
||||||
|
|
||||||
public List<ActiveUser> activeUsers;
|
|
||||||
|
|
||||||
public ActiveUserStore() {
|
|
||||||
users = new ArrayList<String>();
|
|
||||||
activeUsers = new ArrayList<>();
|
|
||||||
}
|
|
||||||
}
|
|
@ -26,14 +26,11 @@ import lombok.Setter;
|
|||||||
public class LoggedUser implements HttpSessionBindingListener {
|
public class LoggedUser implements HttpSessionBindingListener {
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
private ActiveUserStore activeUserStore;
|
|
||||||
|
|
||||||
public LoggedUser(String username, ActiveUserStore activeUserStore) {
|
public LoggedUser(String username)
|
||||||
|
{
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.activeUserStore = activeUserStore;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public LoggedUser() {
|
public LoggedUser() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,27 +38,9 @@ public class LoggedUser implements HttpSessionBindingListener {
|
|||||||
public void valueBound(HttpSessionBindingEvent event) {
|
public void valueBound(HttpSessionBindingEvent event) {
|
||||||
UserService userService = getUserService(event);
|
UserService userService = getUserService(event);
|
||||||
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
|
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
|
||||||
List<String> users = activeUserStore.getUsers();
|
|
||||||
List<ActiveUser> activeUsers = activeUserStore.getActiveUsers();
|
|
||||||
LoggedUser user = (LoggedUser) event.getValue();
|
LoggedUser user = (LoggedUser) event.getValue();
|
||||||
if (!users.contains(user.getUsername())) {
|
|
||||||
users.add(user.getUsername());
|
|
||||||
}
|
|
||||||
|
|
||||||
Instant instant = Instant.now();
|
Instant instant = Instant.now();
|
||||||
// Optional<ActiveUser> 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());
|
ChatUser chatUser = userService.findByUserName(user.getUsername());
|
||||||
|
|
||||||
UserSession userSession = userSessionRepository.findByUserName(user.getUsername());
|
UserSession userSession = userSessionRepository.findByUserName(user.getUsername());
|
||||||
@ -79,27 +58,13 @@ public class LoggedUser implements HttpSessionBindingListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void valueUnbound(HttpSessionBindingEvent event) {
|
public void valueUnbound(HttpSessionBindingEvent event) {
|
||||||
List<String> users = activeUserStore.getUsers();
|
|
||||||
LoggedUser user = (LoggedUser) event.getValue();
|
LoggedUser user = (LoggedUser) event.getValue();
|
||||||
List<ActiveUser> activeUsers = activeUserStore.getActiveUsers();
|
|
||||||
|
|
||||||
UserService userService = getUserService(event);
|
UserService userService = getUserService(event);
|
||||||
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
|
UserSessionRepository userSessionRepository = getUserSessionRepository(event);
|
||||||
Instant instant = Instant.now();
|
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());
|
ChatUser chatUser = userService.findByUserName(user.getUsername());
|
||||||
|
@ -14,16 +14,13 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component("myAuthenticationSuccessHandler")
|
@Component("myAuthenticationSuccessHandler")
|
||||||
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
public class MySimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
ActiveUserStore activeUserStore;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAuthenticationSuccess(HttpServletRequest request,
|
public void onAuthenticationSuccess(HttpServletRequest request,
|
||||||
HttpServletResponse response, Authentication authentication)
|
HttpServletResponse response, Authentication authentication)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
HttpSession session = request.getSession(false);
|
HttpSession session = request.getSession(false);
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
LoggedUser user = new LoggedUser(authentication.getName(), activeUserStore);
|
LoggedUser user = new LoggedUser(authentication.getName());
|
||||||
session.setAttribute("user", user);
|
session.setAttribute("user", user);
|
||||||
}
|
}
|
||||||
response.sendRedirect("/chat");
|
response.sendRedirect("/chat");
|
||||||
|
@ -13,11 +13,15 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.PersistenceContext;
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
import org.ros.chatto.model.ApplicationStatus;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
@ -58,6 +62,9 @@ public class DBInitializerService {
|
|||||||
|
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
EntityManager entityManager;
|
EntityManager entityManager;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserSessionRepository userSessionRepository;
|
||||||
|
|
||||||
private final String tablesCreatedKey = "tables_created";
|
private final String tablesCreatedKey = "tables_created";
|
||||||
private final String rolesPopulatedKey = "roles_populated";
|
private final String rolesPopulatedKey = "roles_populated";
|
||||||
@ -162,6 +169,8 @@ public class DBInitializerService {
|
|||||||
if (getNumTables() == 0)
|
if (getNumTables() == 0)
|
||||||
populateDB();
|
populateDB();
|
||||||
closeConnection();
|
closeConnection();
|
||||||
|
|
||||||
|
resetAllUserSessions(userSessionRepository.findAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void populateDB() throws SQLException, IOException {
|
public void populateDB() throws SQLException, IOException {
|
||||||
@ -242,5 +251,15 @@ public class DBInitializerService {
|
|||||||
public void closeConnection() throws SQLException {
|
public void closeConnection() throws SQLException {
|
||||||
connection.close();
|
connection.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void resetAllUserSessions(List<UserSession> userSessionsList)
|
||||||
|
{
|
||||||
|
List<UserSession> userSessionsResetList = userSessionsList.stream().map(us -> {
|
||||||
|
us.setNumSessions(0);
|
||||||
|
us.setOnline(false);
|
||||||
|
return us;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
userSessionRepository.saveAll(userSessionsResetList);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package org.ros.chatto.service;
|
package org.ros.chatto.service;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -10,7 +11,6 @@ import javax.transaction.Transactional;
|
|||||||
|
|
||||||
import org.ros.chatto.dto.ActiveUserDTO;
|
import org.ros.chatto.dto.ActiveUserDTO;
|
||||||
import org.ros.chatto.dto.UserRegistrationDTO;
|
import org.ros.chatto.dto.UserRegistrationDTO;
|
||||||
import org.ros.chatto.logged.ActiveUserStore;
|
|
||||||
import org.ros.chatto.model.ChatUser;
|
import org.ros.chatto.model.ChatUser;
|
||||||
import org.ros.chatto.model.Role;
|
import org.ros.chatto.model.Role;
|
||||||
import org.ros.chatto.model.UserRole;
|
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.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -45,8 +43,6 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserSessionRepository userSessionRepository;
|
private UserSessionRepository userSessionRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private ActiveUserStore activeUserStore;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
|
Loading…
Reference in New Issue
Block a user