65 lines
2.1 KiB
Plaintext
65 lines
2.1 KiB
Plaintext
package com.healthcare.ohctech.service.impl;
|
|
|
|
import com.healthcare.ohctech.converter.RoleToRoleDto;
|
|
import com.healthcare.ohctech.dto.RoleDto;
|
|
import com.healthcare.ohctech.dto.UserDto;
|
|
import com.healthcare.ohctech.entity.OhcType;
|
|
import com.healthcare.ohctech.entity.User;
|
|
import com.healthcare.ohctech.repository.UserRepo;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service
|
|
public class OurUserDetailsService implements UserDetailsService {
|
|
|
|
|
|
private final UserRepo userRepo;
|
|
private final RoleToRoleDto roleToRoleDto;
|
|
|
|
|
|
public OurUserDetailsService(UserRepo userRepo, RoleToRoleDto roleToRoleDto) {
|
|
this.userRepo = userRepo;
|
|
this.roleToRoleDto = roleToRoleDto;
|
|
}
|
|
|
|
@Override
|
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
return userRepo.findByusername(username)
|
|
.orElseThrow();
|
|
}
|
|
|
|
public Set<RoleDto> getRoleByUserId(Integer userId) {
|
|
Optional<User> userOptional = userRepo.findById((long) userId);
|
|
if (userOptional.isPresent()) {
|
|
User user = userOptional.get();
|
|
return user.getRoles().stream().map(role -> roleToRoleDto.convert(role)).collect(Collectors.toSet());
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public Set<OhcType> getOhcByUserId(Integer userId) {
|
|
Optional<User> optionalUser = userRepo.findById((long) userId);
|
|
if (optionalUser.isPresent()) {
|
|
User user = optionalUser.get();
|
|
return user.getOhcTypes();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public List<UserDto> findAll() {
|
|
|
|
return userRepo.findAll()
|
|
.stream()
|
|
.map(user -> new UserDto(user.getUsername()))
|
|
.collect(Collectors.toList());
|
|
|
|
}
|
|
}
|