private void useForceAdminCreationFile()

in redback-integrations/redback-common-integrations/src/main/java/org/apache/archiva/redback/integration/checks/security/AdminAutoCreateCheck.java [108:194]


    private void useForceAdminCreationFile()
    {
        try
        {
            String forceAdminFilePath = System.getProperty( FORCE_ADMIN_FILE_PATH );
            if ( StringUtils.isBlank( forceAdminFilePath ) )
            {
                log.info( "{} system props is empty don't use an auto creation admin ", FORCE_ADMIN_FILE_PATH );
                return;
            }
            Path file = Paths.get( forceAdminFilePath );
            if ( !Files.exists(file) )
            {
                log.warn( "file set in sysprops {} not exists skip admin auto creation", FORCE_ADMIN_FILE_PATH );
                return;
            }
            log.debug( "user {} not found try auto creation", getAdminUid() );
            Properties properties = new Properties();
            InputStream fis = Files.newInputStream(file);
            try
            {
                properties.load( fis );
            }
            catch ( Exception e )
            {
                log.warn( "error loading properties from file {} skip admin auto creation", forceAdminFilePath );
                return;
            }
            finally
            {
                IOUtils.closeQuietly( fis );
            }

            // ensure we have all properties
            String password = properties.getProperty( ADMIN_PASSWORD_KEY );
            String email = properties.getProperty( ADMIN_EMAIL_KEY );
            String fullName = properties.getProperty( ADMIN_FULL_NAME_KEY );

            if ( StringUtils.isBlank( password ) )
            {
                log.warn( "property {} not set skip auto admin creation", ADMIN_PASSWORD_KEY );
                return;
            }

            if ( StringUtils.isBlank( email ) )
            {
                log.warn( "property not set skip auto admin creation", ADMIN_EMAIL_KEY );
                return;
            }

            if ( StringUtils.isBlank( fullName ) )
            {
                log.warn( "property {} not set skip auto admin creation", ADMIN_FULL_NAME_KEY );
                return;
            }

            User u = userManager.createUser( getAdminUid(), fullName, email );

            u.setPassword( password );
            u.setLocked( false );
            u.setPasswordChangeRequired( false );
            u.setPermanent( true );
            u.setValidated( true );

            u = userManager.addUser( u );
            u.setPassword( password );

            PasswordBasedAuthenticationDataSource authdatasource = new PasswordBasedAuthenticationDataSource();
            authdatasource.setPrincipal( u.getUsername() );
            authdatasource.setPassword( u.getPassword() );
            SecuritySession securitySession = securitySystem.authenticate( authdatasource );
            if ( securitySession.getAuthenticationResult().isAuthenticated() )
            {
                // good add various tokens.
                u = securitySession.getUser();
                u.setLastLoginDate( new Date() );
                u.setPassword( null );
                securitySystem.getUserManager().updateUser( u );
            }
            assignAdminRole( u );

        }
        catch ( Exception e )
        {
            log.warn( "failed to automatically create an admin account {}", e.getMessage(), e );
        }
    }