changed myuserdetailsservice to use userservice instead of calling userrolerepository directly.

This commit is contained in:
nova 2019-12-09 21:02:25 +05:30
parent f9e6e3085c
commit b83417570d
3 changed files with 27 additions and 20 deletions

View File

@ -4,7 +4,7 @@ import java.util.List;
import org.ros.chatto.model.ChatUser; import org.ros.chatto.model.ChatUser;
import org.ros.chatto.model.UserRole; 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.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails; 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.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
@Service @Service
@Slf4j
public class MyUserDetailsService implements UserDetailsService { public class MyUserDetailsService implements UserDetailsService {
@Autowired @Autowired
private UserRoleRepository userRoleRepository; private UserService userService;
@Override @Override
public UserDetails loadUserByUsername(String username) { public UserDetails loadUserByUsername(String username) {
List<UserRole> userRoles = userRoleRepository.findByUser(username); log.trace("User Details - loading with username: {}", username);
System.out.println("Test from userdetails"); List<UserRole> userRoles = userService.getUserWithRole(username);
if (userRoles.size() == 0) { if (userRoles.size() == 0) {
log.warn("Request for unknown user {}", username);
throw new UsernameNotFoundException(username); throw new UsernameNotFoundException(username);
} }
ChatUser user = userRoles.get(0).getUser(); ChatUser user = userRoles.get(0).getUser();
return User.withUsername(user.getUserName()).password(user.getPassword()) return User.withUsername(user.getUserName()).password(user.getPassword())
.roles(userRoles.stream().map(userRole -> { .roles(userRoles.stream().map(userRole -> {
System.out.println("role = " + userRole.getRole().getName()); log.trace("role = " + userRole.getRole().getName());
return userRole.getRole().getName(); return userRole.getRole().getName();
}).toArray(size -> new String[size])).build(); }).toArray(size -> new String[size])).build();
} }

View File

@ -15,4 +15,5 @@ public interface UserService {
public List<String> getAllRegularUsers(); public List<String> getAllRegularUsers();
public ChatUser findByUserName(String userName); public ChatUser findByUserName(String userName);
public List<ActiveUserDTO> getOtherActiveUsers(String userName); public List<ActiveUserDTO> getOtherActiveUsers(String userName);
public List<UserRole> getUserWithRole(String userName);
} }

View File

@ -36,7 +36,6 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private RoleService roleService; private RoleService roleService;
@Autowired @Autowired
private UserSessionRepository userSessionRepository; private UserSessionRepository userSessionRepository;
@ -83,8 +82,7 @@ public class UserServiceImpl implements UserService {
activeUserDTO.setUserName(u); activeUserDTO.setUserName(u);
activeUserDTO.setOnline(false); activeUserDTO.setOnline(false);
activeUserDTO.setLastActive(null); activeUserDTO.setLastActive(null);
if(us != null) if (us != null) {
{
activeUserDTO.setOnline(us.isOnline()); activeUserDTO.setOnline(us.isOnline());
activeUserDTO.setLastActive(toLastActiveString(us.getTimeStamp())); activeUserDTO.setLastActive(toLastActiveString(us.getTimeStamp()));
} }
@ -145,4 +143,10 @@ public class UserServiceImpl implements UserService {
// @formatter:on // @formatter:on
} }
@Override
@Transactional(readOnly = true)
public List<UserRole> getUserWithRole(String userName) {
return userRoleRepository.findByUser(userName);
}
} }