@Test public void shouldConnectFoundUser() { when(eventMock.getAuthentication()).thenReturn(authenticationMock); when(authenticationMock.getPrincipal()).thenReturn(userDetailsMock); when(userDetailsMock.getSource()).thenReturn(USERSOURCE); when(userDetailsMock.getSourceId()).thenReturn(USERSOURCEID); when(userServiceMock.findBySource(userDetailsMock.getSource(), userDetailsMock.getSourceId(), false)).thenReturn(new UserEntity()); listener.onApplicationEvent(eventMock); verify(userServiceMock, times(1)).findBySource(USERSOURCE, userDetailsMock.getSourceId(), false); verify(userServiceMock, never()).create(any(NewExternalUserEntity.class), anyBoolean()); verify(userServiceMock, times(1)).connect(userDetailsMock.getUsername()); }
@Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); details.setUsername(registeredUser.getId()); if ("memory".equals(details.getSource()) && registeredUser.getEmail() != null) { details.setEmail(registeredUser.getEmail()); SecurityContextHolder.getContext().setAuthentication(event.getAuthentication()); } } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); byte[] pictureData = details.getPicture(); if(pictureData != null && pictureData.length > 0) { String picture = computePicture(pictureData); newUser.setPicture(picture); } boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.ENVIRONMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.ORGANIZATION, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); }
AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { @Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); details.setUsername(registeredUser.getId()); if ("memory".equals(details.getSource()) && registeredUser.getEmail() != null) { details.setEmail(registeredUser.getEmail()); SecurityContextHolder.getContext().setAuthentication(event.getAuthentication()); } } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); byte[] pictureData = details.getPicture(); if(pictureData != null && pictureData.length > 0) { String picture = computePicture(pictureData); newUser.setPicture(picture); } boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.ENVIRONMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.ORGANIZATION, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); } }
AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { @Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); details.setUsername(registeredUser.getId()); if ("memory".equals(details.getSource()) && registeredUser.getEmail() != null) { details.setEmail(registeredUser.getEmail()); SecurityContextHolder.getContext().setAuthentication(event.getAuthentication()); } } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); byte[] pictureData = details.getPicture(); if(pictureData != null && pictureData.length > 0) { String picture = computePicture(pictureData); newUser.setPicture(picture); } boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.ENVIRONMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.ORGANIZATION, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); }  }
AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { @Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); details.setUsername(registeredUser.getId()); if ("memory".equals(details.getSource()) && registeredUser.getEmail() != null) { details.setEmail(registeredUser.getEmail()); SecurityContextHolder.getContext().setAuthentication(event.getAuthentication()); } } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); byte[] pictureData = details.getPicture(); if(pictureData != null && pictureData.length > 0) { String picture = computePicture(pictureData); newUser.setPicture(picture); } boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.ENVIRONMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.ORGANIZATION, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); }  @Override void onApplicationEvent(AuthenticationSuccessEvent event); String computePicture(final byte[] pictureData); }
AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> { @Override public void onApplicationEvent(AuthenticationSuccessEvent event) { final UserDetails details = (UserDetails) event.getAuthentication().getPrincipal(); try { UserEntity registeredUser = userService.findBySource(details.getSource(), details.getSourceId(), false); updateRegisteredUser(registeredUser, details); details.setUsername(registeredUser.getId()); if ("memory".equals(details.getSource()) && registeredUser.getEmail() != null) { details.setEmail(registeredUser.getEmail()); SecurityContextHolder.getContext().setAuthentication(event.getAuthentication()); } } catch (UserNotFoundException unfe) { final NewExternalUserEntity newUser = new NewExternalUserEntity(); newUser.setSource(details.getSource()); newUser.setSourceId(details.getSourceId()); newUser.setFirstname(details.getFirstname()); newUser.setLastname(details.getLastname()); newUser.setEmail(details.getEmail()); byte[] pictureData = details.getPicture(); if(pictureData != null && pictureData.length > 0) { String picture = computePicture(pictureData); newUser.setPicture(picture); } boolean addDefaultRole = false; if (event.getAuthentication().getAuthorities() == null || event.getAuthentication().getAuthorities().isEmpty()) { addDefaultRole = true; } UserEntity createdUser = userService.create(newUser, addDefaultRole); details.setUsername(createdUser.getId()); if (!addDefaultRole) { addRole(RoleScope.ENVIRONMENT, createdUser.getId(), event.getAuthentication().getAuthorities()); addRole(RoleScope.ORGANIZATION, createdUser.getId(), event.getAuthentication().getAuthorities()); } } userService.connect(details.getUsername()); }  @Override void onApplicationEvent(AuthenticationSuccessEvent event); String computePicture(final byte[] pictureData);  }