Ejemplo n.º 1
0
  @CacheEvict(value = "users", allEntries = true)
  public List<UserInvitation> inviteUsers(
      UserInvitationCreateRequest form, BindingResult result, AuthorizedUser authorizedUser)
      throws MessagingException {
    String[] recipients = StringUtils.commaDelimitedListToStringArray(form.getInvitees());

    LocalDateTime now = LocalDateTime.now();

    List<UserInvitation> invitations = new ArrayList<>();
    for (String recipient : recipients) {
      UserInvitation invitation = new UserInvitation();
      invitation.setEmail(recipient);
      invitation.setMessage(form.getMessage());
      invitation.setExpiredAt(now.plusHours(72));
      invitation.setCreatedAt(now);
      invitation.setCreatedBy(authorizedUser.toString());
      invitation.setUpdatedAt(now);
      invitation.setUpdatedBy(authorizedUser.toString());
      invitation = userInvitationRepository.saveAndFlush(invitation);
      invitations.add(invitation);
    }

    Blog blog = blogService.readBlogById(Blog.DEFAULT_ID);
    for (UserInvitation invitation : invitations) {
      String websiteTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage());
      String signupLink =
          ServletUriComponentsBuilder.fromCurrentContextPath()
              .path("/_admin/signup")
              .queryParam("token", invitation.getToken())
              .buildAndExpand()
              .toString();

      final Context ctx = new Context(LocaleContextHolder.getLocale());
      ctx.setVariable("websiteTitle", websiteTitle);
      ctx.setVariable("authorizedUser", authorizedUser);
      ctx.setVariable("signupLink", signupLink);
      ctx.setVariable("invitation", invitation);

      final MimeMessage mimeMessage = mailSender.createMimeMessage();
      final MimeMessageHelper message =
          new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart
      message.setSubject(
          MessageFormat.format(
              messageSourceAccessor.getMessage(
                  "InvitationMessageTitle", LocaleContextHolder.getLocale()),
              authorizedUser.toString(),
              websiteTitle));
      message.setFrom(authorizedUser.getEmail());
      message.setTo(invitation.getEmail());

      final String htmlContent = templateEngine.process("user-invite", ctx);
      message.setText(htmlContent, true); // true = isHtml

      mailSender.send(mimeMessage);
    }

    return invitations;
  }
Ejemplo n.º 2
0
  @CacheEvict(value = "users", allEntries = true)
  public User updateProfile(ProfileUpdateRequest request, AuthorizedUser updatedBy) {
    User user = userRepository.findByIdForUpdate(request.getUserId());
    if (user == null) {
      throw new IllegalArgumentException("The user does not exist");
    }

    User duplicate;
    if (!ObjectUtils.nullSafeEquals(request.getEmail(), user.getEmail())) {
      duplicate = userRepository.findByEmail(request.getEmail());
      if (duplicate != null) {
        throw new DuplicateEmailException(request.getEmail());
      }
    }
    if (!ObjectUtils.nullSafeEquals(request.getLoginId(), user.getLoginId())) {
      duplicate = userRepository.findByLoginId(request.getLoginId());
      if (duplicate != null) {
        throw new DuplicateLoginIdException(request.getLoginId());
      }
    }

    user.setEmail(request.getEmail());
    user.setLoginId(request.getLoginId());
    user.setName(request.getName());
    user.setUpdatedAt(LocalDateTime.now());
    user.setUpdatedBy(updatedBy.toString());
    return userRepository.saveAndFlush(user);
  }
Ejemplo n.º 3
0
 @CacheEvict(value = "users", allEntries = true)
 public User updatePassword(PasswordUpdateRequest request, AuthorizedUser updatedBy) {
   User user = userRepository.findByIdForUpdate(request.getUserId());
   if (user == null) {
     throw new IllegalArgumentException("The user does not exist");
   }
   PasswordEncoder passwordEncoder = new StandardPasswordEncoder();
   user.setLoginPassword(passwordEncoder.encode(request.getPassword()));
   user.setUpdatedAt(LocalDateTime.now());
   user.setUpdatedBy(updatedBy.toString());
   return userRepository.saveAndFlush(user);
 }