1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.view;
|
10
|
|
11
|
import java.util.List;
|
12
|
|
13
|
import org.apache.commons.lang.StringUtils;
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.context.ApplicationEventPublisher;
|
17
|
import org.springframework.context.event.EventListener;
|
18
|
import org.springframework.security.authentication.AuthenticationManager;
|
19
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
20
|
import org.springframework.security.core.Authentication;
|
21
|
import org.springframework.security.core.AuthenticationException;
|
22
|
|
23
|
import com.vaadin.spring.annotation.SpringComponent;
|
24
|
import com.vaadin.spring.annotation.UIScope;
|
25
|
|
26
|
import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
|
27
|
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
|
28
|
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
|
29
|
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
|
30
|
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
|
31
|
|
32
|
/**
|
33
|
* The {@link LoginView} is used as replacement view in the scope of other views.
|
34
|
* Therefore the LoginPresenter must be in <b>UIScope</b> so that the LoginPresenter
|
35
|
* is available to all Views.
|
36
|
* <p>
|
37
|
* The LoginPresenter offers a <b>auto login feature for developers</b>. To activate the auto login
|
38
|
* you need to provide the <code>user name</code> and <code>password</code> using the environment variables
|
39
|
* <code>cdm-vaadin.login.usr</code> and <code>cdm-vaadin.login.pwd</code>, e.g.:
|
40
|
* <pre>
|
41
|
* -Dcdm-vaadin.login.usr=admin -Dcdm-vaadin.login.pwd=00000
|
42
|
* </pre>
|
43
|
*
|
44
|
* @author a.kohlbecker
|
45
|
* @since Apr 25, 2017
|
46
|
*
|
47
|
*/
|
48
|
@SpringComponent
|
49
|
@UIScope // DO NOT CHANGE !!! LoginPresenter must be in UIScope so that the LoginPresenter is available to all Views.
|
50
|
public class LoginPresenter extends AbstractPresenter<LoginView> {
|
51
|
|
52
|
private static final long serialVersionUID = 4020699735656994791L;
|
53
|
|
54
|
private static final Logger log = Logger.getLogger(LoginPresenter.class);
|
55
|
|
56
|
private final static String PROPNAME_USER = "cdm-vaadin.login.usr";
|
57
|
|
58
|
private final static String PROPNAME_PASSWORD = "cdm-vaadin.login.pwd";
|
59
|
|
60
|
@Autowired
|
61
|
protected ApplicationEventPublisher eventBus;
|
62
|
|
63
|
private String redirectToState;
|
64
|
|
65
|
public boolean authenticate(String userName, String password) {
|
66
|
|
67
|
getView().clearMessage();
|
68
|
|
69
|
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, password);
|
70
|
AuthenticationManager authenticationManager = getRepo().getAuthenticationManager();
|
71
|
try {
|
72
|
Authentication authentication = authenticationManager.authenticate(token);
|
73
|
if(authentication != null && authentication.isAuthenticated()) {
|
74
|
log.debug("user '" + userName + "' authenticated");
|
75
|
currentSecurityContext().setAuthentication(authentication);
|
76
|
if(NavigationManager.class.isAssignableFrom(getNavigationManager().getClass())){
|
77
|
eventBus.publishEvent(new AuthenticationSuccessEvent(userName));
|
78
|
log.debug("redirecting to " + redirectToState);
|
79
|
eventBus.publishEvent(new NavigationEvent(redirectToState));
|
80
|
}
|
81
|
}
|
82
|
} catch (AuthenticationException e){
|
83
|
getView().showErrorMessage("Login failed! Please check your username and password.");
|
84
|
}
|
85
|
return false;
|
86
|
}
|
87
|
|
88
|
|
89
|
|
90
|
/**
|
91
|
* {@inheritDoc}
|
92
|
*/
|
93
|
@Override
|
94
|
public void handleViewEntered() {
|
95
|
|
96
|
List<String> redirectToStateTokens = getNavigationManager().getCurrentViewParameters();
|
97
|
String currentViewName = getNavigationManager().getCurrentViewName();
|
98
|
|
99
|
if(currentViewName.equals(LoginViewBean.NAME) && redirectToStateTokens.isEmpty()){
|
100
|
// login view is shown in turn to an explicit login request of the user (e.g. login button pressed)
|
101
|
// use the redirectToStateTokens 1-n as redirectToState
|
102
|
//FIXME implement : redirectToState = UserView.NAME
|
103
|
|
104
|
} else {
|
105
|
// the login view is shown instead of the requested view for which the user needs to login
|
106
|
redirectToState = String.join("/", redirectToStateTokens);
|
107
|
}
|
108
|
|
109
|
// attempt to auto login
|
110
|
if(StringUtils.isNotEmpty(System.getProperty(PROPNAME_USER)) && StringUtils.isNotEmpty(System.getProperty(PROPNAME_PASSWORD))){
|
111
|
log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
|
112
|
authenticate(System.getProperty(PROPNAME_USER), System.getProperty(PROPNAME_PASSWORD));
|
113
|
}
|
114
|
}
|
115
|
|
116
|
@EventListener
|
117
|
protected void onLoginEvent(AuthenticationAttemptEvent e){
|
118
|
authenticate(e.getUserName(), getView().getLoginDialog().getPassword().getValue());
|
119
|
}
|
120
|
|
121
|
|
122
|
|
123
|
}
|