@@ -819,11 +819,13 @@ public AuthenticationResponse login(String organizationId, String username, Stri
819819 }
820820
821821 OpenCGAResult <User > userOpenCGAResult = getUserDBAdaptor (organizationId ).get (username , INCLUDE_INTERNAL );
822+ User user = null ;
822823 if (userOpenCGAResult .getNumResults () == 1 ) {
823- User user = userOpenCGAResult .first ();
824+ user = userOpenCGAResult .first ();
825+ authId = user .getInternal ().getAccount ().getAuthentication ().getId ();
824826 // Only local OPENCGA users that are not superadmins can be automatically banned or their accounts be expired
825827 boolean userCanBeBanned = !ParamConstants .ADMIN_ORGANIZATION .equals (organizationId )
826- && CatalogAuthenticationManager .OPENCGA .equals (user . getInternal (). getAccount (). getAuthentication (). getId () );
828+ && CatalogAuthenticationManager .OPENCGA .equals (authId );
827829 // We check
828830 if (userCanBeBanned ) {
829831 // Check user is not banned, suspended or has an expired account
@@ -856,8 +858,6 @@ public AuthenticationResponse login(String organizationId, String username, Stri
856858 }
857859 }
858860 }
859- User user1 = userOpenCGAResult .first ();
860- authId = user1 .getInternal ().getAccount ().getAuthentication ().getId ();
861861 try {
862862 response = authenticationFactory .authenticate (organizationId , authId , username , password );
863863 } catch (CatalogAuthenticationException e ) {
@@ -901,6 +901,17 @@ public AuthenticationResponse login(String organizationId, String username, Stri
901901 logger .debug ("Attempted authentication failed with {} for user '{}'\n {}" , entry .getKey (), username , e .getMessage (), e );
902902 }
903903 }
904+
905+ if (response != null && !CatalogAuthenticationManager .OPENCGA .equals (authId )
906+ && !CatalogAuthenticationManager .INTERNAL .equals (authId )) {
907+ // The user does not exist so we register it
908+ user = authenticationFactory .getRemoteUserInformation (organizationId , authId , Collections .singletonList (username ))
909+ .get (0 );
910+ user .setOrganization (organizationId );
911+ // Generate a root token to be able to create the user even if the installation is private
912+ String rootToken = authenticationFactory .createToken (organizationId , CatalogAuthenticationManager .OPENCGA , OPENCGA );
913+ create (user , null , rootToken );
914+ }
904915 }
905916
906917 if (response == null ) {
@@ -911,29 +922,14 @@ public AuthenticationResponse login(String organizationId, String username, Stri
911922
912923 auditManager .auditUser (organizationId , username , Enums .Action .LOGIN , username ,
913924 new AuditRecord .Status (AuditRecord .Status .Result .SUCCESS ));
914- String userId = authenticationFactory .getUserId (organizationId , authId , response .getToken ());
915925 if (!CatalogAuthenticationManager .OPENCGA .equals (authId ) && !CatalogAuthenticationManager .INTERNAL .equals (authId )) {
916- // External authorization
917- try {
918- // If the user is not registered, an exception will be raised
919- getUserDBAdaptor (organizationId ).checkId (userId );
920- } catch (CatalogDBException e ) {
921- // The user does not exist so we register it
922- User user = authenticationFactory .getRemoteUserInformation (organizationId , authId , Collections .singletonList (userId ))
923- .get (0 );
924- user .setOrganization (organizationId );
925- // Generate a root token to be able to create the user even if the installation is private
926- String rootToken = authenticationFactory .createToken (organizationId , CatalogAuthenticationManager .OPENCGA , OPENCGA );
927- create (user , null , rootToken );
928- }
929-
930926 try {
931927 List <String > remoteGroups = authenticationFactory .getRemoteGroups (organizationId , authId , response .getToken ());
932928
933929 // Resync synced groups of user in OpenCGA
934- getStudyDBAdaptor (organizationId ).resyncUserWithSyncedGroups (userId , remoteGroups , authId );
930+ getStudyDBAdaptor (organizationId ).resyncUserWithSyncedGroups (user . getId () , remoteGroups , authId );
935931 } catch (CatalogException e ) {
936- logger .error ("Could not update synced groups for user '" + userId + "' \n " + e .getMessage (), e );
932+ logger .error ("Could not update synced groups for user '{}': {}" , user . getId (), e .getMessage (), e );
937933 }
938934 }
939935
0 commit comments