Project

General

Profile

1
package eu.dnetlib.repo.manager.client;
2

    
3
import com.google.gwt.core.client.GWT;
4
import com.google.gwt.dom.client.Document;
5
import com.google.gwt.dom.client.Style;
6
import com.google.gwt.event.dom.client.ClickEvent;
7
import com.google.gwt.event.dom.client.ClickHandler;
8
import com.google.gwt.user.client.Cookies;
9
import com.google.gwt.user.client.DOM;
10
import com.google.gwt.user.client.Window;
11
import com.google.gwt.user.client.rpc.AsyncCallback;
12
import com.google.gwt.user.client.ui.PasswordTextBox;
13
import com.google.gwt.user.client.ui.RootPanel;
14
import com.google.gwt.user.client.ui.TextBox;
15
import com.google.gwt.user.datepicker.client.CalendarUtil;
16
import eu.dnetlib.domain.functionality.UserProfile;
17
import eu.dnetlib.gwt.client.MyFormGroup;
18
import eu.dnetlib.repo.manager.client.services.UserService;
19
import eu.dnetlib.repo.manager.client.services.UserServiceAsync;
20
import eu.dnetlib.repo.manager.shared.Tuple;
21
import eu.dnetlib.repo.manager.shared.UserAccessException;
22
import org.gwtbootstrap3.client.ui.Alert;
23
import org.gwtbootstrap3.client.ui.Anchor;
24
import org.gwtbootstrap3.client.ui.Form;
25
import org.gwtbootstrap3.client.ui.SubmitButton;
26
import org.gwtbootstrap3.client.ui.constants.AlertType;
27
import org.gwtbootstrap3.client.ui.constants.ButtonType;
28
import org.gwtbootstrap3.client.ui.html.Paragraph;
29
import org.springframework.beans.factory.annotation.Value;
30

    
31
import java.io.IOException;
32
import java.net.HttpURLConnection;
33
import java.net.URL;
34
import java.util.Date;
35

    
36
import static org.springframework.http.HttpHeaders.USER_AGENT;
37

    
38
/**
39
 * Created by stefania on 12/2/15.
40
 */
