Ejemplo n.º 1
0
  @Override
  @Transactional(readOnly = false)
  public CommandProcessingResult updatePassword(Long userId, final JsonCommand command) {
    User user = this.userRepository.findOne(userId);

    if (user == null) {
      throw new ResourceNotFoundException(
          "error.entity.user.not.found", "User with id " + userId + " not found", userId);
    }

    if (this.context.authenticatedUser().isPublicUser()
        || command.parameterExists(OldPasswordParamName)) {
      // validate old password
      String oldPassword = command.stringValueOfParameterNamed(OldPasswordParamName);
      if (!user.getPassword().equals(this.applicationPasswordEncoder.encode(oldPassword, user))) {
        throw new GeneralPlatformRuleException(
            "error.old.password.invalid", "Old Password is Incorrect");
      }
    }
    user.updatePasswordFromCommand(command);
    generateKeyUsedForPasswordSalting(user);
    final String encodePassword = this.applicationPasswordEncoder.encode(user);
    user.updatePassword(encodePassword);

    this.userRepository.saveAndFlush(user);

    return new CommandProcessingResultBuilder().withResourceIdAsString(user.getId()).build();
  }
Ejemplo n.º 2
0
  // public functions
  @Override
  @Transactional
  public CommandProcessingResult createPublicUser(final JsonCommand command) {
    try {
      this.userDataValidator.validateCreate(command.getJsonCommand());

      User user = User.fromJson(command, false, true);

      generateKeyUsedForPasswordSalting(user);
      final String encodePassword = this.applicationPasswordEncoder.encode(user);
      user.updatePassword(encodePassword);

      this.userRepository.save(user);

      final JsonElement element = this.fromJsonHelper.parse(command.getJsonCommand());
      final String returnUrl = this.fromJsonHelper.extractStringNamed(ReturnUrlParamName, element);

      final String email = user.getUsername();
      final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      final String nowDate = sf.format(DateTime.now().toDate());
      final String text = nowDate + email + nowDate + Math.random();

      final String otp = new String(Base64.encode(text.getBytes()));
      final UserOtp userOtp =
          UserOtp.createOtp(user, email, otp.substring(3, otp.length() - 5), returnUrl);

      this.userOtpRepository.save(userOtp);

      final String finalOtp = userOtp.getOtp();
      final String verificationLink =
          DefaultAppUrl + "userapi/activate" + "?e=" + email + "&uas=" + finalOtp;
      String toEmails[] = new String[] {email};
      this.emailSenderService.sendEmail(
          toEmails,
          null,
          null,
          EmailTemplates.activateUserEmailSubject(),
          EmailTemplates.activateUserEmailTemplate(user.getName(), verificationLink));
      return new CommandProcessingResultBuilder().withSuccessStatus().build();
    } catch (DataIntegrityViolationException ex) {
      ex.printStackTrace();
      final Throwable realCause = ex.getCause();
      if (realCause.getMessage().toLowerCase().contains("email")) {
        throw new PlatformDataIntegrityException(
            "error.msg.email.already.exist",
            "The email provided already exitst in the system." + realCause.getMessage());
      }
      throw new PlatformDataIntegrityException(
          "error.msg.unknown.data.integrity.issue",
          "Unknown data integrity issue with resource: " + realCause.getMessage());
    }
  }
Ejemplo n.º 3
0
  @Override
  @Transactional
  public CommandProcessingResult create(final JsonCommand command) {

    this.userDataValidator.validateCreate(command.getJsonCommand());

    User user = User.fromJson(command, true, false);

    generateKeyUsedForPasswordSalting(user);
    final String encodePassword = this.applicationPasswordEncoder.encode(user);
    user.updatePassword(encodePassword);

    this.userRepository.saveAndFlush(user);

    return new CommandProcessingResultBuilder().withResourceIdAsString(user.getId()).build();
  }