From b83417570d7efef324362abba61f39650603d116 Mon Sep 17 00:00:00 2001 From: nova Date: Mon, 9 Dec 2019 21:02:25 +0530 Subject: [PATCH] changed myuserdetailsservice to use userservice instead of calling userrolerepository directly. --- .../chatto/security/MyUserDetailsService.java | 16 +++++----- .../org/ros/chatto/service/UserService.java | 1 + .../ros/chatto/service/UserServiceImpl.java | 30 +++++++++++-------- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/chatto/src/main/java/org/ros/chatto/security/MyUserDetailsService.java b/chatto/src/main/java/org/ros/chatto/security/MyUserDetailsService.java index 882d9ef..0310cea 100644 --- a/chatto/src/main/java/org/ros/chatto/security/MyUserDetailsService.java +++ b/chatto/src/main/java/org/ros/chatto/security/MyUserDetailsService.java @@ -4,7 +4,7 @@ import java.util.List; import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.UserRole; -import org.ros.chatto.repository.UserRoleRepository; +import org.ros.chatto.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; @@ -12,26 +12,28 @@ import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import lombok.extern.slf4j.Slf4j; + @Service +@Slf4j public class MyUserDetailsService implements UserDetailsService { @Autowired - private UserRoleRepository userRoleRepository; - + private UserService userService; @Override public UserDetails loadUserByUsername(String username) { - List userRoles = userRoleRepository.findByUser(username); - System.out.println("Test from userdetails"); - + log.trace("User Details - loading with username: {}", username); + List userRoles = userService.getUserWithRole(username); if (userRoles.size() == 0) { + log.warn("Request for unknown user {}", username); throw new UsernameNotFoundException(username); } ChatUser user = userRoles.get(0).getUser(); return User.withUsername(user.getUserName()).password(user.getPassword()) .roles(userRoles.stream().map(userRole -> { - System.out.println("role = " + userRole.getRole().getName()); + log.trace("role = " + userRole.getRole().getName()); return userRole.getRole().getName(); }).toArray(size -> new String[size])).build(); } diff --git a/chatto/src/main/java/org/ros/chatto/service/UserService.java b/chatto/src/main/java/org/ros/chatto/service/UserService.java index 56a97bc..d849ff0 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserService.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserService.java @@ -15,4 +15,5 @@ public interface UserService { public List getAllRegularUsers(); public ChatUser findByUserName(String userName); public List getOtherActiveUsers(String userName); + public List getUserWithRole(String userName); } 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 15733cb..598d5b3 100644 --- a/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java +++ b/chatto/src/main/java/org/ros/chatto/service/UserServiceImpl.java @@ -31,12 +31,11 @@ public class UserServiceImpl implements UserService { private UserRoleRepository userRoleRepository; @Autowired - private PasswordEncoder passwordEncoder; - + private PasswordEncoder passwordEncoder; + @Autowired private RoleService roleService; - @Autowired private UserSessionRepository userSessionRepository; @@ -50,8 +49,8 @@ public class UserServiceImpl implements UserService { Role role = roleService.getRole("USER"); userRole.setRole(role); userRole.setUser(changedUser); -// System.out.println(role.getRoleID()); -// System.out.println(changedUser.getUserID()); + // System.out.println(role.getRoleID()); + // System.out.println(changedUser.getUserID()); userRoleRepository.save(userRole); return userRole; } @@ -71,27 +70,26 @@ public class UserServiceImpl implements UserService { @Transactional(readOnly = true) public List getOtherActiveUsers(String userName) { List userList = findAllOtherUsers(userName); - + List userSessionsList = userSessionRepository.findAllUserSessions(); Map lastActiveMap = convertToMap(userSessionsList); - + List activeUserDTOs = new ArrayList(); userList.forEach(u -> { ActiveUserDTO activeUserDTO = new ActiveUserDTO(); - UserSession us = lastActiveMap.get(u); - + UserSession us = lastActiveMap.get(u); + activeUserDTO.setUserName(u); activeUserDTO.setOnline(false); activeUserDTO.setLastActive(null); - if(us != null) - { + if (us != null) { activeUserDTO.setOnline(us.isOnline()); activeUserDTO.setLastActive(toLastActiveString(us.getTimeStamp())); } - + activeUserDTOs.add(activeUserDTO); }); - + return activeUserDTOs; } @@ -145,4 +143,10 @@ public class UserServiceImpl implements UserService { // @formatter:on } + + @Override + @Transactional(readOnly = true) + public List getUserWithRole(String userName) { + return userRoleRepository.findByUser(userName); + } }