41
public class LoginPage {
42

    
43
    private static LoginPage instance = null;
44

    
45
    private Form userLoginForm = new Form();
46

    
47
    private Alert successLabel = new Alert();
48
    private Alert errorLabel = new Alert();
49
    private TextBox email;
50
    private PasswordTextBox password;
51

    
52
    private SubmitButton login = new SubmitButton();
53

    
54
    private Anchor forgotYourPassword = new Anchor();
55
    private Paragraph paragraph = new Paragraph();
56
    private Anchor register = new Anchor();
57

    
58
    private UserServiceAsync userService = GWT.create(UserService.class);
59
    private LoginListener loginListener;
60

    
61
    @Value("${oidc.issuer}")
62
    private String oidcURL;
63

    
64
    private LoginPage() {
65

    
66
        successLabel.setType(AlertType.SUCCESS);
67
        successLabel.setVisible(false);
68
        successLabel.setDismissable(false);
69
        userLoginForm.add(successLabel);
70

    
71
        errorLabel.setType(AlertType.DANGER);
72
        errorLabel.setVisible(false);
73
        errorLabel.setDismissable(false);
74
        userLoginForm.add(errorLabel);
75

    
76
        userLoginForm.addStyleName("m-t");
77

    
78
        email = TextBox.wrap(DOM.getElementById("creds_username"));
79
        email.getElement().setPropertyString("placeholder", "Username / Email");
80
        email.addStyleName("form-control");
81
        userLoginForm.add(new MyFormGroup(false, null, email));
82

    
83
        password = PasswordTextBox.wrap(DOM.getElementById("creds_password"));
84
        password.getElement().setPropertyString("placeholder", "Password");
85
        password.addStyleName("form-control");
86
        userLoginForm.add(new MyFormGroup(false, null, password));
87

    
88
        login.setType(ButtonType.PRIMARY);
89
        login.setText("Login");
90
        login.addStyleName("block full-width m-b");
91
        login.addClickHandler(new ClickHandler() {
92
            @Override
93
            public void onClick(ClickEvent event) {
94

    
95
                successLabel.setVisible(false);
96
                errorLabel.setVisible(false);
97

    
98
                if(email.getValue().trim().isEmpty() || password.getValue().trim().isEmpty()) {
99

    
100
                    errorLabel.setText("Both username / email and password fields are required");
101
                    errorLabel.setVisible(true);
102

    
103
                } else {
104

    
105
                    userService.login(email.getValue(), password.getValue(), new AsyncCallback<Tuple<UserProfile, String>>() {
106

    
107
                        @Override
108
                        public void onFailure(Throwable throwable) {
109

    
110
                            if (throwable instanceof UserAccessException) {
111

    
112
                                UserAccessException uae = (UserAccessException) throwable;
113
                                errorLabel.setText(uae.getMessage());
114
                                errorLabel.setVisible(true);
115

    
116
                            } else {
117

    
118
                                errorLabel.setText("Login failed - Something went wrong, please try again.");
119
                                errorLabel.setVisible(true);
120
                            }
121
                        }
122

    
123
                        @Override
124
                        public void onSuccess(Tuple<UserProfile, String> result) {
125

    
126
                            RepositoryManager.currentUser = result.getFirst();
127
                            RepositoryManager.currentUserRole = result.getSecond();
128

    
129
                            Date expireDate = new Date();
130
                            CalendarUtil.addDaysToDate(expireDate, 1);
131
                            Cookies.setCookie("currentUser", Crypto.encrypt(result.getFirst().getEmail()), expireDate);
132

    
133
                            if (loginListener != null)
134
                                loginListener.loginSucceeded(result.getFirst());
135
                        }
136
                    });
137
                }
138
            }
139
        });
140

    
141
        userLoginForm.add(login);
142

    
143
        forgotYourPassword.setHTML("<small>Forgot password?</small>");
144
        userLoginForm.add(forgotYourPassword);
145

    
146
        paragraph.setHTML("<small>Do not have an account?</small>");
147
        paragraph.addStyleName("text-muted text-center");
148
        userLoginForm.add(paragraph);
149

    
150
        register.setHref("#register");
151
        register.setText("Create an account");
152
        register.addStyleName("btn btn-sm btn-white btn-block");
153
        userLoginForm.add(register);
154

    
155
        RootPanel.get("loginForm").add(userLoginForm);
156
    }
157

    
158
    public static final LoginPage getInstance() {
159

    
160
        if(instance==null)
161
            instance = new LoginPage();
162

    
163
        return instance;
164
    }
165

    
166
    public void showLoginPage() {
167

    
168
        Document.get().getElementById("landingPage").getStyle().setDisplay(Style.Display.NONE);
169
        Document.get().getElementById("register").getStyle().setDisplay(Style.Display.NONE);
170
        Document.get().getElementById("wrapper").getStyle().setDisplay(Style.Display.NONE);
171
        Document.get().getElementById("login").getStyle().setDisplay(Style.Display.BLOCK);
172

    
173
//        Document.get().getBody().removeClassName("landing-page");
174
//        Document.get().getBody().addClassName("gray-bg");
175
    }
176

    
177
    public interface LoginListener {
178
        void loginSucceeded(UserProfile userProfile);
179
    }
180

    
181
    public void setLoginListener(LoginListener loginListener) {
182
        this.loginListener = loginListener;
183
    }
184

    
185
    public void showSuccessfulMessage(String message) {
186

    
187
        Window.alert("showing successful activation message");
188

    
189
        successLabel.setText(message);
190
        successLabel.setVisible(true);
191
    }
192
}
(5-5/12)