Beispiel #1
0
  public User createUserInformation(
      String login,
      String password,
      String firstName,
      String lastName,
      String email,
      String langKey) {

    User newUser = new User();
    Authority authority = authorityRepository.findOne("ROLE_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.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);
    userSearchRepository.save(newUser);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
  }
 /**
  * Not activated users should be automatically deleted after 3 days.
  *
  * <p>
  *
  * <p>This is scheduled to get fired everyday, at 01:00 (am).
  */
 @Scheduled(cron = "0 0 1 * * ?")
 public void removeNotActivatedUsers() {
   DateTime now = new DateTime();
   List<User> users =
       userRepository.findAllByActivatedIsFalseAndCreatedDateBefore(now.minusDays(3));
   for (User user : users) {
     log.debug("Deleting not activated user {}", user.getLogin());
     userRepository.delete(user);
   }
 }
Beispiel #3
0
 /**
  * Persistent Token are used for providing automatic authentication, they should be automatically
  * deleted after 30 days.
  *
  * <p>
  *
  * <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))
       .stream()
       .forEach(
           token -> {
             log.debug("Deleting token {}", token.getSeries());
             User user = token.getUser();
             user.getPersistentTokens().remove(token);
             persistentTokenRepository.delete(token);
           });
 }
  @Test
  public void testGetExistingAccount() throws Exception {
    Set<Authority> authorities = new HashSet<>();
    Authority authority = new Authority();
    authority.setName(AuthoritiesConstants.ADMIN);
    authorities.add(authority);

    User user = new User();
    user.setLogin("test");
    user.setFirstName("john");
    user.setLastName("doe");
    user.setEmail("*****@*****.**");
    user.setAuthorities(authorities);
    when(mockUserService.getUserWithAuthorities()).thenReturn(user);

    restUserMockMvc
        .perform(get("/api/account").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.login").value("test"))
        .andExpect(jsonPath("$.firstName").value("john"))
        .andExpect(jsonPath("$.lastName").value("doe"))
        .andExpect(jsonPath("$.email").value("*****@*****.**"))
        .andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
  }
Beispiel #5
0
 @Transactional(readOnly = true)
 public User getUserWithAuthorities() {
   User user = userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin()).get();
   user.getAuthorities().size(); // eagerly load the association
   return user;
 }
Beispiel #6
0
 @Transactional(readOnly = true)
 public User getUserWithAuthorities(Long id) {
   User user = userRepository.findOne(id);
   user.getAuthorities().size(); // eagerly load the association
   return user;
 }
 public User getUserWithAuthorities(String id) {
   User user = userRepository.findOne(id);
   user.getAuthorities().size(); // eagerly load the association
   return user;
 }
 public User createUser(ManagedUserDTO managedUserDTO) {
   User user = new User();
   user.setLogin(managedUserDTO.getLogin());
   user.setFirstName(managedUserDTO.getFirstName());
   user.setLastName(managedUserDTO.getLastName());
   user.setEmail(managedUserDTO.getEmail());
   if (managedUserDTO.getLangKey() == null) {
     user.setLangKey("en"); // default language is English
   } else {
     user.setLangKey(managedUserDTO.getLangKey());
   }
   Set<Authority> authorities = new HashSet<>();
   managedUserDTO
       .getAuthorities()
       .stream()
       .forEach(authority -> authorities.add(authorityRepository.findOne(authority)));
   user.setAuthorities(authorities);
   String encryptedPassword = passwordEncoder.encode(RandomUtil.generatePassword());
   user.setPassword(encryptedPassword);
   user.setResetKey(RandomUtil.generateResetKey());
   user.setResetDate(ZonedDateTime.now());
   user.setActivated(true);
   userRepository.save(user);
   log.debug("Created Information for User: {}", user);
   return user;
 }