@Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(
      value = "updateAccount",
      params = {"delete"},
      method = RequestMethod.POST)
  public ModelAndView deleteAccount(@Valid User user, HttpServletRequest request)
      throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);
    if (!authUserId.equals(user.getId())) throw new IllegalRequestException();

    userProfileMapper.deleteProfile(authUserId);
    userMasterMapper.deleteUser(authUserId);

    ConnectionRepository connectionRepository =
        usersConnectionRepository.createConnectionRepository(authUserId.toString());
    Connection connection = connectionRepository.findPrimaryConnection(Facebook.class);
    if (connection != null) connectionRepository.removeConnection(connection.getKey());
    connection = connectionRepository.findPrimaryConnection(Twitter.class);
    if (connection != null) connectionRepository.removeConnection(connection.getKey());

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("forward:logout");
    return modelAndView;
  }
 private boolean userNotFound(String userId) {
   // doesn't bother checking a local user database: simply checks if the userId is connected to
   // Facebook
   return connectionRepository
           .createConnectionRepository(userId)
           .findPrimaryConnection(Twitter.class)
       != null;
 }
 private void handleSignOut(HttpServletRequest request, HttpServletResponse response) {
   if (SecurityContext.userSignedIn() && request.getServletPath().startsWith("/signout")) {
     connectionRepository
         .createConnectionRepository(SecurityContext.getCurrentUser().getId())
         .removeConnections("twitter");
     userCookieGenerator.removeCookie(response);
     SecurityContext.remove();
   }
 }
 @Test
 public void test() {
   logger.debug("Running '{}'...", name.getMethodName());
   con = usersConnectionRepository.createConnectionRepository("GeraldXv");
   Connection<Twitter> twitter = con.findPrimaryConnection(Twitter.class);
   twitterApi = twitter.getApi();
   Connection<Google> google = con.findPrimaryConnection(Google.class);
   googleApi = google.getApi();
   System.out.println(twitterApi.friendOperations().getFriends().get(0).getName());
   // System.out.println(googleApi.personOperations().getPerson("110377639084744464746").get);
 }
    public LinkedIn getApi() {
      if (logger.isDebugEnabled()) {
        logger.debug("Getting API binding instance for Facebook");
      }

      Connection<LinkedIn> connection =
          usersConnectionRepository
              .createConnectionRepository(userIdSource.getUserId())
              .findPrimaryConnection(LinkedIn.class);
      if (logger.isDebugEnabled() && connection == null) {
        logger.debug("No current connection; Returning default FacebookTemplate instance.");
      }
      return connection != null ? connection.getApi() : null;
    }
  @Before
  public void setup() {
    MockitoAnnotations.initMocks(this);
    doNothing()
        .when(mockMailService)
        .sendSocialRegistrationValidationEmail(anyObject(), anyString());
    doNothing().when(mockConnectionRepository).addConnection(anyObject());
    when(mockUsersConnectionRepository.createConnectionRepository(anyString()))
        .thenReturn(mockConnectionRepository);

    socialService = new SocialService();
    ReflectionTestUtils.setField(socialService, "authorityRepository", authorityRepository);
    ReflectionTestUtils.setField(socialService, "passwordEncoder", passwordEncoder);
    ReflectionTestUtils.setField(socialService, "mailService", mockMailService);
    ReflectionTestUtils.setField(socialService, "userRepository", userRepository);
    ReflectionTestUtils.setField(
        socialService, "usersConnectionRepository", mockUsersConnectionRepository);
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(value = "updateSocial", method = RequestMethod.GET)
  public ModelAndView updateSocial(HttpServletRequest request) throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);

    ConnectionRepository connectionRepository =
        usersConnectionRepository.createConnectionRepository(authUserId.toString());
    boolean facebookConnected =
        ((connectionRepository.findPrimaryConnection(Facebook.class)) != null);
    boolean twitterConnected =
        ((connectionRepository.findPrimaryConnection(Twitter.class)) != null);

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("facebookConnected", facebookConnected);
    modelAndView.addObject("twitterConnected", twitterConnected);
    modelAndView.setViewName("user/editSocial");
    return modelAndView;
  }
  @Transactional(rollbackForClassName = "java.lang.Exception")
  @RequestMapping(value = "disconnect", method = RequestMethod.POST)
  public ModelAndView disconnect(HttpServletRequest request) throws IllegalRequestException {

    Integer authUserId = userCookieGenerator.getUserId(request);

    ConnectionRepository connectionRepository =
        usersConnectionRepository.createConnectionRepository(authUserId.toString());
    if (request.getParameter("disconnectFacebook") != null) {
      Connection connection = connectionRepository.findPrimaryConnection(Facebook.class);
      if (connection != null) connectionRepository.removeConnection(connection.getKey());
    }
    if (request.getParameter("disconnectTwitter") != null) {
      Connection connection = connectionRepository.findPrimaryConnection(Twitter.class);
      if (connection != null) connectionRepository.removeConnection(connection.getKey());
    }
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("activeTab", "social");
    modelAndView.addObject("updated", true);
    modelAndView.setViewName("user/edit");
    return modelAndView;
  }
  @SuppressWarnings("unchecked")
  @Test
  public void addConnection() {
    UsersConnectionRepository usersConnectionRepository = mock(UsersConnectionRepository.class);
    SocialAuthenticationFilter filter =
        new SocialAuthenticationFilter(null, null, usersConnectionRepository, null);

    SocialAuthenticationService<Object> authService = mock(SocialAuthenticationService.class);
    ConnectionRepository connectionRepository = mock(ConnectionRepository.class);
    ConnectionFactory<Object> connectionFactory = mock(MockConnectionFactory.class);

    MockHttpServletRequest request = new MockHttpServletRequest();
    ConnectionData data =
        new ConnectionData("dummyprovider", "1234", null, null, null, null, null, null, null);
    String userId = "joe";

    DummyConnection<Object> connection = DummyConnection.dummy(data.getProviderId(), userId);

    when(usersConnectionRepository.findUserIdsConnectedTo(
            data.getProviderId(), set(data.getProviderUserId())))
        .thenReturn(empty(String.class));
    when(usersConnectionRepository.createConnectionRepository(userId))
        .thenReturn(connectionRepository);

    when(authService.getConnectionCardinality()).thenReturn(ConnectionCardinality.ONE_TO_ONE);
    when(authService.getConnectionFactory()).thenReturn(connectionFactory);
    when(authService.getConnectionAddedRedirectUrl(request, connection)).thenReturn("/redirect");

    when(connectionFactory.createConnection(data)).thenReturn(connection);

    Connection<?> addedConnection = filter.addConnection(authService, userId, data);
    assertNotNull(addedConnection);
    assertSame(connection, addedConnection);

    verify(connectionRepository).addConnection(connection);
  }
 private void createSocialConnection(String login, Connection<?> connection) {
   ConnectionRepository connectionRepository =
       usersConnectionRepository.createConnectionRepository(login);
   connectionRepository.addConnection(connection);
 }
 private boolean userNotFound(String userId) {
   return myConnectionRepository
           .createConnectionRepository(userId)
           .findPrimaryConnection(Facebook.class)
       != null;
 }