public RegistrationKey registerUser()

in redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java [655:767]


    public RegistrationKey registerUser( String userId, UserRegistrationRequest userRegistrationRequest )
        throws RedbackServiceException
    {
        User user = userRegistrationRequest.getUser( );
        if ( user == null )
        {
            throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USER_NOT_FOUND, userId ), 404 );

        }

        UserSecurityPolicy securityPolicy = securitySystem.getPolicy( );

        boolean emailValidationRequired = securityPolicy.getUserValidationSettings( ).isEmailValidationRequired( );

        if ( emailValidationRequired )
        {
            validateCredentialsLoose( user );
        }
        else
        {
            validateCredentialsStrict( user );
        }

        org.apache.archiva.redback.users.User u = null;

        try
        {

            // NOTE: Do not perform Password Rules Validation Here.

            if ( userManager.userExists( user.getUserId( ) ) )
            {
                throw new RedbackServiceException(
                    ErrorMessage.of( MessageKeys.ERR_USER_EXISTS, user.getUserId() ));
            }

            u = userManager.createUser( user.getUserId( ), user.getFullName( ), user.getEmail( ) );
            u.setPassword( user.getPassword( ) );
            u.setValidated( false );
            u.setLocked( false );

            roleManager.assignRole( RedbackRoleConstants.REGISTERED_USER_ROLE_ID, u.getUsername( ) );
        }
        catch ( RoleManagerException rpe )
        {
            log.error( "RoleProfile Error: {}", rpe.getMessage( ), rpe );
            throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_REGISTRATION_ROLE_ASSIGNMENT_FAILED, rpe.getMessage( ) ), 400 );
        }
        catch ( UserManagerException e )
        {
            throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USERMANAGER_FAIL, e.getMessage( ) ), 400 );
        }

        if ( emailValidationRequired )
        {
            u.setLocked( true );

            try
            {
                AuthenticationKey authkey =
                    securitySystem.getKeyManager( ).createKey( u.getUsername( ), "New User Email Validation",
                        securityPolicy.getUserValidationSettings( ).getEmailValidationTimeout( ) );

                String baseUrl = userRegistrationRequest.getApplicationUrl( );
                if ( StringUtils.isBlank( baseUrl ) )
                {
                    baseUrl = getBaseUrl( );
                }

                log.debug( "register user {} with email {} and app url {}", u.getUsername( ), u.getEmail( ), baseUrl );

                mailer.sendAccountValidationEmail( Arrays.asList( u.getEmail( ) ), authkey, baseUrl );

                securityPolicy.setEnabled( false );
                userManager.addUser( u );
                return new RegistrationKey( authkey.getKey( ), true );

            }
            catch ( KeyManagerException e )
            {
                log.error( "Unable to register a new user.", e );
                throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_KEYMANAGER_FAIL, e.getMessage( ) ), 400 );
            }
            catch ( UserManagerException e )
            {
                throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USERMANAGER_FAIL, e.getMessage( ) ), 400 );
            }
            finally
            {
                securityPolicy.setEnabled( true );
            }
        }
        else
        {
            try
            {
                userManager.addUser( u );
                return new RegistrationKey( "-1", false );
            }
            catch ( UserManagerException e )
            {
                throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USERMANAGER_FAIL, e.getMessage( ) ), 400 );
            }
        }

        // FIXME log this event
        /*
        AuditEvent event = new AuditEvent( getText( "log.account.create" ) );
        event.setAffectedUser( username );
        event.log();
        */

    }