Project

General

Profile

Download (4.63 KB) Statistics
| Branch: | Tag: | Revision:
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

    
22
import com.vaadin.spring.annotation.SpringComponent;
23
import com.vaadin.spring.annotation.UIScope;
24

    
25
import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
26
import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
27
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
28
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
29
import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
30

    
31
/**
32
 * The {@link LoginView} is used as replacement view in the scope of other views.
33
 * Therefore the LoginPresenter must be in <b>UIScope</b> so that the LoginPresenter
34
 * is available to all Views.
35
 * <p>
36
 * The LoginPresenter offers a <b>auto login feature for developers</b>. To activate the auto login
37
 * you need to provide the <code>user name</code> and <code>password</code> using the environment variables
38
 * <code>cdm-vaadin.login.usr</code> and <code>cdm-vaadin.login.pwd</code>, e.g.:
39
 * <pre>
40
 * -Dcdm-vaadin.login.usr=admin -Dcdm-vaadin.login.pwd=00000
41
 * </pre>
42
 *
43
 * @author a.kohlbecker
44
 * @since Apr 25, 2017
45
 *
46
 */
47
@SpringComponent
48
@UIScope // DO NOT CHANGE !!! LoginPresenter must be in UIScope so that the LoginPresenter is available to all Views.
49
public class LoginPresenter extends AbstractPresenter<LoginView> {
50

    
51
    private static final long serialVersionUID = 4020699735656994791L;
52

    
53
    private static final Logger log = Logger.getLogger(LoginPresenter.class);
54

    
55
    private final static String PROPNAME_USER = "cdm-vaadin.login.usr";
56

    
57
    private final static String PROPNAME_PASSWORD = "cdm-vaadin.login.pwd";
58

    
59
    @Autowired
60
    protected ApplicationEventPublisher eventBus;
61

    
62
    private String redirectToState;
63

    
64
    public boolean authenticate(String userName, String password){
65

    
66
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, password);
67
        AuthenticationManager authenticationManager = getRepo().getAuthenticationManager();
68
        Authentication authentication = authenticationManager.authenticate(token);
69

    
70
        if(authentication != null && authentication.isAuthenticated()) {
71
            log.debug("user '" + userName + "' authenticated");
72
            currentSecurityContext().setAuthentication(authentication);
73
            if(NavigationManager.class.isAssignableFrom(getNavigationManager().getClass())){
74
                eventBus.publishEvent(new AuthenticationSuccessEvent(userName));
75
                log.debug("redirecting to " + redirectToState);
76
                eventBus.publishEvent(new NavigationEvent(redirectToState));
77
            }
78
        }
79
        return false;
80
    }
81

    
82

    
83

    
84
    /**
85
     * {@inheritDoc}
86
     */
87
    @Override
88
    public void handleViewEntered() {
89

    
90
        List<String> redirectToStateTokens = getNavigationManager().getCurrentViewParameters();
91
        String currentViewName = getNavigationManager().getCurrentViewName();
92

    
93
        if(currentViewName.equals(LoginViewBean.NAME) && redirectToStateTokens.isEmpty()){
94
            // login view is shown in turn to an explicit login request of the user (e.g. login button pressed)
95
            // use the redirectToStateTokens 1-n as redirectToState
96
            //FIXME implement : redirectToState = UserView.NAME
97

    
98
        } else {
99
            // the login view is shown instead of the requested view for which the user needs to login
100
            redirectToState = String.join("/", redirectToStateTokens);
101
        }
102

    
103
        // attempt to auto login
104
        if(StringUtils.isNotEmpty(System.getProperty(PROPNAME_USER)) && StringUtils.isNotEmpty(System.getProperty(PROPNAME_PASSWORD))){
105
            log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
106
            authenticate(System.getProperty(PROPNAME_USER), System.getProperty(PROPNAME_PASSWORD));
107
        }
108
    }
109

    
110
    @EventListener
111
    protected void onLoginEvent(AuthenticationAttemptEvent e){
112
        authenticate(e.getUserName(), getView().getLoginDialog().getPassword().getValue());
113
    }
114

    
115

    
116

    
117
}
(6-6/9)