@Test void activateUser_alreadyActivated() { @SuppressWarnings("unchecked") Query<User> q = mock(Query.class); when(q.eq(ACTIVE, false)).thenReturn(q); when(q.and()).thenReturn(q); when(q.eq(ACTIVATIONCODE, "456")).thenReturn(q); when(q.findOne()).thenReturn(null); when(dataService.query(USER, User.class)).thenReturn(q); assertThrows(MolgenisUserException.class, () -> accountService.activateUser("456")); }
@Override @RunAsSystem public void activateUser(String activationCode) { User user = dataService .query(USER, User.class) .eq(ACTIVE, false) .and() .eq(ACTIVATIONCODE, activationCode) .findOne(); if (user != null) { user.setActive(true); dataService.update(USER, user); SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(user.getEmail()); mailMessage.setSubject("Your registration request for " + appSettings.getTitle()); mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle())); mailSender.send(mailMessage); } else { throw new MolgenisUserException("Invalid activation code or account already activated."); } }
AccountServiceImpl implements AccountService { @Override @RunAsSystem public void activateUser(String activationCode) { User user = dataService .query(USER, User.class) .eq(ACTIVE, false) .and() .eq(ACTIVATIONCODE, activationCode) .findOne(); if (user != null) { user.setActive(true); dataService.update(USER, user); SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(user.getEmail()); mailMessage.setSubject("Your registration request for " + appSettings.getTitle()); mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle())); mailSender.send(mailMessage); } else { throw new MolgenisUserException("Invalid activation code or account already activated."); } } }
AccountServiceImpl implements AccountService { @Override @RunAsSystem public void activateUser(String activationCode) { User user = dataService .query(USER, User.class) .eq(ACTIVE, false) .and() .eq(ACTIVATIONCODE, activationCode) .findOne(); if (user != null) { user.setActive(true); dataService.update(USER, user); SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(user.getEmail()); mailMessage.setSubject("Your registration request for " + appSettings.getTitle()); mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle())); mailSender.send(mailMessage); } else { throw new MolgenisUserException("Invalid activation code or account already activated."); } } AccountServiceImpl(
      DataService dataService,
      MailSender mailSender,
      UserService userService,
      AppSettings appSettings,
      AuthenticationSettings authenticationSettings,
      IdGenerator idGenerator); }
AccountServiceImpl implements AccountService { @Override @RunAsSystem public void activateUser(String activationCode) { User user = dataService .query(USER, User.class) .eq(ACTIVE, false) .and() .eq(ACTIVATIONCODE, activationCode) .findOne(); if (user != null) { user.setActive(true); dataService.update(USER, user); SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(user.getEmail()); mailMessage.setSubject("Your registration request for " + appSettings.getTitle()); mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle())); mailSender.send(mailMessage); } else { throw new MolgenisUserException("Invalid activation code or account already activated."); } } AccountServiceImpl(
      DataService dataService,
      MailSender mailSender,
      UserService userService,
      AppSettings appSettings,
      AuthenticationSettings authenticationSettings,
      IdGenerator idGenerator); @Override @RunAsSystem @Transactional void createUser(User user, String baseActivationUri); @Override @RunAsSystem void activateUser(String activationCode); }
AccountServiceImpl implements AccountService { @Override @RunAsSystem public void activateUser(String activationCode) { User user = dataService .query(USER, User.class) .eq(ACTIVE, false) .and() .eq(ACTIVATIONCODE, activationCode) .findOne(); if (user != null) { user.setActive(true); dataService.update(USER, user); SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setTo(user.getEmail()); mailMessage.setSubject("Your registration request for " + appSettings.getTitle()); mailMessage.setText(createActivatedEmailText(user, appSettings.getTitle())); mailSender.send(mailMessage); } else { throw new MolgenisUserException("Invalid activation code or account already activated."); } } AccountServiceImpl(
      DataService dataService,
      MailSender mailSender,
      UserService userService,
      AppSettings appSettings,
      AuthenticationSettings authenticationSettings,
      IdGenerator idGenerator); @Override @RunAsSystem @Transactional void createUser(User user, String baseActivationUri); @Override @RunAsSystem void activateUser(String activationCode);  }