@Test public void shouldBuildImmutableUser() { LegacyCredential abc = LegacyCredential.forPassword( "123abc" ); LegacyCredential fruit = LegacyCredential.forPassword( "fruit" ); User u1 = new User.Builder( "Steve", abc ).build(); User u2 = new User.Builder( "Steve", fruit ) .withRequiredPasswordChange( true ) .withFlag( "nice_guy" ).build(); assertThat( u1, equalTo( u1 ) ); assertThat( u1, not( equalTo( u2 ) ) ); User u1AsU2 = u1.augment().withCredentials( fruit ) .withRequiredPasswordChange( true ) .withFlag( "nice_guy" ).build(); assertThat( u1, not( equalTo( u1AsU2 ))); assertThat( u2, equalTo( u1AsU2 )); User u2AsU1 = u2.augment().withCredentials( abc ) .withRequiredPasswordChange( false ) .withoutFlag( "nice_guy" ).build(); assertThat( u2, not( equalTo( u2AsU1 ))); assertThat( u1, equalTo( u2AsU1 )); assertThat( u1, not( equalTo( u2 ) ) ); } }
User updatedUser = existingUser.augment().withCredentials( LegacyCredential.forPassword( password ) ).withRequiredPasswordChange( requirePasswordChange ).build(); userRepository.update( existingUser, updatedUser );
@Test public void shouldThrowIfExistingUserDoesNotMatch() throws Throwable { // Given FileUserRepository users = new FileUserRepository( fs, authFile, logProvider ); User user = new User.Builder( "jake", LegacyCredential.INACCESSIBLE ).withRequiredPasswordChange( true ).build(); users.create( user ); User modifiedUser = user.augment().withCredentials( LegacyCredential.forPassword( "foo" ) ).build(); // When User updatedUser = user.augment().withCredentials( LegacyCredential.forPassword( "bar" ) ).build(); try { users.update( modifiedUser, updatedUser ); fail( "expected exception not thrown" ); } catch ( ConcurrentModificationException e ) { // Then continue } }
@Override public void suspendUser( String username ) throws IOException, InvalidArgumentsException { User user = getUser( username ); if ( !user.hasFlag( IS_SUSPENDED ) ) { User suspendedUser = user.augment().withFlag( IS_SUSPENDED ).build(); try { synchronized ( this ) { userRepository.update( user, suspendedUser ); } } catch ( ConcurrentModificationException e ) { // Try again suspendUser( username ); } } clearCacheForUser( username ); }
@Override public void activateUser( String username, boolean requirePasswordChange ) throws IOException, InvalidArgumentsException { User user = getUser( username ); if ( user.hasFlag( IS_SUSPENDED ) ) { User activatedUser = user.augment() .withoutFlag( IS_SUSPENDED ) .withRequiredPasswordChange( requirePasswordChange ) .build(); try { synchronized ( this ) { userRepository.update( user, activatedUser ); } } catch ( ConcurrentModificationException e ) { // Try again activateUser( username, requirePasswordChange ); } } clearCacheForUser( username ); }
User updatedUser = existingUser.augment().withCredentials( LegacyCredential.forPassword( password ) ).withRequiredPasswordChange( requirePasswordChange ).build(); userRepository.update( existingUser, updatedUser );
@Override public void setUserPassword( String username, String password, boolean requirePasswordChange ) throws IOException, InvalidArgumentsException { User existingUser = getUser( username ); passwordPolicy.validatePassword( password ); if ( existingUser.credentials().matchesPassword( password ) ) { throw new InvalidArgumentsException( "Old password and new password cannot be the same." ); } try { User updatedUser = existingUser.augment() .withCredentials( Credential.forPassword( password ) ) .withRequiredPasswordChange( requirePasswordChange ) .build(); synchronized ( this ) { userRepository.update( existingUser, updatedUser ); } } catch ( ConcurrentModificationException e ) { // try again setUserPassword( username, password, requirePasswordChange ); } clearCacheForUser( username ); }