[74af394] | 1 | package com.example.salonbella.service;
|
---|
| 2 |
|
---|
| 3 | import com.example.salonbella.controller.register.RegisterRequest;
|
---|
| 4 | import com.example.salonbella.entity.ConfirmationTokenEntity;
|
---|
| 5 | import com.example.salonbella.entity.UserEntity;
|
---|
| 6 | import com.example.salonbella.repository.UserRepository;
|
---|
| 7 | import com.example.salonbella.service.mail.ConfirmationTokenService;
|
---|
| 8 | import com.example.salonbella.service.mail.EmailService;
|
---|
| 9 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 10 | import org.springframework.security.core.userdetails.User;
|
---|
| 11 | import org.springframework.security.core.userdetails.UserDetails;
|
---|
| 12 | import org.springframework.security.core.userdetails.UserDetailsService;
|
---|
| 13 | import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
---|
| 14 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
| 15 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
| 16 | import org.springframework.stereotype.Service;
|
---|
| 17 |
|
---|
| 18 | import java.time.LocalDateTime;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.UUID;
|
---|
| 21 |
|
---|
| 22 | @Service
|
---|
| 23 | public class UserService implements UserDetailsService {
|
---|
| 24 | private final UserRepository repository;
|
---|
| 25 | private final ConfirmationTokenService confirmationTokenService;
|
---|
| 26 | private final EmailService emailService;
|
---|
| 27 |
|
---|
| 28 | @Autowired
|
---|
| 29 | public UserService(UserRepository repository, ConfirmationTokenService confirmationTokenService, EmailService emailService) {
|
---|
| 30 | this.repository = repository;
|
---|
| 31 | this.confirmationTokenService = confirmationTokenService;
|
---|
| 32 | this.emailService = emailService;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public void registerNewUser(RegisterRequest registerRequest) {
|
---|
| 36 | PasswordEncoder encoder = new BCryptPasswordEncoder(10);
|
---|
| 37 | UserEntity userEntity = new UserEntity();
|
---|
| 38 | userEntity.setName(registerRequest.getName());
|
---|
| 39 | userEntity.setSurname(registerRequest.getSurname());
|
---|
| 40 | userEntity.setNumber(registerRequest.getNumber());
|
---|
| 41 | userEntity.setEmail(registerRequest.getEmail());
|
---|
| 42 | userEntity.setUsername(registerRequest.getUsername());
|
---|
| 43 | userEntity.setPassword(encoder.encode(registerRequest.getPassword()));
|
---|
| 44 |
|
---|
| 45 | String token = UUID.randomUUID().toString();
|
---|
| 46 | ConfirmationTokenEntity confirmationTokenEntity = new ConfirmationTokenEntity(
|
---|
| 47 | token,
|
---|
| 48 | LocalDateTime.now(),
|
---|
| 49 | LocalDateTime.now().plusMinutes(30),
|
---|
| 50 | userEntity
|
---|
| 51 | );
|
---|
| 52 | repository.save(userEntity);
|
---|
| 53 | confirmationTokenService.saveConfirmationToken(confirmationTokenEntity);
|
---|
| 54 | emailService.send(registerRequest.getEmail(), registerRequest.getName(), token);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public void registerAdmin(UserEntity userEntity) {
|
---|
| 58 | repository.save(userEntity);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public boolean checkUsernameExists(String username) {
|
---|
| 62 | List<UserEntity> userEntities = repository.findAll();
|
---|
| 63 | for (UserEntity u : userEntities) {
|
---|
| 64 | if (u.getUsername().equalsIgnoreCase(username)) {
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | return false;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public boolean checkEmailExists(String email) {
|
---|
| 72 | List<UserEntity> userEntities = repository.findAll();
|
---|
| 73 | for (UserEntity u : userEntities) {
|
---|
| 74 | if (u.getEmail().equalsIgnoreCase(email)) {
|
---|
| 75 | return true;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 | return false;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | @Override
|
---|
| 82 | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
---|
| 83 |
|
---|
| 84 | final UserEntity user = repository.findByUsername(username);
|
---|
| 85 | if (user == null || !user.isValid())
|
---|
| 86 | throw new UsernameNotFoundException(username);
|
---|
| 87 | return User.withUsername(user.getUsername()).password(user.getPassword()).roles(user.getRole()).build();
|
---|
| 88 | }
|
---|
| 89 | }
|
---|