use of alfio.config.authentication.support.OpenIdAlfioUser in project alf.io by alfio-event.
the class BaseOpenIdAuthenticationManager method updateOrganizations.
private void updateOrganizations(OpenIdAlfioUser alfioUser) {
int userId = userRepository.findIdByUserName(alfioUser.getEmail()).orElseThrow();
var databaseOrganizationIds = organizationRepository.findAllForUser(alfioUser.getEmail()).stream().map(Organization::getId).collect(Collectors.toSet());
if (alfioUser.isAdmin()) {
if (!databaseOrganizationIds.isEmpty()) {
userOrganizationRepository.removeOrganizationUserLinks(userId, databaseOrganizationIds);
}
return;
}
List<Integer> organizationIds;
var userOrg = alfioUser.getAlfioOrganizationAuthorizations().keySet();
if (!userOrg.isEmpty()) {
organizationIds = organizationRepository.findOrganizationIdsByExternalId(userOrg);
} else {
organizationIds = List.of();
}
var organizationsToUnlink = databaseOrganizationIds.stream().filter(orgId -> !organizationIds.contains(orgId)).collect(Collectors.toSet());
if (!organizationsToUnlink.isEmpty()) {
userOrganizationRepository.removeOrganizationUserLinks(userId, organizationsToUnlink);
}
if (organizationIds.isEmpty()) {
throw new IllegalStateException("The user needs to be ADMIN or have at least one organization linked");
}
var params = organizationIds.stream().filter(orgId -> !databaseOrganizationIds.contains(orgId)).map(id -> new MapSqlParameterSource("userId", userId).addValue("organizationId", id)).toArray(MapSqlParameterSource[]::new);
jdbcTemplate.batchUpdate(userOrganizationRepository.bulkCreate(), params);
}
Aggregations