Search in sources :

Example 1 with OpenIdAlfioUser

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);
}
Also used : JWT(com.auth0.jwt.JWT) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) java.util(java.util) HttpUtils(alfio.util.HttpUtils) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) OpenIdAlfioAuthentication(alfio.config.authentication.support.OpenIdAlfioAuthentication) StringUtils(org.apache.commons.lang3.StringUtils) HttpRequest(java.net.http.HttpRequest) APPLICATION_FORM_URLENCODED(alfio.util.HttpUtils.APPLICATION_FORM_URLENCODED) Json(alfio.util.Json) HttpClient(java.net.http.HttpClient) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Claim(com.auth0.jwt.interfaces.Claim) AuthorityRepository(alfio.repository.user.AuthorityRepository) HttpResponse(java.net.http.HttpResponse) HttpSession(javax.servlet.http.HttpSession) APPLICATION_JSON(alfio.util.HttpUtils.APPLICATION_JSON) OrganizationRepository(alfio.repository.user.OrganizationRepository) PasswordGenerator(alfio.util.PasswordGenerator) UserOrganizationRepository(alfio.repository.user.join.UserOrganizationRepository) Organization(alfio.model.user.Organization) User(alfio.model.user.User) OpenIdAlfioUser(alfio.config.authentication.support.OpenIdAlfioUser) Collectors(java.util.stream.Collectors) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Role(alfio.model.user.Role) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) UserRepository(alfio.repository.user.UserRepository) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) UriComponents(org.springframework.web.util.UriComponents) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Aggregations

OpenIdAlfioAuthentication (alfio.config.authentication.support.OpenIdAlfioAuthentication)1 OpenIdAlfioUser (alfio.config.authentication.support.OpenIdAlfioUser)1 UserManager (alfio.manager.user.UserManager)1 Organization (alfio.model.user.Organization)1 Role (alfio.model.user.Role)1 User (alfio.model.user.User)1 AuthorityRepository (alfio.repository.user.AuthorityRepository)1 OrganizationRepository (alfio.repository.user.OrganizationRepository)1 UserRepository (alfio.repository.user.UserRepository)1 UserOrganizationRepository (alfio.repository.user.join.UserOrganizationRepository)1 HttpUtils (alfio.util.HttpUtils)1 APPLICATION_FORM_URLENCODED (alfio.util.HttpUtils.APPLICATION_FORM_URLENCODED)1 APPLICATION_JSON (alfio.util.HttpUtils.APPLICATION_JSON)1 Json (alfio.util.Json)1 PasswordGenerator (alfio.util.PasswordGenerator)1 JWT (com.auth0.jwt.JWT)1 Claim (com.auth0.jwt.interfaces.Claim)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 URI (java.net.URI)1 HttpClient (java.net.http.HttpClient)1