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.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<UserRole> userRoles = userRoleRepository.findByUser(username);
System.out.println("Test from userdetails");
log.trace("User Details - loading with username: {}", username);
List<UserRole> 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();
}

View File

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

View File

@ -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;
}
@ -78,13 +77,12 @@ public class UserServiceImpl implements UserService {
List<ActiveUserDTO> activeUserDTOs = new ArrayList<ActiveUserDTO>();
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()));
}
@ -145,4 +143,10 @@ public class UserServiceImpl implements UserService {
// @formatter:on
}
@Override
@Transactional(readOnly = true)
public List<UserRole> getUserWithRole(String userName) {
return userRoleRepository.findByUser(userName);
}
}