@Transactional
  @Override
  @CacheEvict(value = "usersByUsername", allEntries = true)
  public CommandProcessingResult createUser(final JsonCommand command) {

    try {
      this.context.authenticatedUser();

      this.fromApiJsonDeserializer.validateForCreate(command.json());

      final String officeIdParamName = "officeId";
      final Long officeId = command.longValueOfParameterNamed(officeIdParamName);

      final Office userOffice = this.officeRepository.findOne(officeId);
      if (userOffice == null) {
        throw new OfficeNotFoundException(officeId);
      }

      final String[] roles = command.arrayValueOfParameterNamed("roles");
      final Set<Role> allRoles = assembleSetOfRoles(roles);

      final AppUser appUser = AppUser.fromJson(userOffice, allRoles, command);
      final Boolean sendPasswordToEmail =
          command.booleanObjectValueOfParameterNamed("sendPasswordToEmail");
      this.userDomainService.create(appUser, sendPasswordToEmail);

      return new CommandProcessingResultBuilder() //
          .withCommandId(command.commandId()) //
          .withEntityId(appUser.getId()) //
          .withOfficeId(userOffice.getId()) //
          .build();
    } catch (final DataIntegrityViolationException dve) {
      handleDataIntegrityIssues(command, dve);
      return CommandProcessingResult.empty();
    } catch (final PlatformEmailSendException e) {
      final List<ApiParameterError> dataValidationErrors = new ArrayList<ApiParameterError>();

      final String email = command.stringValueOfParameterNamed("email");
      final ApiParameterError error =
          ApiParameterError.parameterError(
              "error.msg.user.email.invalid", "The parameter email is invalid.", "email", email);
      dataValidationErrors.add(error);

      throw new PlatformApiDataValidationException(
          "validation.msg.validation.errors.exist",
          "Validation errors exist.",
          dataValidationErrors);
    }
  }
예제 #2
0
  public Map<String, Object> update(
      final JsonCommand command, final PlatformPasswordEncoder platformPasswordEncoder) {

    final Map<String, Object> actualChanges = new LinkedHashMap<String, Object>(7);

    // unencoded password provided
    final String passwordParamName = "password";
    final String passwordEncodedParamName = "passwordEncoded";
    if (command.hasParameter(passwordParamName)) {
      if (command.isChangeInPasswordParameterNamed(
          passwordParamName, this.password, platformPasswordEncoder, this.getId())) {
        final String passwordEncodedValue =
            command.passwordValueOfParameterNamed(
                passwordParamName, platformPasswordEncoder, this.getId());
        actualChanges.put(passwordEncodedParamName, passwordEncodedValue);
        updatePassword(passwordEncodedValue);
      }
    }

    if (command.hasParameter(passwordEncodedParamName)) {
      if (command.isChangeInStringParameterNamed(passwordEncodedParamName, this.password)) {
        final String newValue = command.stringValueOfParameterNamed(passwordEncodedParamName);
        actualChanges.put(passwordEncodedParamName, newValue);
        updatePassword(newValue);
      }
    }

    final String officeIdParamName = "officeId";
    if (command.isChangeInLongParameterNamed(officeIdParamName, this.office.getId())) {
      final Long newValue = command.longValueOfParameterNamed(officeIdParamName);
      actualChanges.put(officeIdParamName, newValue);
    }

    final String rolesParamName = "roles";
    if (command.isChangeInArrayParameterNamed(rolesParamName, getRolesAsIdStringArray())) {
      final String[] newValue = command.arrayValueOfParameterNamed(rolesParamName);
      actualChanges.put(rolesParamName, newValue);
    }

    final String usernameParamName = "username";
    if (command.isChangeInStringParameterNamed(usernameParamName, this.username)) {
      final String newValue = command.stringValueOfParameterNamed(usernameParamName);
      actualChanges.put(usernameParamName, newValue);
      this.username = newValue;
    }

    final String firstnameParamName = "firstname";
    if (command.isChangeInStringParameterNamed(firstnameParamName, this.firstname)) {
      final String newValue = command.stringValueOfParameterNamed(firstnameParamName);
      actualChanges.put(firstnameParamName, newValue);
      this.firstname = newValue;
    }

    final String lastnameParamName = "lastname";
    if (command.isChangeInStringParameterNamed(lastnameParamName, this.lastname)) {
      final String newValue = command.stringValueOfParameterNamed(lastnameParamName);
      actualChanges.put(lastnameParamName, newValue);
      this.lastname = newValue;
    }

    final String emailParamName = "email";
    if (command.isChangeInStringParameterNamed(emailParamName, this.email)) {
      final String newValue = command.stringValueOfParameterNamed(emailParamName);
      actualChanges.put(emailParamName, newValue);
      this.email = newValue;
    }

    return actualChanges;
  }