Skip to content

Commit

Permalink
Merge pull request #24 from FINTLabs/feature_republish_all_users
Browse files Browse the repository at this point in the history
FKS-986 add scheduled user publishing component
  • Loading branch information
berntjoh authored Oct 18, 2024
2 parents 67f8fad + 8113b6e commit b4a782d
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/main/java/no/fintlabs/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@ConfigurationPropertiesScan
@SpringBootApplication
@EnableScheduling
public class Application {

public static void main(String[] args) {
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/no/fintlabs/user/ResponseFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public ResponseEntity<Map<String, Object>> toResponseEntity(Page<SimpleUser> use
);
}

public ResponseEntity<Map<String,Object>> toResponseEntity(String message) {
return new ResponseEntity<>(
Map.of("message", message),
HttpStatus.OK
);
}

private Page<SimpleUser> toPage(List<SimpleUser> list, Pageable paging) {
int start = (int) paging.getOffset();
int end = Math.min((start + paging.getPageSize()), list.size());
Expand All @@ -58,6 +65,4 @@ private Page<SimpleUser> toPage(List<SimpleUser> list, Pageable paging) {
? new PageImpl<>(new ArrayList<>(), paging, list.size())
: new PageImpl<>(list.subList(start, end), paging, list.size());
}


}
16 changes: 8 additions & 8 deletions src/main/java/no/fintlabs/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ public ResponseEntity<LoggedOnUser> getLoggedOnUser(@AuthenticationPrincipal Jwt
}

@PostMapping("/republish")
public ResponseEntity<HttpStatus> republishKontrollUsers() { //@AuthenticationPrincipal Jwt jwt) {
public ResponseEntity<Map<String,Object>> republishKontrollUsers(@AuthenticationPrincipal Jwt jwt) {

//TODO: implement when agreed on security solution
/*if (!validateIsAdmin(jwt)) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "User does not have access to republish all kontrollusers");
}*/
String triggerType = "admin (" + FintJwtEndUserPrincipal.from(jwt).getMail() + ") request";
List<User> allUsers = userService.getAllUsers();
log.info("Republishing all {} kontrollusers", allUsers.size());

allUsers.stream()
.filter(user -> user.getIdentityProviderUserObjectId() != null)
.forEach(userEntityProducerService::publish);

log.info("Republishing all {} kontrollusers done", allUsers.size());
if (allUsers.isEmpty()) {
log.info("No users found to publish");
return responseFactory.toResponseEntity("No users found to publish");
}
int noOfPublishedUsers = userEntityProducerService.publishAllKontrollUsers(triggerType, allUsers);

return new ResponseEntity<>(HttpStatus.OK);
return responseFactory.toResponseEntity("Republished " + noOfPublishedUsers + " users");
}
}
16 changes: 14 additions & 2 deletions src/main/java/no/fintlabs/user/UserEntityProducerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
import no.fintlabs.kafka.entity.topic.EntityTopicService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserEntityProducerService {
private final EntityProducer<User> entityProducer;
private final EntityTopicNameParameters entityTopicNameParameters;

public UserEntityProducerService(EntityProducerFactory entityProducerFactory,
EntityTopicService entityTopicService) {
public UserEntityProducerService(
EntityProducerFactory entityProducerFactory,
EntityTopicService entityTopicService
) {
entityProducer = entityProducerFactory.createProducer(User.class);
entityTopicNameParameters = EntityTopicNameParameters
.builder()
Expand All @@ -34,4 +38,12 @@ public void publish(User user){
.build()
);
}

public int publishAllKontrollUsers(String triggerType, List<User> allUsers){
log.info("Republishing all {} kontrollusers triggered by {}", allUsers.size(), triggerType);

allUsers.forEach(this::publish);
log.info("Republishing all {} kontrollusers triggered by {} done", allUsers.size(), triggerType);
return allUsers.size();
}
}
35 changes: 35 additions & 0 deletions src/main/java/no/fintlabs/user/UserPublishingComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package no.fintlabs.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class UserPublishingComponent {
private final UserEntityProducerService userEntityProducerService;
private final UserService userService;

public UserPublishingComponent(UserEntityProducerService userEntityProducerService, UserService userService) {
this.userEntityProducerService = userEntityProducerService;
this.userService = userService;
}
@Scheduled(
initialDelayString = "${fint.kontroll.user-catalog.publishing.initial-delay}",
fixedDelayString = "${fint.kontroll.user-catalog.publishing.fixed-delay}",
timeUnit = TimeUnit.HOURS
)
public void publishUsers() {
String triggerType = "scheduled job";
List<User> allUsers = userService.getAllUsers();

if (allUsers.isEmpty()) {
log.info("No users to publish");
return;
}
userEntityProducerService.publishAllKontrollUsers(triggerType, allUsers);
}
}
13 changes: 8 additions & 5 deletions src/main/java/no/fintlabs/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public UserService(UserRepository userRepository, UserEntityProducerService user
}

public List<User> getAllUsers() {
return userRepository.findAll();
return userRepository.findAll()
.stream()
.filter(user -> user.getIdentityProviderUserObjectId() != null)
.toList();
}

public void save(User user) {
Expand All @@ -40,18 +43,18 @@ public void save(User user) {
private Runnable onSaveNewUser(User user) {
return () -> {
User newUser = userRepository.save(user);
log.info("Create new user: " + user.getId());
log.info("created kontrollUser: " + newUser.getIdentityProviderUserObjectId());
log.info("Create new user: {}", user.getId());
log.info("Created kontrollUser: {}", newUser.getIdentityProviderUserObjectId());
userEntityProducerService.publish(newUser);
};
}

private Consumer<User> onSaveExistingUser(User user) {
return existingUser -> {
user.setId(existingUser.getId());
log.debug("Update user: " + user.getId());
log.debug("Update user: {}", user.getId());
User savedUser = userRepository.save(user);
log.debug("update kontrollUser: " + savedUser.getIdentityProviderUserObjectId());
log.debug("update kontrollUser: {}", savedUser.getIdentityProviderUserObjectId());
userEntityProducerService.publish(savedUser);
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ fint:
kontroll:
user-catalog:
pagesize: 50
publishing:
initial-delay: 0
fixed-delay: 48
opa:
url: http://fint-kontroll-opa:8181/v1/data/accessmanagement
authorization:
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/no/fintlabs/user/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -103,8 +104,21 @@ void testGetDetailedUserById_shouldPermitAccess(){
DetailedUser foundDetailedUser = userService.getDetailedUserById(fintJwtEndUserPrincipal,1L);

assertEquals(requestedDetailedUser.getId(), foundDetailedUser.getId());
}
@Test
void testGetAllUsers_shouldReturnOnlyUsersWithNotNullIdpObjectId() {
User user1 = User.builder()
.id(1L)
.identityProviderUserObjectId(UUID.fromString("3f2b9b63-47e9-43c2-9d61-dd078d621479"))
.build();

User user2 = User.builder()
.id(2L)
.build();

when(userRepository.findAll()).thenReturn(List.of(user1,user2));

assertEquals(1, userService.getAllUsers().size());
assertEquals(user1, userService.getAllUsers().get(0));
}
}

0 comments on commit b4a782d

Please sign in to comment.