1
|
package eu.etaxonomy.cdm.vaadin.presenter;
|
2
|
|
3
|
import java.net.URI;
|
4
|
|
5
|
import org.springframework.beans.factory.annotation.Autowired;
|
6
|
import org.springframework.context.annotation.Scope;
|
7
|
import org.springframework.security.authentication.AuthenticationManager;
|
8
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
9
|
import org.springframework.security.core.Authentication;
|
10
|
import org.springframework.security.core.context.SecurityContextHolder;
|
11
|
|
12
|
import com.vaadin.server.VaadinSession;
|
13
|
import com.vaadin.spring.annotation.SpringComponent;
|
14
|
|
15
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
16
|
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinAuthentication;
|
17
|
import eu.etaxonomy.cdm.vaadin.util.CdmVaadinSessionUtilities;
|
18
|
import eu.etaxonomy.cdm.vaadin.view.IAuthenticationComponent;
|
19
|
|
20
|
@SpringComponent
|
21
|
@Scope("prototype")
|
22
|
public class AuthenticationPresenter implements IAuthenticationComponent.AuthenticationComponentListener{
|
23
|
|
24
|
@Autowired
|
25
|
private CdmRepository cdmRepo = null;
|
26
|
|
27
|
@Override
|
28
|
public boolean login(URI uri, String context, String userName, String password) {
|
29
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, password);
|
30
|
AuthenticationManager authenticationManager = cdmRepo.getAuthenticationManager();
|
31
|
Authentication authentication = authenticationManager.authenticate(token);
|
32
|
if(authentication != null && authentication.isAuthenticated()) {
|
33
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
34
|
CdmVaadinAuthentication cvAuthentication = (CdmVaadinAuthentication) VaadinSession.getCurrent().getAttribute(CdmVaadinAuthentication.KEY);
|
35
|
if(cvAuthentication == null) {
|
36
|
cvAuthentication = new CdmVaadinAuthentication();
|
37
|
}
|
38
|
cvAuthentication.addAuthentication(uri, context, authentication);
|
39
|
CdmVaadinSessionUtilities.setCurrentAttribute(CdmVaadinAuthentication.KEY, cvAuthentication);
|
40
|
return true;
|
41
|
}
|
42
|
return false;
|
43
|
}
|
44
|
|
45
|
}
|