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