1
|
package eu.dnetlib.repo.manager.client;
|
2
|
|
3
|
import com.google.gwt.core.client.EntryPoint;
|
4
|
import com.google.gwt.core.client.GWT;
|
5
|
import com.google.gwt.dom.client.Document;
|
6
|
import com.google.gwt.dom.client.Style;
|
7
|
import com.google.gwt.event.logical.shared.ValueChangeEvent;
|
8
|
import com.google.gwt.event.logical.shared.ValueChangeHandler;
|
9
|
import com.google.gwt.query.client.Function;
|
10
|
import com.google.gwt.user.client.Cookies;
|
11
|
import com.google.gwt.user.client.Event;
|
12
|
import com.google.gwt.user.client.History;
|
13
|
import com.google.gwt.user.client.Window;
|
14
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
15
|
import eu.dnetlib.domain.functionality.UserProfile;
|
16
|
import eu.dnetlib.repo.manager.client.services.UserService;
|
17
|
import eu.dnetlib.repo.manager.client.services.UserServiceAsync;
|
18
|
import eu.dnetlib.repo.manager.shared.Tuple;
|
19
|
|
20
|
import javax.servlet.http.Cookie;
|
21
|
import java.util.Date;
|
22
|
import java.util.logging.Level;
|
23
|
import java.util.logging.Logger;
|
24
|
|
25
|
import static com.google.gwt.query.client.GQuery.$;
|
26
|
|
27
|
|
28
|
public class RepositoryManager implements EntryPoint {
|
29
|
|
30
|
public static UserProfile currentUser = null;
|
31
|
public static String currentUserRole = null;
|
32
|
|
33
|
public static String publicCaptchaKey = "";
|
34
|
|
35
|
public static String currentToken = "landing";
|
36
|
public static String previousToken = "landing";
|
37
|
|
38
|
private UserServiceAsync userService = GWT.create(UserService.class);
|
39
|
|
40
|
public void onModuleLoad() {
|
41
|
|
42
|
GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
|
43
|
|
44
|
Logger logger = Logger.getLogger("UncaughtException");
|
45
|
|
46
|
@Override
|
47
|
public void onUncaughtException(Throwable arg0) {
|
48
|
|
49
|
logger.log(Level.SEVERE, "Exception: ", arg0);
|
50
|
}
|
51
|
});
|
52
|
|
53
|
History.addValueChangeHandler(new ValueChangeHandler<String>() {
|
54
|
@Override
|
55
|
public void onValueChange(ValueChangeEvent<String> event) {
|
56
|
String historyToken = event.getValue();
|
57
|
if(historyToken!=null && !historyToken.equals("")) {
|
58
|
String finalHistoryToken = getFinalHistoryToken(historyToken);
|
59
|
History.newItem(finalHistoryToken);
|
60
|
NavigationManager.getInstance().navigate(finalHistoryToken);
|
61
|
}else {
|
62
|
//TODO kapou peftei se loop
|
63
|
History.newItem("landing");
|
64
|
NavigationManager.getInstance().navigate("landing");
|
65
|
}
|
66
|
}
|
67
|
});
|
68
|
checkCookie("dashboard");
|
69
|
addLogoutHandler();
|
70
|
}
|
71
|
|
72
|
private void checkCookie(final String nextPage) {
|
73
|
userService.checkCookie(new AsyncCallback<Tuple<UserProfile, String>>() {
|
74
|
@Override
|
75
|
public void onFailure(Throwable throwable) {
|
76
|
Cookies.setCookie("currentUser","",new Date(0));
|
77
|
History.newItem("landing");
|
78
|
NavigationManager.getInstance().navigate("landing");
|
79
|
}
|
80
|
@Override
|
81
|
public void onSuccess(Tuple<UserProfile, String> result) {
|
82
|
RepositoryManager.currentUser = result.getFirst();
|
83
|
RepositoryManager.currentUserRole = result.getSecond();
|
84
|
|
85
|
if (RepositoryManager.currentUser != null)
|
86
|
Document.get().getElementById("currentUser").setInnerText(RepositoryManager.currentUser.getFirstname()
|
87
|
+ " " + RepositoryManager.currentUser.getLastname());
|
88
|
|
89
|
//TODO use role instead of email
|
90
|
if(RepositoryManager.currentUserRole.equals("admin")) {
|
91
|
Document.get().getElementById("admin1").getStyle().setDisplay(Style.Display.BLOCK);
|
92
|
Document.get().getElementById("admin2").getStyle().setDisplay(Style.Display.BLOCK);
|
93
|
}
|
94
|
History.newItem(nextPage);
|
95
|
NavigationManager.getInstance().navigate(nextPage);
|
96
|
}
|
97
|
});
|
98
|
}
|
99
|
|
100
|
private void addLogoutHandler() {
|
101
|
|
102
|
$(".logoutLink").click(new Function() {
|
103
|
|
104
|
public boolean f(Event e) {
|
105
|
|
106
|
Cookies.setCookie("currentUser","",new Date(0));
|
107
|
|
108
|
userService.clearCookie(new AsyncCallback<Void>() {
|
109
|
@Override
|
110
|
public void onFailure(Throwable throwable) {
|
111
|
Window.alert("Fail to clear cookie!");
|
112
|
}
|
113
|
|
114
|
@Override
|
115
|
public void onSuccess(Void aVoid) {
|
116
|
RepositoryManager.currentUser = null;
|
117
|
RepositoryManager.currentUserRole = null;
|
118
|
Window.Location.replace("https://aai.openminted.eu/proxy/saml2/idp/SingleLogoutService.php?ReturnTo="+
|
119
|
GWT.getHostPageBaseURL() + "#landing");
|
120
|
}
|
121
|
});
|
122
|
return false;
|
123
|
}
|
124
|
});
|
125
|
}
|
126
|
|
127
|
private String getFinalHistoryToken(String historyToken) {
|
128
|
|
129
|
if(RepositoryManager.currentUser==null) {
|
130
|
|
131
|
if(!(historyToken.equals("landing") || historyToken.equals("login") || historyToken.equals("register"))) {
|
132
|
historyToken = "landing";
|
133
|
}
|
134
|
|
135
|
} else if(!RepositoryManager.currentUserRole.equals("admin")) {
|
136
|
if(historyToken.equals("admin/helpTexts") || historyToken.equals("admin/metrics")) {
|
137
|
historyToken = "dashboard";
|
138
|
}
|
139
|
}
|
140
|
|
141
|
return historyToken;
|
142
|
}
|
143
|
}
|