Skip to content

Commit

Permalink
Merge pull request #17 from FINTLabs/FFS-1140-lage-producer-i-authori…
Browse files Browse the repository at this point in the history
…zation-service-som-publiserer-source-application-ids-per-user-pa-entity-topic

Ffs 1140 lage producer i authorization service som publiserer source application ids per user pa entity topic
  • Loading branch information
Battlestad authored Jul 5, 2024
2 parents b56008d + ead80e3 commit 583ae96
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void syncUsers() {

log.info("Syncing users");
updateUsers(graphService.getPermittedUsersInfo());
userService.publishUsers();
log.info("Successfully synced users");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package no.fintlabs.flyt.authorization.user;

import lombok.extern.slf4j.Slf4j;
import no.fintlabs.flyt.authorization.user.kafka.UserPermission;
import no.fintlabs.flyt.authorization.user.kafka.UserPermissionEntityProducerService;
import no.fintlabs.flyt.authorization.user.model.User;
import no.fintlabs.flyt.authorization.user.model.UserEntity;
import org.springframework.data.domain.Page;
Expand All @@ -22,9 +24,14 @@ public class UserService {
// ettersom horisontal skalering av synkingen vil skape problemer

private final UserRepository userRepository;
private final UserPermissionEntityProducerService userPermissionEntityProducerService;

public UserService(UserRepository userRepository) {
public UserService(
UserRepository userRepository,
UserPermissionEntityProducerService userPermissionEntityProducerService
) {
this.userRepository = userRepository;
this.userPermissionEntityProducerService = userPermissionEntityProducerService;
}

@Transactional
Expand Down Expand Up @@ -75,6 +82,15 @@ public void updateUsers(Collection<User> users) {
log.info("Successfully updated user entities");
}

public void publishUsers() {
this.userRepository
.findAll()
.stream()
.map(this::mapFromEntityToUserPermission)
.toList()
.forEach(userPermissionEntityProducerService::send);
}

public Optional<User> find(UUID objectIdentifier) {
return this.userRepository.findByObjectIdentifier(objectIdentifier)
.map(this::mapFromEntity);
Expand Down Expand Up @@ -116,4 +132,12 @@ private User mapFromEntity(UserEntity userEntity) {
.build();
}

private UserPermission mapFromEntityToUserPermission(UserEntity userEntity) {
return UserPermission
.builder()
.objectIdentifier(userEntity.getObjectIdentifier())
.sourceApplicationIds(userEntity.getSourceApplicationIds())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package no.fintlabs.flyt.authorization.user.kafka;

import lombok.Builder;
import lombok.Getter;

import java.util.List;
import java.util.UUID;

@Getter
@Builder
public class UserPermission {
private UUID objectIdentifier;
private List<Long> sourceApplicationIds;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package no.fintlabs.flyt.authorization.user.kafka;

import no.fintlabs.kafka.entity.EntityProducer;
import no.fintlabs.kafka.entity.EntityProducerFactory;
import no.fintlabs.kafka.entity.EntityProducerRecord;
import no.fintlabs.kafka.entity.topic.EntityTopicNameParameters;
import no.fintlabs.kafka.entity.topic.EntityTopicService;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserPermissionEntityProducerService {

private final EntityProducer<UserPermission> entityProducer;
private final EntityTopicNameParameters entityTopicNameParameters;

public UserPermissionEntityProducerService(
EntityProducerFactory entityProducerFactory,
EntityTopicService entityTopicService
) {
this.entityProducer = entityProducerFactory.createProducer(UserPermission.class);
entityTopicNameParameters = EntityTopicNameParameters
.builder()
.resource("userpermission")
.build();
int retentionTimeInHours = 1;
long retentionTimeInMilliseconds = TimeUnit.HOURS.toMillis(retentionTimeInHours);
entityTopicService.ensureTopic(entityTopicNameParameters, retentionTimeInMilliseconds);
}

public void send(UserPermission userPermission) {
entityProducer.send(
EntityProducerRecord.<UserPermission>builder()
.topicNameParameters(entityTopicNameParameters)
.key(String.valueOf(userPermission.getObjectIdentifier()))
.value(userPermission)
.build()
);
}

}

0 comments on commit 583ae96

Please sign in to comment.