Beispiel #1
0
 public void deleteUserInformation(String login) {
   userRepository
       .findOneByLogin(login)
       .ifPresent(
           u -> {
             userRepository.delete(u);
             log.debug("Deleted User: {}", u);
           });
 }
Beispiel #2
0
 /**
  * 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() {
   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);
   }
 }
Beispiel #3
0
 public void changePassword(String password) {
   userRepository
       .findOneByLogin(SecurityUtils.getCurrentUser().getUsername())
       .ifPresent(
           u -> {
             String encryptedPassword = passwordEncoder.encode(password);
             u.setPassword(encryptedPassword);
             userRepository.save(u);
             log.debug("Changed password for User: {}", u);
           });
 }
Beispiel #4
0
 public Optional<User> requestPasswordReset(String mail) {
   return userRepository
       .findOneByEmail(mail)
       .filter(User::getActivated)
       .map(
           user -> {
             user.setResetKey(RandomUtil.generateResetKey());
             user.setResetDate(ZonedDateTime.now());
             userRepository.save(user);
             return user;
           });
 }
Beispiel #5
0
 public void updateUserInformation(
     String firstName, String lastName, String email, String langKey) {
   userRepository
       .findOneByLogin(SecurityUtils.getCurrentUser().getUsername())
       .ifPresent(
           u -> {
             u.setFirstName(firstName);
             u.setLastName(lastName);
             u.setEmail(email);
             u.setLangKey(langKey);
             userRepository.save(u);
             log.debug("Changed Information for User: {}", u);
           });
 }
Beispiel #6
0
 public Optional<User> activateRegistration(String key) {
   log.debug("Activating user for activation key {}", key);
   userRepository
       .findOneByActivationKey(key)
       .map(
           user -> {
             // activate given user for the registration key.
             user.setActivated(true);
             user.setActivationKey(null);
             userRepository.save(user);
             log.debug("Activated user: {}", user);
             return user;
           });
   return Optional.empty();
 }
Beispiel #7
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);
    log.debug("Created Information for User: {}", newUser);
    return newUser;
  }
Beispiel #8
0
 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());
   }
   if (managedUserDTO.getAuthorities() != null) {
     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;
 }
Beispiel #9
0
  public Optional<User> completePasswordReset(String newPassword, String key) {
    log.debug("Reset user password for reset key {}", key);

    return userRepository
        .findOneByResetKey(key)
        .filter(
            user -> {
              ZonedDateTime oneDayAgo = ZonedDateTime.now().minusHours(24);
              return user.getResetDate().isAfter(oneDayAgo);
            })
        .map(
            user -> {
              user.setPassword(passwordEncoder.encode(newPassword));
              user.setResetKey(null);
              user.setResetDate(null);
              userRepository.save(user);
              return user;
            });
  }
Beispiel #10
0
 @Transactional(readOnly = true)
 public Optional<User> getUserWithAuthoritiesByLogin(String login) {
   return userRepository
       .findOneByLogin(login)
       .map(
           u -> {
             u.getAuthorities().size();
             return u;
           });
 }
Beispiel #11
0
 @Transactional(readOnly = true)
 public User getUserWithAuthorities() {
   User user = userRepository.findOneByLogin(SecurityUtils.getCurrentUser().getUsername()).get();
   user.getAuthorities().size(); // eagerly load the association
   return user;
 }
Beispiel #12
0
 @Transactional(readOnly = true)
 public User getUserWithAuthorities(Long id) {
   User user = userRepository.findOne(id);
   user.getAuthorities().size(); // eagerly load the association
   return user;
 }