private Long getUserId() { Long userId = null; SecurityContext context = SecurityContextHolder.getContext(); if (context.getAuthentication() != null) { AppUser appUser = this.context.authenticatedUser(); userId = appUser.getId(); } else { userId = new Long(0); } return userId; }
@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); } }