Exceptions and Errors are better Files structure is better New ComponentContext.ts New DataTable.tsx tables.ts Massive components refactoring New Group.java New LoggingRequestFilter.java LoggingSessionListener.java New NotificationStore.ts SysInfoStore.ts New reactiveValue.ts ReactiveControls.tsx New dependencies And much more
100 lines
3.6 KiB
Java
100 lines
3.6 KiB
Java
package ru.tubryansk.tdms.service;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import ru.tubryansk.tdms.controller.payload.RegistrationDTO;
|
|
import ru.tubryansk.tdms.controller.payload.UserDTO;
|
|
import ru.tubryansk.tdms.entity.Role;
|
|
import ru.tubryansk.tdms.entity.Student;
|
|
import ru.tubryansk.tdms.entity.User;
|
|
import ru.tubryansk.tdms.entity.repository.GroupRepository;
|
|
import ru.tubryansk.tdms.entity.repository.StudentRepository;
|
|
import ru.tubryansk.tdms.entity.repository.UserRepository;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@Service
|
|
@Transactional
|
|
@Slf4j
|
|
public class UserService implements UserDetailsService {
|
|
@Autowired
|
|
private UserRepository userRepository;
|
|
@Autowired
|
|
private GroupRepository groupRepository;
|
|
@Autowired
|
|
private StudentRepository studentRepository;
|
|
@Autowired
|
|
private RoleService roleService;
|
|
@Autowired
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
@Override
|
|
public User loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
log.info("Loading user with username: {}", username);
|
|
User user = userRepository.findUserByLogin(username).orElseThrow(() -> {
|
|
log.info("User with login {} not found", username);
|
|
return new UsernameNotFoundException("User with login " + username + " not found");
|
|
});
|
|
log.info("User with login {} loaded", username);
|
|
return user;
|
|
}
|
|
|
|
public List<UserDTO> getAllUsers() {
|
|
log.info("Loading all users");
|
|
List<UserDTO> users = userRepository.findAll().stream()
|
|
.map(UserDTO::from)
|
|
.toList();
|
|
log.info("{} users loaded", users.size());
|
|
return users;
|
|
}
|
|
|
|
public void registerUser(RegistrationDTO registrationDTO) {
|
|
log.info("Registering new user with login: {}", registrationDTO.getLogin());
|
|
User user = transientUser(registrationDTO);
|
|
Student student = transientStudent(registrationDTO.getStudentData());
|
|
fillRoles(user, registrationDTO);
|
|
|
|
log.info("Saving new user: {}", user);
|
|
userRepository.save(user);
|
|
if (student != null) {
|
|
student.setUser(user);
|
|
log.info("User is student, saving student: {}", student);
|
|
studentRepository.save(student);
|
|
}
|
|
}
|
|
|
|
private User transientUser(RegistrationDTO registrationDTO) {
|
|
User user = new User();
|
|
user.setLogin(registrationDTO.getLogin());
|
|
user.setPassword(passwordEncoder.encode(registrationDTO.getPassword()));
|
|
user.setFullName(registrationDTO.getFullName());
|
|
user.setEmail(registrationDTO.getEmail());
|
|
user.setNumberPhone(registrationDTO.getNumberPhone());
|
|
return user;
|
|
}
|
|
|
|
private Student transientStudent(RegistrationDTO.StudentRegistrationDTO studentData) {
|
|
if (studentData == null) {
|
|
return null;
|
|
}
|
|
|
|
Student student = new Student();
|
|
student.setGroup(groupRepository.findByIdThrow(studentData.getGroupId()));
|
|
return student;
|
|
}
|
|
|
|
private void fillRoles(User user, RegistrationDTO registrationDTO) {
|
|
List<Role> roles = new ArrayList<>();
|
|
if (registrationDTO.getStudentData() != null) {
|
|
roles.add(roleService.getRoleByAuthority(RoleService.Authority.ROLE_STUDENT));
|
|
}
|
|
user.setRoles(roles);
|
|
}
|
|
}
|