@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); } }
public static AppUser fromJson( final Office userOffice, final Set<Role> allRoles, final JsonCommand command) { final String username = command.stringValueOfParameterNamed("username"); String password = command.stringValueOfParameterNamed("password"); final Boolean sendPasswordToEmail = command.booleanObjectValueOfParameterNamed("sendPasswordToEmail"); if (sendPasswordToEmail.booleanValue()) { password = new RandomPasswordGenerator(13).generate(); } final boolean userEnabled = true; final boolean userAccountNonExpired = true; final boolean userCredentialsNonExpired = true; final boolean userAccountNonLocked = true; final Collection<SimpleGrantedAuthority> authorities = new ArrayList<SimpleGrantedAuthority>(); authorities.add( new SimpleGrantedAuthority("DUMMY_ROLE_NOT_USED_OR_PERSISTED_TO_AVOID_EXCEPTION")); final User user = new User( username, password, userEnabled, userAccountNonExpired, userCredentialsNonExpired, userAccountNonLocked, authorities); final String email = command.stringValueOfParameterNamed("email"); final String firstname = command.stringValueOfParameterNamed("firstname"); final String lastname = command.stringValueOfParameterNamed("lastname"); return new AppUser(userOffice, user, allRoles, email, firstname, lastname); }