aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/minitutorials/v70/SimpleLoginUI.java
blob: f2f9c1b657aab17bf8c8417b084a8bf4c666b61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.vaadin.tests.minitutorials.v70;

import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;

public class SimpleLoginUI extends UI {

    @Override
    protected void init(VaadinRequest request) {

        /*
         * Create a new instance of the navigator. The navigator will attach
         * itself automatically to this view.
         */
        new Navigator(this, this);

        /*
         * The initial log view where the user can login to the application
         */
        getNavigator().addView(SimpleLoginView.NAME, SimpleLoginView.class);

        /*
         * Add the main view of the application
         */
        getNavigator().addView(SimpleLoginMainView.NAME,
                SimpleLoginMainView.class);

        /*
         * We use a view change handler to ensure the user is always redirected
         * to the login view if the user is not logged in.
         */
        getNavigator().addViewChangeListener(new ViewChangeListener() {

            @Override
            public boolean beforeViewChange(ViewChangeEvent event) {

                // Check if a user has logged in
                boolean isLoggedIn = getSession().getAttribute("user") != null;
                boolean isLoginView = event
                        .getNewView() instanceof SimpleLoginView;

                if (!isLoggedIn && !isLoginView) {
                    // Redirect to login view always if a user has not yet
                    // logged in
                    getNavigator().navigateTo(SimpleLoginView.NAME);
                    return false;

                } else if (isLoggedIn && isLoginView) {
                    // If someone tries to access to login view while logged in,
                    // then cancel
                    return false;
                }

                return true;
            }

        });
    }
}