@@ -189,6 +189,8 @@ public List<SideEffect> initialize(InstallConfiguration installConfiguration, bo
189189
190190 // first we create the admin user, then the user types followed by all users
191191 sideEffects .addAll (createAdminUser (installConfiguration , action ));
192+ sideEffects .addAll (createSystemAdmin (installConfiguration , action ));
193+
192194
193195 createSystemUser (installConfiguration );
194196
@@ -681,16 +683,16 @@ protected List<User> createUsers(
681683 }
682684 }
683685 if (action ){
684- user = userService .getUser (user .getId ());
686+ var newUser = userService .getUser (user .getId ());
685687 var definition = userDTO .getAtlpDefinition ();
686688 if (null != definition && !definition .isEmpty ()) {
687689 Optional <ATPLPolicyEntity > policy = policyList .stream ()
688690 .filter (p -> p .getPolicyId ().equals (definition ))
689691 .findFirst ();
690- if (policy .isPresent ()) {
691- atplPolicyService .assignPolicyToUser (user , policy .get ());
692+ if (policy .isPresent () & newUser . isPresent () ) {
693+ atplPolicyService .assignPolicyToUser (newUser . get () , policy .get ());
692694 } else {
693- log .warn ("No ATPL policy found for user {} with policy id {}" , user .getUsername (),
695+ log .warn ("No ATPL policy found for user {} with policy id {}" , newUser . get () .getUsername (),
694696 definition );
695697 }
696698 }
@@ -809,16 +811,16 @@ protected List<User> createNPEs(
809811 }
810812 }
811813 if (action ){
812- user = userService .getUser (user .getId ());
814+ var newUser = userService .getUser (user .getId ());
813815 var definition = userDTO .getAtlpDefinition ();
814816 if (null != definition && !definition .isEmpty ()) {
815817 Optional <ATPLPolicyEntity > policy = policyList .stream ()
816818 .filter (p -> p .getPolicyId ().equals (definition ))
817819 .findFirst ();
818820 if (policy .isPresent ()) {
819- atplPolicyService .assignPolicyToUser (user , policy .get ());
821+ atplPolicyService .assignPolicyToUser (newUser . get () , policy .get ());
820822 } else {
821- log .warn ("No ATPL policy found for user {} with policy id {}" , user .getUsername (),
823+ log .warn ("No ATPL policy found for user {} with policy id {}" , newUser . get () .getUsername (),
822824 definition );
823825 }
824826 }
@@ -880,6 +882,50 @@ protected List<SideEffect> createAdminUser(InstallConfiguration installConfigura
880882 return sideEffects ;
881883 }
882884
885+ @ Transactional
886+ public List <SideEffect > createSystemAdmin (InstallConfiguration installConfiguration , boolean action ) throws NoSuchAlgorithmException {
887+
888+ var user = installConfiguration .getSystemUser ();
889+
890+ if (null == user ) {
891+ throw new IllegalStateException ("Admin user not found in configuration" );
892+ }
893+ List <SideEffect > sideEffects = new ArrayList <>();
894+ userService .findByUsername ("SYSTEM" ).ifPresentOrElse (
895+ user1 -> {
896+ // ignore
897+ },
898+ () -> {
899+ sideEffects .add (SideEffect .builder ().sideEffectDescription ("Creating admin user " + user .getUsername ()).type (
900+ SideEffectType .UPDATE_DATABASE ).asset ("Users" ).build ());
901+ if (action ) {
902+ try {
903+ user .setUserId ("SYSTEM" );
904+ user .setPassword (userService .encodePassword (UUID .randomUUID ().toString ()));
905+ user .setAuthorizationType (UserType .createSystemAdmin ().toDTO ());
906+ user .setIdentityType (IdentityType .NON_PERSON_ENTITY .toString ());
907+
908+ var type =
909+ userService .getUserType (UserType .createSystemAdmin ());
910+ if (type .isEmpty ()){
911+ type = Optional .of ( userService .saveUserType (UserType .createSystemAdmin ()) );
912+ }
913+
914+ userService .addUscer (User .from (user , type .get ()));
915+ } catch (NoSuchAlgorithmException e ) {
916+ throw new RuntimeException (e );
917+ }
918+
919+ // insert default admin user
920+
921+ }
922+ }
923+ );
924+
925+
926+ return sideEffects ;
927+ }
928+
883929 @ Transactional
884930 protected void createSystemUser (InstallConfiguration connection ) throws NoSuchAlgorithmException {
885931
0 commit comments