22
33import static java .util .Optional .ofNullable ;
44import static org .springframework .util .StringUtils .hasLength ;
5+ import static org .springframework .util .StringUtils .hasText ;
56
7+ import io .openaev .database .model .Tenant ;
68import io .openaev .database .model .User ;
79import io .openaev .database .repository .UserRepository ;
810import io .openaev .service .UserMappingService ;
911import io .openaev .service .UserService ;
12+ import io .openaev .service .tenants .TenantService ;
1013import io .openaev .service .user_events .UserEventService ;
14+ import jakarta .persistence .EntityNotFoundException ;
1115import jakarta .validation .constraints .NotBlank ;
1216import java .util .ArrayList ;
1317import java .util .List ;
1418import java .util .Optional ;
1519import java .util .UUID ;
1620import lombok .RequiredArgsConstructor ;
21+ import lombok .extern .slf4j .Slf4j ;
1722import org .springframework .core .env .Environment ;
1823import org .springframework .stereotype .Service ;
1924
25+ @ Slf4j
2026@ Service
2127@ RequiredArgsConstructor
2228public class SecurityService {
@@ -26,13 +32,15 @@ public class SecurityService {
2632 public static final String GROUPS_MANAGEMENT_SUFFIX = ".groups_management" ;
2733 public static final String ALL_ADMIN_PATH_SUFFIX = ".all_admin" ;
2834 public static final String AUDIENCE_PATH = ".audience" ;
35+ public static final String TENANT_ID_SUFFIX = ".tenant_id" ;
2936 public static final String REGISTRATION_ID = "registration_id" ;
3037
3138 private final UserRepository userRepository ;
3239 private final UserService userService ;
3340 private final UserMappingService userMappingService ;
3441 private final Environment env ;
3542 private final UserEventService userEventService ;
43+ private final TenantService tenantService ;
3644
3745 public User userManagement (
3846 String emailAttribute ,
@@ -60,6 +68,7 @@ public User userManagement(
6068 String .class ,
6169 "" );
6270 userMappingService .mapCurrentUserWithGroup (groupsManagementObject , user , groups );
71+ attachTenant (registrationId , user );
6372 return this .userService .saveUser (user );
6473 } else {
6574 // If user exists, update it
@@ -76,6 +85,7 @@ public User userManagement(
7685 String .class ,
7786 "" );
7887 userMappingService .mapCurrentUserWithGroup (groupsManagementObject , currentUser , groups );
88+ attachTenant (registrationId , currentUser );
7989 return this .userService .saveUser (currentUser );
8090 }
8191 }
@@ -91,6 +101,26 @@ public String getAudience(@NotBlank final String registrationId) {
91101
92102 // -- PRIVATE --
93103
104+ /** Attaches the user to the tenant configured for the given SSO provider registration. */
105+ private void attachTenant (String registrationId , User user ) {
106+ String tenantId =
107+ env .getProperty (
108+ OPENAEV_PROVIDER_PATH_PREFIX + registrationId + TENANT_ID_SUFFIX , String .class , "" );
109+ if (!hasText (tenantId )) {
110+ return ;
111+ }
112+ boolean alreadyAttached = user .getTenants ().stream ().anyMatch (t -> t .getId ().equals (tenantId ));
113+ if (alreadyAttached ) {
114+ return ;
115+ }
116+ try {
117+ Tenant tenant = tenantService .findById (tenantId );
118+ user .getTenants ().add (tenant );
119+ } catch (EntityNotFoundException e ) {
120+ log .warn ("SSO tenant ID '{}' configured but not found in database" , tenantId );
121+ }
122+ }
123+
94124 private List <String > getAdminRoles (@ NotBlank final String registrationId ) {
95125 String rolesAdminConfig =
96126 OPENAEV_PROVIDER_PATH_PREFIX + registrationId + ROLES_ADMIN_PATH_SUFFIX ;
0 commit comments