Search in sources :

Example 31 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserService method removeNotActivatedUsers.

/**
 * Not activated users should be automatically deleted after 3 days.
 * <p>
 * This is scheduled to get fired everyday, at 01:00 (am).
 * </p>
 */
@Scheduled(cron = "0 0 1 * * ?")
public void removeNotActivatedUsers() {
    ZonedDateTime now = ZonedDateTime.now();
    List<User> users = userRepository.findAllByActivatedIsFalseAndCreatedDateBefore(now.minusDays(3));
    for (User user : users) {
        log.debug("Deleting not activated user {}", user.getLogin());
        userRepository.delete(user);
    }
}
Also used : User(com.baeldung.domain.User) ZonedDateTime(java.time.ZonedDateTime) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 32 with User

use of com.baeldung.domain.User in project tutorials by eugenp.

the class UserService method createUser.

public User createUser(String login, String password, String firstName, String lastName, String email, String imageUrl, String langKey) {
    User newUser = new User();
    Authority authority = authorityRepository.findOne(AuthoritiesConstants.USER);
    Set<Authority> authorities = new HashSet<>();
    String encryptedPassword = passwordEncoder.encode(password);
    newUser.setLogin(login);
    // new user gets initially a generated password
    newUser.setPassword(encryptedPassword);
    newUser.setFirstName(firstName);
    newUser.setLastName(lastName);
    newUser.setEmail(email);
    newUser.setImageUrl(imageUrl);
    newUser.setLangKey(langKey);
    // new user is not active
    newUser.setActivated(false);
    // new user gets registration key
    newUser.setActivationKey(RandomUtil.generateActivationKey());
    authorities.add(authority);
    newUser.setAuthorities(authorities);
    userRepository.save(newUser);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
}
Also used : User(com.baeldung.domain.User) Authority(com.baeldung.domain.Authority)

Aggregations

User (com.baeldung.domain.User)32 Test (org.junit.Test)23 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)23 Transactional (org.springframework.transaction.annotation.Transactional)18 ManagedUserVM (com.baeldung.web.rest.vm.ManagedUserVM)16 HashSet (java.util.HashSet)8 ZonedDateTime (java.time.ZonedDateTime)5 Authority (com.baeldung.domain.Authority)3 UserRepository (com.baeldung.repository.UserRepository)2 UserDTO (com.baeldung.service.dto.UserDTO)2 Timed (com.codahale.metrics.annotation.Timed)2 java.util (java.util)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Post (com.baeldung.domain.Post)1 SecurityUtils (com.baeldung.security.SecurityUtils)1 MailService (com.baeldung.service.MailService)1 UserService (com.baeldung.service.UserService)1 HeaderUtil (com.baeldung.web.rest.util.HeaderUtil)1 KeyAndPasswordVM (com.baeldung.web.rest.vm.KeyAndPasswordVM)1