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
|
|
30
|
import java.util.Date;
|
31
|
|
32
|
/**
|
33
|
* Created by stefania on 12/2/15.
|
34
|
*/
|
35
|
public class LoginPage {
|
36
|
|
37
|
private static LoginPage instance = null;
|
38
|
|
39
|
private Form userLoginForm = new Form();
|
40
|
|
41
|
private Alert successLabel = new Alert();
|
42
|
private Alert errorLabel = new Alert();
|
43
|
private TextBox email;
|
44
|
private PasswordTextBox password;
|
45
|
|
46
|
private SubmitButton login = new SubmitButton();
|
47
|
|
48
|
private Anchor forgotYourPassword = new Anchor();
|
49
|
private Paragraph paragraph = new Paragraph();
|
50
|
private Anchor register = new Anchor();
|
51
|
|
52
|
private UserServiceAsync userService = GWT.create(UserService.class);
|
53
|
private LoginListener loginListener;
|
54
|
|
55
|
private LoginPage() {
|
56
|
|
57
|
successLabel.setType(AlertType.SUCCESS);
|
58
|
successLabel.setVisible(false);
|
59
|
successLabel.setDismissable(false);
|
60
|
userLoginForm.add(successLabel);
|
61
|
|
62
|
errorLabel.setType(AlertType.DANGER);
|
63
|
errorLabel.setVisible(false);
|
64
|
errorLabel.setDismissable(false);
|
65
|
userLoginForm.add(errorLabel);
|
66
|
|
67
|
userLoginForm.addStyleName("m-t");
|
68
|
|
69
|
email = TextBox.wrap(DOM.getElementById("creds_username"));
|
70
|
email.getElement().setPropertyString("placeholder", "Username / Email");
|
71
|
email.addStyleName("form-control");
|
72
|
userLoginForm.add(new MyFormGroup(false, null, email));
|
73
|
|
74
|
password = PasswordTextBox.wrap(DOM.getElementById("creds_password"));
|
75
|
password.getElement().setPropertyString("placeholder", "Password");
|
76
|
password.addStyleName("form-control");
|
77
|
userLoginForm.add(new MyFormGroup(false, null, password));
|
78
|
|
79
|
login.setType(ButtonType.PRIMARY);
|
80
|
login.setText("Login");
|
81
|
login.addStyleName("block full-width m-b");
|
82
|
login.addClickHandler(new ClickHandler() {
|
83
|
@Override
|
84
|
public void onClick(ClickEvent event) {
|
85
|
|
86
|
successLabel.setVisible(false);
|
87
|
errorLabel.setVisible(false);
|
88
|
|
89
|
if(email.getValue().trim().isEmpty() || password.getValue().trim().isEmpty()) {
|
90
|
|
91
|
errorLabel.setText("Both username / email and password fields are required");
|
92
|
errorLabel.setVisible(true);
|
93
|
|
94
|
} else {
|
95
|
|
96
|
userService.login(email.getValue(), password.getValue(), new AsyncCallback<Tuple<UserProfile, String>>() {
|
97
|
|
98
|
@Override
|
99
|
public void onFailure(Throwable throwable) {
|
100
|
|
101
|
if (throwable instanceof UserAccessException) {
|
102
|
|
103
|
UserAccessException uae = (UserAccessException) throwable;
|
104
|
errorLabel.setText(uae.getMessage());
|
105
|
errorLabel.setVisible(true);
|
106
|
|
107
|
} else {
|
108
|
|
109
|
errorLabel.setText("Login failed - Something went wrong, please try again.");
|
110
|
errorLabel.setVisible(true);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public void onSuccess(Tuple<UserProfile, String> result) {
|
116
|
|
117
|
RepositoryManager.currentUser = result.getFirst();
|
118
|
RepositoryManager.currentUserRole = result.getSecond();
|
119
|
|
120
|
Date expireDate = new Date();
|
121
|
CalendarUtil.addDaysToDate(expireDate, 1);
|
122
|
Cookies.setCookie("currentUser", Crypto.encrypt(result.getFirst().getEmail()), expireDate);
|
123
|
|
124
|
if (loginListener != null)
|
125
|
loginListener.loginSucceeded(result.getFirst());
|
126
|
}
|
127
|
});
|
128
|
}
|
129
|
}
|
130
|
});
|
131
|
|
132
|
userLoginForm.add(login);
|
133
|
|
134
|
forgotYourPassword.setHTML("<small>Forgot password?</small>");
|
135
|
userLoginForm.add(forgotYourPassword);
|
136
|
|
137
|
paragraph.setHTML("<small>Do not have an account?</small>");
|
138
|
paragraph.addStyleName("text-muted text-center");
|
139
|
userLoginForm.add(paragraph);
|
140
|
|
141
|
register.setHref("#register");
|
142
|
register.setText("Create an account");
|
143
|
register.addStyleName("btn btn-sm btn-white btn-block");
|
144
|
userLoginForm.add(register);
|
145
|
|
146
|
RootPanel.get("loginForm").add(userLoginForm);
|
147
|
}
|
148
|
|
149
|
public static final LoginPage getInstance() {
|
150
|
|
151
|
if(instance==null)
|
152
|
instance = new LoginPage();
|
153
|
|
154
|
return instance;
|
155
|
}
|
156
|
|
157
|
public void showLoginPage() {
|
158
|
|
159
|
Document.get().getElementById("landingPage").getStyle().setDisplay(Style.Display.NONE);
|
160
|
Document.get().getElementById("register").getStyle().setDisplay(Style.Display.NONE);
|
161
|
Document.get().getElementById("wrapper").getStyle().setDisplay(Style.Display.NONE);
|
162
|
Document.get().getElementById("login").getStyle().setDisplay(Style.Display.BLOCK);
|
163
|
|
164
|
// Document.get().getBody().removeClassName("landing-page");
|
165
|
// Document.get().getBody().addClassName("gray-bg");
|
166
|
}
|
167
|
|
168
|
public interface LoginListener {
|
169
|
void loginSucceeded(UserProfile userProfile);
|
170
|
}
|
171
|
|
172
|
public void setLoginListener(LoginListener loginListener) {
|
173
|
this.loginListener = loginListener;
|
174
|
}
|
175
|
|
176
|
public void showSuccessfulMessage(String message) {
|
177
|
|
178
|
Window.alert("showing successful activation message");
|
179
|
|
180
|
successLabel.setText(message);
|
181
|
successLabel.setVisible(true);
|
182
|
}
|
183
|
}
|