@Test public void shouldChangeUserStatusToMustChangePass() { MotechUser motechUser = new MotechUser("bob", "encodedPassword", "entity_1", "", asList("some_role"), "", Locale.ENGLISH); motechUser.setUserStatus(UserStatus.ACTIVE); motechUser.setLastPasswordChange(DateTime.now().minusDays(3)); when(motechUsersDao.findByUserName("bob")).thenReturn(motechUser); when(settingService.getNumberOfDaysToChangePassword()).thenReturn(2); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("bob", "encodedPassword"); UserDetails details = authenticationProvider.retrieveUser("bob", authentication); verify(motechUsersDao).update(userCaptor.capture()); assertTrue(details.isAccountNonLocked()); assertFalse(details.isCredentialsNonExpired()); MotechUser capturedUser = userCaptor.getValue(); Assert.assertEquals((Integer) 0, capturedUser.getFailureLoginCounter()); Assert.assertEquals(UserStatus.MUST_CHANGE_PASSWORD, capturedUser.getUserStatus()); }
@Override @Transactional protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) { MotechUser user = motechUsersDao.findByUserName(username); if (user == null) { throw new BadCredentialsException(USER_NOT_FOUND); } else if (!user.isActive()) { throw new LockedException(USER_BLOCKED); } else { if (settingService.getNumberOfDaysToChangePassword() > 0 && Days.daysBetween(user.getSafeLastPasswordChange(), DateUtil.now()).getDays() >= settingService.getNumberOfDaysToChangePassword()) { user.setUserStatus(UserStatus.MUST_CHANGE_PASSWORD); motechUsersDao.update(user); } authentication.setDetails(new MotechUserProfile(user)); return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user)); } }
MotechAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { @Override @Transactional protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) { MotechUser user = motechUsersDao.findByUserName(username); if (user == null) { throw new BadCredentialsException(USER_NOT_FOUND); } else if (!user.isActive()) { throw new LockedException(USER_BLOCKED); } else { if (settingService.getNumberOfDaysToChangePassword() > 0 && Days.daysBetween(user.getSafeLastPasswordChange(), DateUtil.now()).getDays() >= settingService.getNumberOfDaysToChangePassword()) { user.setUserStatus(UserStatus.MUST_CHANGE_PASSWORD); motechUsersDao.update(user); } authentication.setDetails(new MotechUserProfile(user)); return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user)); } } }
MotechAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { @Override @Transactional protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) { MotechUser user = motechUsersDao.findByUserName(username); if (user == null) { throw new BadCredentialsException(USER_NOT_FOUND); } else if (!user.isActive()) { throw new LockedException(USER_BLOCKED); } else { if (settingService.getNumberOfDaysToChangePassword() > 0 && Days.daysBetween(user.getSafeLastPasswordChange(), DateUtil.now()).getDays() >= settingService.getNumberOfDaysToChangePassword()) { user.setUserStatus(UserStatus.MUST_CHANGE_PASSWORD); motechUsersDao.update(user); } authentication.setDetails(new MotechUserProfile(user)); return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user)); } } @Autowired  MotechAuthenticationProvider(MotechUsersDao motechUsersDao, MotechPasswordEncoder motechPasswordEncoder,
                                        AuthoritiesService authoritiesService, SettingService settingService); }
MotechAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { @Override @Transactional protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) { MotechUser user = motechUsersDao.findByUserName(username); if (user == null) { throw new BadCredentialsException(USER_NOT_FOUND); } else if (!user.isActive()) { throw new LockedException(USER_BLOCKED); } else { if (settingService.getNumberOfDaysToChangePassword() > 0 && Days.daysBetween(user.getSafeLastPasswordChange(), DateUtil.now()).getDays() >= settingService.getNumberOfDaysToChangePassword()) { user.setUserStatus(UserStatus.MUST_CHANGE_PASSWORD); motechUsersDao.update(user); } authentication.setDetails(new MotechUserProfile(user)); return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user)); } } @Autowired  MotechAuthenticationProvider(MotechUsersDao motechUsersDao, MotechPasswordEncoder motechPasswordEncoder,
                                        AuthoritiesService authoritiesService, SettingService settingService);  }
MotechAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider { @Override @Transactional protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) { MotechUser user = motechUsersDao.findByUserName(username); if (user == null) { throw new BadCredentialsException(USER_NOT_FOUND); } else if (!user.isActive()) { throw new LockedException(USER_BLOCKED); } else { if (settingService.getNumberOfDaysToChangePassword() > 0 && Days.daysBetween(user.getSafeLastPasswordChange(), DateUtil.now()).getDays() >= settingService.getNumberOfDaysToChangePassword()) { user.setUserStatus(UserStatus.MUST_CHANGE_PASSWORD); motechUsersDao.update(user); } authentication.setDetails(new MotechUserProfile(user)); return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user)); } } @Autowired  MotechAuthenticationProvider(MotechUsersDao motechUsersDao, MotechPasswordEncoder motechPasswordEncoder,
                                        AuthoritiesService authoritiesService, SettingService settingService);  static final String PLEASE_ENTER_PASSWORD; static final String USER_NOT_FOUND; static final String USER_BLOCKED; }