SpringSource/spring-social — Issue #26: [suggestion] Halting Connection process by returning boolean from preConnect?
Author: rickcr
Created: 2011-12-08 00:58:35+00:00
State: open

In our application we do not want to allow a user to connect with a different facebook connection if they've already connected previously with a different facebook providerUserId.

I've already had to extend ConnectController for some other purposes so I could accomplish what I need by overriding connect as so:

```java

@Override
public RedirectView connect(@PathVariable String providerId, NativeWebRequest request) {
/*
User from session
if userId found in UserConnections table for the providerId, then abort the connect process
put message in scope "This account has already been connected with Facebook User: "John Doe"
return user to some page
*/
else {
return super.connect(providerId, request);
}
}
```

However, many users might not have a need to really extend ConnectController but they'll probably have created an Interceptor and might be relying on preConnect.

I was thinking why not have preConnect in ConnectController return a boolean and in the preConnect method if any of the interceptor preConnects return a 'false' then you leave preConnect and return false. Within "connect" you'd then halt the oauth stuff if preConnect returned false. So something like:

```java
@RequestMapping(value="/{providerId}", method=RequestMethod.POST)
public RedirectView connect(@PathVariable String providerId, NativeWebRequest request) {
ConnectionFactory connectionFactory = connectionFactoryLocator.getConnectionFactory(providerId);
MultiValueMap parameters = new LinkedMultiValueMap();
if (preConnect(connectionFactory, parameters, request)) {
return new RedirectView(webSupport.buildOAuthUrl(connectionFactory, request, parameters));
} else {
return new RedirectView("connectionExistsForUser");
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
protected boolean preConnect(ConnectionFactory connectionFactory, MultiValueMap parameters, WebRequest request) {
for (ConnectInterceptor interceptor : interceptingConnectionsTo(connectionFactory)) {
boolean ok = interceptor.preConnect(connectionFactory, parameters, request);
if (!ok) return false;
}
return true;
}
```