}
private void verifyPersonConsumer(Consumer consumer, ConsumerType type,
Owner owner, String username, Principal principal) {
User user = null;
try {
user = userService.findByLogin(username);
}
catch (UnsupportedOperationException e) {
log.warn("User service does not allow user lookups, " +
"cannot verify person consumer.");
}
if (user == null) {
throw new NotFoundException(
i18n.tr("User with ID ''{0}'' could not be found."));
}
// When registering person consumers we need to be sure the username
// has some association with the owner the consumer is destined for:
if (!principal.canAccess(owner, SubResource.NONE, Access.ALL) &&
!principal.hasFullAccess()) {
throw new ForbiddenException(i18n.tr(
"User ''{0}'' has no roles for organization ''{1}''",
user.getUsername(), owner.getKey()));
}
// TODO: Refactor out type specific checks?
if (type.isType(ConsumerTypeEnum.PERSON)) {
Consumer existing = consumerCurator.findByUser(user);
if (existing != null &&
existing.getType().isType(ConsumerTypeEnum.PERSON)) {
// TODO: This is not the correct error code for this situation!
throw new BadRequestException(i18n.tr(
"User ''{0}'' has already registered a personal consumer",
user.getUsername()));
}
consumer.setName(user.getUsername());
}
}