ref #7995 invoking Registration.updateStatusAndDate() from RegistrationEditorPresenter
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / LoginPresenter.java
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.security.authentication.AuthenticationManager;
17 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
18 import org.springframework.security.core.Authentication;
19 import org.springframework.security.core.AuthenticationException;
20 import org.vaadin.spring.events.Event;
21 import org.vaadin.spring.events.EventBus;
22 import org.vaadin.spring.events.EventBus.ViewEventBus;
23 import org.vaadin.spring.events.EventBusListener;
24
25 import com.vaadin.spring.annotation.SpringComponent;
26 import com.vaadin.spring.annotation.ViewScope;
27
28 import eu.etaxonomy.cdm.vaadin.event.AuthenticationAttemptEvent;
29 import eu.etaxonomy.cdm.vaadin.event.AuthenticationSuccessEvent;
30 import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
31 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
32 import eu.etaxonomy.vaadin.ui.navigation.NavigationManager;
33
34 /**
35 * The {@link LoginView} is used as replacement view in the scope of other views.
36 * Therefore the LoginPresenter must be in <b>UIScope</b> so that the LoginPresenter
37 * is available to all Views.
38 * <p>
39 * The LoginPresenter offers a <b>auto login feature for developers</b>. To activate the auto login
40 * you need to provide the <code>user name</code> and <code>password</code> using the environment variables
41 * <code>cdm-vaadin.login.usr</code> and <code>cdm-vaadin.login.pwd</code>, e.g.:
42 * <pre>
43 * -Dcdm-vaadin.login.usr=admin -Dcdm-vaadin.login.pwd=00000
44 * </pre>
45 *
46 * @author a.kohlbecker
47 * @since Apr 25, 2017
48 *
49 */
50 @SpringComponent
51 @ViewScope
52 public class LoginPresenter extends AbstractPresenter<LoginView> implements EventBusListener<AuthenticationAttemptEvent> {
53
54 private static final long serialVersionUID = 4020699735656994791L;
55
56 private static final Logger log = Logger.getLogger(LoginPresenter.class);
57
58 private final static String PROPNAME_USER = "cdm-vaadin.login.usr";
59
60 private final static String PROPNAME_PASSWORD = "cdm-vaadin.login.pwd";
61
62 private String redirectToState;
63
64 protected EventBus.UIEventBus uiEventBus;
65
66 /**
67 * {@inheritDoc}
68 */
69 @Override
70 protected void eventViewBusSubscription(ViewEventBus viewEventBus) {
71 // not listening to view scope events
72 }
73
74 @Autowired
75 protected void setUIEventBus(EventBus.UIEventBus uiEventBus){
76 this.uiEventBus = uiEventBus;
77 uiEventBus.subscribe(this);
78 }
79
80 public boolean authenticate(String userName, String password) {
81
82 getView().clearMessage();
83
84 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(userName, password);
85 AuthenticationManager authenticationManager = getRepo().getAuthenticationManager();
86 try {
87 Authentication authentication = authenticationManager.authenticate(token);
88 if(authentication != null && authentication.isAuthenticated()) {
89 log.debug("user '" + userName + "' authenticated");
90 currentSecurityContext().setAuthentication(authentication);
91 if(NavigationManager.class.isAssignableFrom(getNavigationManager().getClass())){
92 uiEventBus.publish(this, new AuthenticationSuccessEvent(userName));
93 log.debug("redirecting to " + redirectToState);
94 uiEventBus.publish(this, new NavigationEvent(redirectToState));
95 }
96 }
97 } catch (AuthenticationException e){
98 getView().showErrorMessage("Login failed! Please check your username and password.");
99 }
100 return false;
101 }
102
103
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public void handleViewEntered() {
110
111 List<String> redirectToStateTokens = getNavigationManager().getCurrentViewParameters();
112 String currentViewName = getNavigationManager().getCurrentViewName();
113
114 if(currentViewName.equals(LoginViewBean.NAME) && redirectToStateTokens.isEmpty()){
115 // login view is shown in turn to an explicit login request of the user (e.g. login button pressed)
116 // use the redirectToStateTokens 1-n as redirectToState
117 //FIXME implement : redirectToState = UserView.NAME
118
119 } else {
120 // the login view is shown instead of the requested view for which the user needs to login
121 redirectToState = String.join("/", redirectToStateTokens);
122 }
123
124 // attempt to auto login
125 if(StringUtils.isNotEmpty(System.getProperty(PROPNAME_USER)) && StringUtils.isNotEmpty(System.getProperty(PROPNAME_PASSWORD))){
126 log.warn("Performing autologin with user " + System.getProperty(PROPNAME_USER));
127 authenticate(System.getProperty(PROPNAME_USER), System.getProperty(PROPNAME_PASSWORD));
128 }
129 }
130
131 @Override
132 public void onEvent(Event<AuthenticationAttemptEvent> event) {
133 if(getView()!= null){
134 authenticate(event.getPayload().getUserName(), getView().getLoginDialog().getPassword().getValue());
135 } else {
136 log.info("view is NULL, not yet disposed LoginPresenter?");
137 }
138 }
139
140
141 }