|
|
|
@ -34,270 +34,255 @@ import java.util.stream.Collectors;
|
|
|
|
|
@Transactional("epasMedTransactionManager")
|
|
|
|
|
public class UserService {
|
|
|
|
|
|
|
|
|
|
private final Logger log = LoggerFactory.getLogger(UserService.class);
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(UserService.class);
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
private final PasswordEncoder passwordEncoder;
|
|
|
|
|
|
|
|
|
|
private final PersistentTokenRepository persistentTokenRepository;
|
|
|
|
|
private final PersistentTokenRepository persistentTokenRepository;
|
|
|
|
|
|
|
|
|
|
private final AuthorityRepository authorityRepository;
|
|
|
|
|
private final AuthorityRepository authorityRepository;
|
|
|
|
|
|
|
|
|
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, PersistentTokenRepository persistentTokenRepository, AuthorityRepository authorityRepository) {
|
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
|
this.persistentTokenRepository = persistentTokenRepository;
|
|
|
|
|
this.authorityRepository = authorityRepository;
|
|
|
|
|
}
|
|
|
|
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder,
|
|
|
|
|
PersistentTokenRepository persistentTokenRepository, AuthorityRepository authorityRepository) {
|
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
|
this.passwordEncoder = passwordEncoder;
|
|
|
|
|
this.persistentTokenRepository = persistentTokenRepository;
|
|
|
|
|
this.authorityRepository = authorityRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<User> activateRegistration(String key) {
|
|
|
|
|
log.debug("Activating user for activation key {}", key);
|
|
|
|
|
return userRepository.findOneByActivationKey(key)
|
|
|
|
|
.map(user -> {
|
|
|
|
|
// activate given user for the registration key.
|
|
|
|
|
user.setActivated(true);
|
|
|
|
|
user.setActivationKey(null);
|
|
|
|
|
log.debug("Activated user: {}", user);
|
|
|
|
|
return user;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public Optional<User> activateRegistration(String key) {
|
|
|
|
|
logger.debug("Activating user for activation key {}", key);
|
|
|
|
|
return userRepository.findOneByActivationKey(key).map(user -> {
|
|
|
|
|
// activate given user for the registration key.
|
|
|
|
|
user.setActivated(true);
|
|
|
|
|
user.setActivationKey(null);
|
|
|
|
|
logger.debug("Activated user: {}", user);
|
|
|
|
|
return user;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<User> completePasswordReset(String newPassword, String key) {
|
|
|
|
|
log.debug("Reset user password for reset key {}", key);
|
|
|
|
|
return userRepository.findOneByResetKey(key)
|
|
|
|
|
.filter(user -> user.getResetDate().isAfter(Instant.now().minusSeconds(86400)))
|
|
|
|
|
.map(user -> {
|
|
|
|
|
user.setPassword(passwordEncoder.encode(newPassword));
|
|
|
|
|
user.setResetKey(null);
|
|
|
|
|
user.setResetDate(null);
|
|
|
|
|
return user;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public Optional<User> completePasswordReset(String newPassword, String key) {
|
|
|
|
|
logger.debug("Reset user password for reset key {}", key);
|
|
|
|
|
return userRepository.findOneByResetKey(key)
|
|
|
|
|
.filter(user -> user.getResetDate().isAfter(Instant.now().minusSeconds(86400))).map(user -> {
|
|
|
|
|
user.setPassword(passwordEncoder.encode(newPassword));
|
|
|
|
|
user.setResetKey(null);
|
|
|
|
|
user.setResetDate(null);
|
|
|
|
|
return user;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<User> requestPasswordReset(String mail) {
|
|
|
|
|
return userRepository.findOneByEmailIgnoreCase(mail)
|
|
|
|
|
.filter(User::getActivated)
|
|
|
|
|
.map(user -> {
|
|
|
|
|
user.setResetKey(RandomUtil.generateResetKey());
|
|
|
|
|
user.setResetDate(Instant.now());
|
|
|
|
|
return user;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public Optional<User> requestPasswordReset(String mail) {
|
|
|
|
|
return userRepository.findOneByEmailIgnoreCase(mail).filter(User::getActivated).map(user -> {
|
|
|
|
|
user.setResetKey(RandomUtil.generateResetKey());
|
|
|
|
|
user.setResetDate(Instant.now());
|
|
|
|
|
return user;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User registerUser(UserDTO 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);
|
|
|
|
|
log.debug("Created Information for User: {}", newUser);
|
|
|
|
|
return newUser;
|
|
|
|
|
}
|
|
|
|
|
public User registerUser(UserDTO 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);
|
|
|
|
|
logger.debug("Created Information for User: {}", newUser);
|
|
|
|
|
return newUser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean removeNonActivatedUser(User existingUser) {
|
|
|
|
|
if (existingUser.getActivated()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
userRepository.delete(existingUser);
|
|
|
|
|
userRepository.flush();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private boolean removeNonActivatedUser(User existingUser) {
|
|
|
|
|
if (existingUser.getActivated()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
userRepository.delete(existingUser);
|
|
|
|
|
userRepository.flush();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User createUser(UserDTO userDTO) {
|
|
|
|
|
User user = new User();
|
|
|
|
|
user.setLogin(userDTO.getLogin().toLowerCase());
|
|
|
|
|
user.setFirstName(userDTO.getFirstName());
|
|
|
|
|
user.setLastName(userDTO.getLastName());
|
|
|
|
|
if (userDTO.getEmail() != null) {
|
|
|
|
|
user.setEmail(userDTO.getEmail().toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
user.setImageUrl(userDTO.getImageUrl());
|
|
|
|
|
if (userDTO.getLangKey() == null) {
|
|
|
|
|
user.setLangKey(Constants.DEFAULT_LANGUAGE); // default language
|
|
|
|
|
} else {
|
|
|
|
|
user.setLangKey(userDTO.getLangKey());
|
|
|
|
|
}
|
|
|
|
|
String encryptedPassword = passwordEncoder.encode(RandomUtil.generatePassword());
|
|
|
|
|
user.setPassword(encryptedPassword);
|
|
|
|
|
user.setResetKey(RandomUtil.generateResetKey());
|
|
|
|
|
user.setResetDate(Instant.now());
|
|
|
|
|
user.setActivated(true);
|
|
|
|
|
if (userDTO.getAuthorities() != null) {
|
|
|
|
|
Set<Authority> authorities = userDTO.getAuthorities().stream()
|
|
|
|
|
.map(authorityRepository::findById)
|
|
|
|
|
.filter(Optional::isPresent)
|
|
|
|
|
.map(Optional::get)
|
|
|
|
|
.collect(Collectors.toSet());
|
|
|
|
|
user.setAuthorities(authorities);
|
|
|
|
|
}
|
|
|
|
|
userRepository.save(user);
|
|
|
|
|
log.debug("Created Information for User: {}", user);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
public User createUser(UserDTO userDTO) {
|
|
|
|
|
User user = new User();
|
|
|
|
|
user.setLogin(userDTO.getLogin().toLowerCase());
|
|
|
|
|
user.setFirstName(userDTO.getFirstName());
|
|
|
|
|
user.setLastName(userDTO.getLastName());
|
|
|
|
|
if (userDTO.getEmail() != null) {
|
|
|
|
|
user.setEmail(userDTO.getEmail().toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
user.setImageUrl(userDTO.getImageUrl());
|
|
|
|
|
if (userDTO.getLangKey() == null) {
|
|
|
|
|
user.setLangKey(Constants.DEFAULT_LANGUAGE); // default language
|
|
|
|
|
} else {
|
|
|
|
|
user.setLangKey(userDTO.getLangKey());
|
|
|
|
|
}
|
|
|
|
|
String encryptedPassword = passwordEncoder.encode(RandomUtil.generatePassword());
|
|
|
|
|
user.setPassword(encryptedPassword);
|
|
|
|
|
user.setResetKey(RandomUtil.generateResetKey());
|
|
|
|
|
user.setResetDate(Instant.now());
|
|
|
|
|
user.setActivated(true);
|
|
|
|
|
if (userDTO.getAuthorities() != null) {
|
|
|
|
|
Set<Authority> authorities = userDTO.getAuthorities().stream().map(authorityRepository::findById)
|
|
|
|
|
.filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());
|
|
|
|
|
user.setAuthorities(authorities);
|
|
|
|
|
}
|
|
|
|
|
userRepository.save(user);
|
|
|
|
|
logger.debug("Created Information for User: {}", user);
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update all information for a specific user, and return the modified user.
|
|
|
|
|
*
|
|
|
|
|
* @param userDTO user to update.
|
|
|
|
|
* @return updated user.
|
|
|
|
|
*/
|
|
|
|
|
public Optional<UserDTO> updateUser(UserDTO userDTO) {
|
|
|
|
|
return Optional.of(userRepository
|
|
|
|
|
.findById(userDTO.getId()))
|
|
|
|
|
.filter(Optional::isPresent)
|
|
|
|
|
.map(Optional::get)
|
|
|
|
|
.map(user -> {
|
|
|
|
|
user.setLogin(userDTO.getLogin().toLowerCase());
|
|
|
|
|
user.setFirstName(userDTO.getFirstName());
|
|
|
|
|
user.setLastName(userDTO.getLastName());
|
|
|
|
|
if (userDTO.getEmail() != null) {
|
|
|
|
|
user.setEmail(userDTO.getEmail().toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
user.setImageUrl(userDTO.getImageUrl());
|
|
|
|
|
user.setActivated(userDTO.isActivated());
|
|
|
|
|
user.setLangKey(userDTO.getLangKey());
|
|
|
|
|
Set<Authority> managedAuthorities = user.getAuthorities();
|
|
|
|
|
managedAuthorities.clear();
|
|
|
|
|
userDTO.getAuthorities().stream()
|
|
|
|
|
.map(authorityRepository::findById)
|
|
|
|
|
.filter(Optional::isPresent)
|
|
|
|
|
.map(Optional::get)
|
|
|
|
|
.forEach(managedAuthorities::add);
|
|
|
|
|
log.debug("Changed Information for User: {}", user);
|
|
|
|
|
return user;
|
|
|
|
|
})
|
|
|
|
|
.map(UserDTO::new);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Update all information for a specific user, and return the modified user.
|
|
|
|
|
*
|
|
|
|
|
* @param userDTO user to update.
|
|
|
|
|
* @return updated user.
|
|
|
|
|
*/
|
|
|
|
|
public Optional<UserDTO> updateUser(UserDTO userDTO) {
|
|
|
|
|
return Optional.of(userRepository.findById(userDTO.getId())).filter(Optional::isPresent).map(Optional::get)
|
|
|
|
|
.map(user -> {
|
|
|
|
|
user.setLogin(userDTO.getLogin().toLowerCase());
|
|
|
|
|
user.setFirstName(userDTO.getFirstName());
|
|
|
|
|
user.setLastName(userDTO.getLastName());
|
|
|
|
|
if (userDTO.getEmail() != null) {
|
|
|
|
|
user.setEmail(userDTO.getEmail().toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
user.setImageUrl(userDTO.getImageUrl());
|
|
|
|
|
user.setActivated(userDTO.isActivated());
|
|
|
|
|
user.setLangKey(userDTO.getLangKey());
|
|
|
|
|
Set<Authority> managedAuthorities = user.getAuthorities();
|
|
|
|
|
managedAuthorities.clear();
|
|
|
|
|
userDTO.getAuthorities().stream().map(authorityRepository::findById).filter(Optional::isPresent)
|
|
|
|
|
.map(Optional::get).forEach(managedAuthorities::add);
|
|
|
|
|
logger.debug("Changed Information for User: {}", user);
|
|
|
|
|
return user;
|
|
|
|
|
}).map(UserDTO::new);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void deleteUser(String login) {
|
|
|
|
|
userRepository.findOneByLogin(login).ifPresent(user -> {
|
|
|
|
|
userRepository.delete(user);
|
|
|
|
|
log.debug("Deleted User: {}", user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
public void deleteUser(String login) {
|
|
|
|
|
userRepository.findOneByLogin(login).ifPresent(user -> {
|
|
|
|
|
userRepository.delete(user);
|
|
|
|
|
logger.debug("Deleted User: {}", user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update basic information (first name, last name, email, language) for the current user.
|
|
|
|
|
*
|
|
|
|
|
* @param firstName first name of user.
|
|
|
|
|
* @param lastName last name of user.
|
|
|
|
|
* @param email email id of user.
|
|
|
|
|
* @param langKey language key.
|
|
|
|
|
* @param imageUrl image URL of user.
|
|
|
|
|
*/
|
|
|
|
|
public void updateUser(String firstName, String lastName, String email, String langKey, String imageUrl) {
|
|
|
|
|
SecurityUtils.getCurrentUserLogin()
|
|
|
|
|
.flatMap(userRepository::findOneByLogin)
|
|
|
|
|
.ifPresent(user -> {
|
|
|
|
|
user.setFirstName(firstName);
|
|
|
|
|
user.setLastName(lastName);
|
|
|
|
|
if (email != null) {
|
|
|
|
|
user.setEmail(email.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
user.setLangKey(langKey);
|
|
|
|
|
user.setImageUrl(imageUrl);
|
|
|
|
|
log.debug("Changed Information for User: {}", user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Update basic information (first name, last name, email, language) for the
|
|
|
|
|
* current user.
|
|
|
|
|
*
|
|
|
|
|
* @param firstName first name of user.
|
|
|
|
|
* @param lastName last name of user.
|
|
|
|
|
* @param email email id of user.
|
|
|
|
|
* @param langKey language key.
|
|
|
|
|
* @param imageUrl image URL of user.
|
|
|
|
|
*/
|
|
|
|
|
public void updateUser(String firstName, String lastName, String email, String langKey, String imageUrl) {
|
|
|
|
|
SecurityUtils.getCurrentUserLogin().flatMap(userRepository::findOneByLogin).ifPresent(user -> {
|
|
|
|
|
user.setFirstName(firstName);
|
|
|
|
|
user.setLastName(lastName);
|
|
|
|
|
if (email != null) {
|
|
|
|
|
user.setEmail(email.toLowerCase());
|
|
|
|
|
}
|
|
|
|
|
user.setLangKey(langKey);
|
|
|
|
|
user.setImageUrl(imageUrl);
|
|
|
|
|
logger.debug("Changed Information for User: {}", user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(value = "epasMedTransactionManager")
|
|
|
|
|
public void changePassword(String currentClearTextPassword, String newPassword) {
|
|
|
|
|
//logger.debug("CurrenClearTextPassword: {}", currentClearTextPassword);
|
|
|
|
|
SecurityUtils.getCurrentUserLogin().flatMap(userRepository::findOneByLogin).ifPresent(user -> {
|
|
|
|
|
String currentEncryptedPassword = user.getPassword();
|
|
|
|
|
//logger.debug("CurrentEncryptedPassword: {}", currentEncryptedPassword);
|
|
|
|
|
if (!passwordEncoder.matches(currentClearTextPassword, currentEncryptedPassword)) {
|
|
|
|
|
throw new InvalidPasswordException();
|
|
|
|
|
}
|
|
|
|
|
String encryptedPassword = passwordEncoder.encode(newPassword);
|
|
|
|
|
user.setPassword(encryptedPassword);
|
|
|
|
|
logger.debug("Changed password for User: {}", user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(value="epasMedTransactionManager")
|
|
|
|
|
public void changePassword(String currentClearTextPassword, String newPassword) {
|
|
|
|
|
SecurityUtils.getCurrentUserLogin()
|
|
|
|
|
.flatMap(userRepository::findOneByLogin)
|
|
|
|
|
.ifPresent(user -> {
|
|
|
|
|
String currentEncryptedPassword = user.getPassword();
|
|
|
|
|
if (!passwordEncoder.matches(currentClearTextPassword, currentEncryptedPassword)) {
|
|
|
|
|
throw new InvalidPasswordException();
|
|
|
|
|
}
|
|
|
|
|
String encryptedPassword = passwordEncoder.encode(newPassword);
|
|
|
|
|
user.setPassword(encryptedPassword);
|
|
|
|
|
log.debug("Changed password for User: {}", user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
@Transactional(value = "epasMedTransactionManager", readOnly = true)
|
|
|
|
|
public Page<UserDTO> getAllManagedUsers(Pageable pageable) {
|
|
|
|
|
return userRepository.findAllByLoginNot(pageable, Constants.ANONYMOUS_USER).map(UserDTO::new);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(value="epasMedTransactionManager",readOnly = true)
|
|
|
|
|
public Page<UserDTO> getAllManagedUsers(Pageable pageable) {
|
|
|
|
|
return userRepository.findAllByLoginNot(pageable, Constants.ANONYMOUS_USER).map(UserDTO::new);
|
|
|
|
|
}
|
|
|
|
|
@Transactional(value = "epasMedTransactionManager", readOnly = true)
|
|
|
|
|
public Optional<User> getUserWithAuthoritiesByLogin(String login) {
|
|
|
|
|
return userRepository.findOneWithAuthoritiesByLogin(login);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(value="epasMedTransactionManager",readOnly = true)
|
|
|
|
|
public Optional<User> getUserWithAuthoritiesByLogin(String login) {
|
|
|
|
|
return userRepository.findOneWithAuthoritiesByLogin(login);
|
|
|
|
|
}
|
|
|
|
|
@Transactional(value = "epasMedTransactionManager", readOnly = true)
|
|
|
|
|
public Optional<User> getUserWithAuthorities() {
|
|
|
|
|
return SecurityUtils.getCurrentUserLogin().flatMap(userRepository::findOneWithAuthoritiesByLogin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional(value="epasMedTransactionManager",readOnly = true)
|
|
|
|
|
public Optional<User> getUserWithAuthorities() {
|
|
|
|
|
return SecurityUtils.getCurrentUserLogin().flatMap(userRepository::findOneWithAuthoritiesByLogin);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Persistent Token are used for providing automatic authentication, they should
|
|
|
|
|
* be automatically deleted after 30 days.
|
|
|
|
|
* <p>
|
|
|
|
|
* This is scheduled to get fired everyday, at midnight.
|
|
|
|
|
*/
|
|
|
|
|
@Scheduled(cron = "0 0 0 * * ?")
|
|
|
|
|
public void removeOldPersistentTokens() {
|
|
|
|
|
LocalDate now = LocalDate.now();
|
|
|
|
|
persistentTokenRepository.findByTokenDateBefore(now.minusMonths(1)).forEach(token -> {
|
|
|
|
|
logger.debug("Deleting token {}", token.getSeries());
|
|
|
|
|
User user = token.getUser();
|
|
|
|
|
user.getPersistentTokens().remove(token);
|
|
|
|
|
persistentTokenRepository.delete(token);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Persistent Token are used for providing automatic authentication, they should be automatically deleted after
|
|
|
|
|
* 30 days.
|
|
|
|
|
* <p>
|
|
|
|
|
* This is scheduled to get fired everyday, at midnight.
|
|
|
|
|
*/
|
|
|
|
|
@Scheduled(cron = "0 0 0 * * ?")
|
|
|
|
|
public void removeOldPersistentTokens() {
|
|
|
|
|
LocalDate now = LocalDate.now();
|
|
|
|
|
persistentTokenRepository.findByTokenDateBefore(now.minusMonths(1)).forEach(token -> {
|
|
|
|
|
log.debug("Deleting token {}", token.getSeries());
|
|
|
|
|
User user = token.getUser();
|
|
|
|
|
user.getPersistentTokens().remove(token);
|
|
|
|
|
persistentTokenRepository.delete(token);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Not activated users should be automatically deleted after 3 days.
|
|
|
|
|
* <p>
|
|
|
|
|
* This is scheduled to get fired everyday, at 01:00 (am).
|
|
|
|
|
*/
|
|
|
|
|
@Scheduled(cron = "0 0 1 * * ?")
|
|
|
|
|
public void removeNotActivatedUsers() {
|
|
|
|
|
userRepository.findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(
|
|
|
|
|
Instant.now().minus(3, ChronoUnit.DAYS)).forEach(user -> {
|
|
|
|
|
logger.debug("Deleting not activated user {}", user.getLogin());
|
|
|
|
|
userRepository.delete(user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Not activated users should be automatically deleted after 3 days.
|
|
|
|
|
* <p>
|
|
|
|
|
* This is scheduled to get fired everyday, at 01:00 (am).
|
|
|
|
|
*/
|
|
|
|
|
@Scheduled(cron = "0 0 1 * * ?")
|
|
|
|
|
public void removeNotActivatedUsers() {
|
|
|
|
|
userRepository
|
|
|
|
|
.findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(Instant.now().minus(3, ChronoUnit.DAYS))
|
|
|
|
|
.forEach(user -> {
|
|
|
|
|
log.debug("Deleting not activated user {}", user.getLogin());
|
|
|
|
|
userRepository.delete(user);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a list of all the authorities.
|
|
|
|
|
* @return a list of all the authorities.
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(value="epasMedTransactionManager",readOnly = true)
|
|
|
|
|
public List<String> getAuthorities() {
|
|
|
|
|
return authorityRepository.findAll().stream().map(Authority::getName).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Gets a list of all the authorities.
|
|
|
|
|
*
|
|
|
|
|
* @return a list of all the authorities.
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(value = "epasMedTransactionManager", readOnly = true)
|
|
|
|
|
public List<String> getAuthorities() {
|
|
|
|
|
return authorityRepository.findAll().stream().map(Authority::getName).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|