Project

General

Profile

Download (5.9 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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 org.springframework.security.authentication.BadCredentialsException;
12

    
13
import com.vaadin.annotations.AutoGenerated;
14
import com.vaadin.data.validator.StringLengthValidator;
15
import com.vaadin.event.ShortcutAction.KeyCode;
16
import com.vaadin.navigator.View;
17
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
18
import com.vaadin.server.Page;
19
import com.vaadin.server.VaadinServlet;
20
import com.vaadin.spring.annotation.SpringView;
21
import com.vaadin.ui.Alignment;
22
import com.vaadin.ui.Button;
23
import com.vaadin.ui.Button.ClickEvent;
24
import com.vaadin.ui.Button.ClickListener;
25
import com.vaadin.ui.CustomComponent;
26
import com.vaadin.ui.Notification;
27
import com.vaadin.ui.Panel;
28
import com.vaadin.ui.PasswordField;
29
import com.vaadin.ui.TextField;
30
import com.vaadin.ui.UI;
31
import com.vaadin.ui.VerticalLayout;
32

    
33
import eu.etaxonomy.cdm.common.URI;
34
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
35
import eu.etaxonomy.cdm.vaadin.ui.AbstractAuthenticatedUI;
36

    
37
@SpringView
38
public class AuthenticationView
39
        extends CustomComponent
40
        implements IAuthenticationComponent, ClickListener , View {
41

    
42
	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */
43

    
44
    private static final long serialVersionUID = -4417412061924225281L;
45

    
46
    @AutoGenerated
47
	private VerticalLayout mainLayout;
48
	@AutoGenerated
49
	private Panel loginPanel;
50
	@AutoGenerated
51
	private VerticalLayout loginPanelLayout;
52
	@AutoGenerated
53
	private Button loginBtn;
54
	@AutoGenerated
55
	private PasswordField passwordField;
56
	@AutoGenerated
57
	private TextField userNameTF;
58
	private AuthenticationComponentListener authListener;
59

    
60
	/**
61
	 * The constructor should first build the main layout, set the
62
	 * composition root and then do any custom initialization.
63
	 *
64
	 * The constructor will not be automatically regenerated by the
65
	 * visual editor.
66
	 */
67
	public AuthenticationView() {
68
		buildMainLayout();
69
		setStyleName("login");
70
		setCompositionRoot(mainLayout);
71
		userNameTF.addValidator(new StringLengthValidator("It must be 3-25 characters", 3, 25, false));
72
		userNameTF.setNullRepresentation("");
73
		userNameTF.focus();
74

    
75
		authListener = new AuthenticationPresenter();
76

    
77
		loginBtn.addClickListener(this);
78
		loginBtn.setClickShortcut(KeyCode.ENTER, null);
79
	}
80

    
81
	@Override
82
	public void addListener(AuthenticationComponentListener listener) {
83
		this.authListener = listener;
84

    
85
	}
86

    
87
	@Override
88
	public void buttonClick(ClickEvent event) {
89
	    boolean isAuthenticated = false;
90
	    try {
91
	        isAuthenticated = authListener.login(new URI(Page.getCurrent().getLocation()),
92
	                VaadinServlet.getCurrent().getServletContext().getContextPath(),
93
	                userNameTF.getValue(),
94
	                passwordField.getValue());
95
	        if(isAuthenticated) {
96
	            // we are sure that since we are in the authentication view that the
97
	            // current ui should be of type AbstractAuthenticatedUI
98
	            AbstractAuthenticatedUI aaui = (AbstractAuthenticatedUI) UI.getCurrent();
99
	            UI.getCurrent().getNavigator().navigateTo(aaui.getFirstViewName());
100
	        }
101
	    } catch(BadCredentialsException e){
102
	        Notification.show("Bad credentials", Notification.Type.ERROR_MESSAGE);
103
	    }
104
	}
105

    
106
	@Override
107
	public void enter(ViewChangeEvent event) {
108
		// TODO Auto-generated method stub
109

    
110
	}
111

    
112
	@AutoGenerated
113
	private VerticalLayout buildMainLayout() {
114
		// common part: create layout
115
		mainLayout = new VerticalLayout();
116
		mainLayout.setImmediate(false);
117
		mainLayout.setWidth("100%");
118
		mainLayout.setHeight("100%");
119
		mainLayout.setMargin(false);
120

    
121
		// top-level component properties
122
		setWidth("100.0%");
123
		setHeight("100.0%");
124

    
125
		// loginPanel
126
		loginPanel = buildLoginPanel();
127
		mainLayout.addComponent(loginPanel);
128
		mainLayout.setExpandRatio(loginPanel, 1.0f);
129
		mainLayout.setComponentAlignment(loginPanel, new Alignment(48));
130

    
131
		return mainLayout;
132
	}
133

    
134
	@AutoGenerated
135
	private Panel buildLoginPanel() {
136
		// common part: create layout
137
		loginPanel = new Panel();
138
		loginPanel.setImmediate(false);
139
		loginPanel.setWidth("-1px");
140
		loginPanel.setHeight("-1px");
141

    
142
		// loginPanelLayout
143
		loginPanelLayout = buildLoginPanelLayout();
144
		loginPanel.setContent(loginPanelLayout);
145

    
146
		return loginPanel;
147
	}
148

    
149
	@AutoGenerated
150
	private VerticalLayout buildLoginPanelLayout() {
151
		// common part: create layout
152
		loginPanelLayout = new VerticalLayout();
153
		loginPanelLayout.setImmediate(false);
154
		loginPanelLayout.setWidth("-1px");
155
		loginPanelLayout.setHeight("-1px");
156
		loginPanelLayout.setMargin(true);
157
		loginPanelLayout.setSpacing(true);
158

    
159
		// userNameTF
160
		userNameTF = new TextFieldNFix();
161
		userNameTF.setCaption("User Name");
162
		userNameTF.setImmediate(false);
163
		userNameTF.setWidth("-1px");
164
		userNameTF.setHeight("-1px");
165
		userNameTF.setInvalidAllowed(false);
166
		userNameTF.setRequired(true);
167
		loginPanelLayout.addComponent(userNameTF);
168
		loginPanelLayout.setComponentAlignment(userNameTF, new Alignment(48));
169

    
170
		// passwordField
171
		passwordField = new PasswordField();
172
		passwordField.setCaption("Password");
173
		passwordField.setImmediate(false);
174
		passwordField.setWidth("-1px");
175
		passwordField.setHeight("-1px");
176
		passwordField.setInvalidAllowed(false);
177
		passwordField.setRequired(true);
178
		loginPanelLayout.addComponent(passwordField);
179
		loginPanelLayout
180
				.setComponentAlignment(passwordField, new Alignment(48));
181

    
182
		// loginBtn
183
		loginBtn = new Button();
184
		loginBtn.setCaption("Login");
185
		loginBtn.setImmediate(true);
186
		loginBtn.setWidth("-1px");
187
		loginBtn.setHeight("-1px");
188
		loginPanelLayout.addComponent(loginBtn);
189
		loginPanelLayout.setComponentAlignment(loginBtn, new Alignment(48));
190

    
191
		return loginPanelLayout;
192
	}
193
}
(7-7/18)