use of com.nexblocks.authguard.service.util.ID in project AuthGuard by AuthGuard.
the class IdempotencyServiceImpl method performOperation.
@Override
public <T extends Entity> CompletableFuture<T> performOperation(final Supplier<T> operation, final String idempotentKey, final String entityType) {
return findByKeyAndEntityType(idempotentKey, entityType).thenApplyAsync(record -> {
if (record.isPresent()) {
throw new IdempotencyException(record.get());
}
return operation.get();
}).thenApply(result -> {
final IdempotentRecordBO record = IdempotentRecordBO.builder().id(ID.generate()).entityId(result.getId()).entityType(result.getEntityType()).idempotentKey(idempotentKey).build();
// we don't have to wait for this to finish
CompletableFuture.runAsync(() -> create(record));
return result;
});
}
use of com.nexblocks.authguard.service.util.ID in project AuthGuard by AuthGuard.
the class CredentialsServiceImpl method removeIdentifiers.
@Override
public Optional<CredentialsBO> removeIdentifiers(final String id, final List<String> identifiers) {
final CredentialsBO existing = getByIdUnsafe(id).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.IDENTIFIER_DOES_NOT_EXIST, "No credentials with ID " + id));
final List<UserIdentifierBO> updatedIdentifiers = existing.getIdentifiers().stream().map(identifier -> {
if (identifiers.contains(identifier.getIdentifier())) {
return identifier.withActive(false);
}
return identifier;
}).collect(Collectors.toList());
final CredentialsBO updated = CredentialsBO.builder().from(existing).identifiers(updatedIdentifiers).build();
return doUpdate(existing, updated, false);
}
use of com.nexblocks.authguard.service.util.ID in project AuthGuard by AuthGuard.
the class CredentialsServiceImpl method replaceIdentifier.
@Override
public Optional<CredentialsBO> replaceIdentifier(final String id, final String oldIdentifier, final UserIdentifierBO newIdentifier) {
final CredentialsBO credentials = getByIdUnsafe(id).orElseThrow(() -> new ServiceNotFoundException(ErrorCode.IDENTIFIER_DOES_NOT_EXIST, "No credentials with ID " + id));
final boolean hasIdentifier = credentials.getIdentifiers().stream().anyMatch(identifier -> identifier.getIdentifier().equals(oldIdentifier));
if (!hasIdentifier) {
throw new ServiceException(ErrorCode.IDENTIFIER_DOES_NOT_EXIST, "Credentials " + id + " has no identifier " + oldIdentifier);
}
final Set<UserIdentifierBO> newIdentifiers = credentials.getIdentifiers().stream().map(identifier -> {
if (identifier.getIdentifier().equals(oldIdentifier)) {
return UserIdentifierBO.builder().identifier(newIdentifier.getIdentifier()).active(newIdentifier.isActive()).type(identifier.getType()).domain(identifier.getDomain()).build();
}
return identifier;
}).collect(Collectors.toSet());
final CredentialsBO update = credentials.withIdentifiers(newIdentifiers);
return doUpdate(credentials, update, false);
}
Aggregations