| | |
| | | package com.pollex.pam.service; |
| | | |
| | | import com.pollex.pam.config.Constants; |
| | | import com.pollex.pam.domain.Authority; |
| | | import com.pollex.pam.domain.User; |
| | | import com.pollex.pam.repository.AuthorityRepository; |
| | | import com.pollex.pam.repository.UserRepository; |
| | | import com.pollex.pam.security.AuthoritiesConstants; |
| | | import com.pollex.pam.security.SecurityUtils; |
| | | import com.pollex.pam.service.dto.AdminUserDTO; |
| | | import com.pollex.pam.service.dto.UserDTO; |
| | | import java.time.Instant; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.Optional; |
| | | import java.util.Set; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.cache.CacheManager; |
| | |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import com.pollex.pam.config.Constants; |
| | | import com.pollex.pam.domain.Authority; |
| | | import com.pollex.pam.domain.User; |
| | | import com.pollex.pam.repository.AuthorityRepository; |
| | | import com.pollex.pam.repository.UserRepository; |
| | | import com.pollex.pam.security.SecurityUtils; |
| | | import com.pollex.pam.service.dto.AdminUserDTO; |
| | | import com.pollex.pam.service.dto.UserDTO; |
| | | |
| | | import tech.jhipster.security.RandomUtil; |
| | | |
| | | /** |
| | |
| | | private final AuthorityRepository authorityRepository; |
| | | |
| | | private final CacheManager cacheManager; |
| | | |
| | | |
| | | public UserService( |
| | | UserRepository userRepository, |
| | |
| | | }); |
| | | } |
| | | |
| | | public User registerUser(AdminUserDTO userDTO, String password) { |
| | | userRepository |
| | | .findOneByLogin(userDTO.getLogin().toLowerCase()) |
| | | .ifPresent(existingUser -> { |
| | | boolean removed = removeNonActivatedUser(existingUser); |
| | | if (!removed) { |
| | | throw new UsernameAlreadyUsedException(); |
| | | } |
| | | }); |
| | | userRepository |
| | | .findOneByEmailIgnoreCase(userDTO.getEmail()) |
| | | .ifPresent(existingUser -> { |
| | | boolean removed = removeNonActivatedUser(existingUser); |
| | | if (!removed) { |
| | | throw new EmailAlreadyUsedException(); |
| | | } |
| | | }); |
| | | User newUser = new User(); |
| | | String encryptedPassword = passwordEncoder.encode(password); |
| | | newUser.setLogin(userDTO.getLogin().toLowerCase()); |
| | | // new user gets initially a generated password |
| | | newUser.setPassword(encryptedPassword); |
| | | newUser.setFirstName(userDTO.getFirstName()); |
| | | newUser.setLastName(userDTO.getLastName()); |
| | | if (userDTO.getEmail() != null) { |
| | | newUser.setEmail(userDTO.getEmail().toLowerCase()); |
| | | } |
| | | newUser.setImageUrl(userDTO.getImageUrl()); |
| | | newUser.setLangKey(userDTO.getLangKey()); |
| | | // new user is not active |
| | | newUser.setActivated(false); |
| | | // new user gets registration key |
| | | newUser.setActivationKey(RandomUtil.generateActivationKey()); |
| | | Set<Authority> authorities = new HashSet<>(); |
| | | authorityRepository.findById(AuthoritiesConstants.USER).ifPresent(authorities::add); |
| | | newUser.setAuthorities(authorities); |
| | | userRepository.save(newUser); |
| | | this.clearUserCaches(newUser); |
| | | log.debug("Created Information for User: {}", newUser); |
| | | return newUser; |
| | | } |
| | | // public User registerUser(AdminUserDTO userDTO, String password) { |
| | | // userRepository |
| | | // .findOneByLogin(userDTO.getLogin().toLowerCase()) |
| | | // .ifPresent(existingUser -> { |
| | | // boolean removed = removeNonActivatedUser(existingUser); |
| | | // if (!removed) { |
| | | // throw new UsernameAlreadyUsedException(); |
| | | // } |
| | | // }); |
| | | // userRepository |
| | | // .findOneByEmailIgnoreCase(userDTO.getEmail()) |
| | | // .ifPresent(existingUser -> { |
| | | // boolean removed = removeNonActivatedUser(existingUser); |
| | | // if (!removed) { |
| | | // throw new EmailAlreadyUsedException(); |
| | | // } |
| | | // }); |
| | | // User newUser = new User(); |
| | | // String encryptedPassword = passwordEncoder.encode(password); |
| | | // newUser.setLogin(userDTO.getLogin().toLowerCase()); |
| | | // // new user gets initially a generated password |
| | | // newUser.setPassword(encryptedPassword); |
| | | // newUser.setFirstName(userDTO.getFirstName()); |
| | | // newUser.setLastName(userDTO.getLastName()); |
| | | // if (userDTO.getEmail() != null) { |
| | | // newUser.setEmail(userDTO.getEmail().toLowerCase()); |
| | | // } |
| | | // newUser.setImageUrl(userDTO.getImageUrl()); |
| | | // newUser.setLangKey(userDTO.getLangKey()); |
| | | // // new user is not active |
| | | // newUser.setActivated(false); |
| | | // // new user gets registration key |
| | | // newUser.setActivationKey(RandomUtil.generateActivationKey()); |
| | | // Set<Authority> authorities = new HashSet<>(); |
| | | // authorityRepository.findById(AuthoritiesConstants.USER).ifPresent(authorities::add); |
| | | // newUser.setAuthorities(authorities); |
| | | // userRepository.save(newUser); |
| | | // this.clearUserCaches(newUser); |
| | | // log.debug("Created Information for User: {}", newUser); |
| | | // return newUser; |
| | | // } |
| | | |
| | | private boolean removeNonActivatedUser(User existingUser) { |
| | | if (existingUser.isActivated()) { |
| | |
| | | Objects.requireNonNull(cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE)).evict(user.getEmail()); |
| | | } |
| | | } |
| | | |
| | | |
| | | } |