Project

General

Profile

1
package eu.dnetlib.client;
2

    
3
import com.google.gwt.core.client.EntryPoint;
4
import com.google.gwt.core.client.GWT;
5
import com.google.gwt.event.logical.shared.ValueChangeEvent;
6
import com.google.gwt.event.logical.shared.ValueChangeHandler;
7
import com.google.gwt.user.client.Cookies;
8
import com.google.gwt.user.client.History;
9
import com.google.gwt.user.client.Window;
10
import com.google.gwt.user.client.rpc.AsyncCallback;
11
import com.google.gwt.user.client.ui.RootPanel;
12
import eu.dnetlib.goldoa.domain.Role;
13
import eu.dnetlib.goldoa.domain.User;
14
import eu.dnetlib.goldoa.domain.UserRole;
15
import eu.dnetlib.shared.InitLoad;
16

    
17
import java.util.ArrayList;
18
import java.util.List;
19

    
20

    
21
public class GoldOAPortal implements EntryPoint {
22

    
23
    public static User currentUser = null;
24

    
25
    public static String publicCaptchaKey = "";
26

    
27
    public static String currentToken = "home";
28
    public static String previousToken = "home";
29

    
30
    public static List<Role> userRoles = new ArrayList<>();
31

    
32
    private DataServiceAsync dataService = GWT.create(DataService.class);
33

    
34
    public void onModuleLoad() {
35

    
36
        RootPanel.get("loginRegister").add(RegisterLoginElement.getInstance());
37

    
38
        History.addValueChangeHandler(new ValueChangeHandler<String>() {
39
            @Override
40
            public void onValueChange(ValueChangeEvent<String> event) {
41

    
42
                String historyToken = event.getValue();
43

    
44
                if(historyToken!=null && !historyToken.equals("")) {
45

    
46
                    String finalHistoryToken = getFinalHistoryToken(historyToken);
47

    
48
                    History.newItem(finalHistoryToken);
49
                    NavigationManager.getInstance().navigate(finalHistoryToken);
50

    
51
                } else {
52

    
53
                    //TODO kapou peftei se loop
54
                    History.newItem("home");
55
                    NavigationManager.getInstance().navigate("home");
56
                }
57
            }
58
        });
59

    
60
        String hash = Window.Location.getHash();
61
        String email = Window.Location.getParameter("email");
62
        String token = Window.Location.getParameter("token");
63
        String encryptedEmail = Cookies.getCookie("currentUser");
64
        String cookieEmail = null;
65
        if(encryptedEmail!=null)
66
            cookieEmail = Crypto.decrypt(encryptedEmail);
67

    
68
        Window.alert(hash + "/" + email +  "/" + token +  "/" + cookieEmail  );
69

    
70
        dataService.load(hash, email, token, cookieEmail, new AsyncCallback<InitLoad>() {
71

    
72
                    @Override
73
                    public void onFailure(Throwable throwable) {
74
                        //TODO system error
75
                        Window.alert("Load error!");
76
                    }
77

    
78
                    @Override
79
                    public void onSuccess(InitLoad initLoad) {
80

    
81
                        GoldOAPortal.publicCaptchaKey = initLoad.getPublicCaptchaKey();
82

    
83
                        GoldOAPortal.currentUser = null;
84
                        Cookies.removeCookie("currentUser");
85
                        GoldOAPortal.userRoles = initLoad.getUserRoles();
86

    
87
                        if(initLoad.getPerson()!=null) {
88

    
89
                            //TODO if it was activate, remove parameters from url
90

    
91
                            GoldOAPortal.currentUser = initLoad.getPerson();
92
                            Cookies.setCookie("currentUser", Crypto.encrypt(initLoad.getPerson().getEmail()));
93

    
94
                            UserInfoElement userInfoElement = new UserInfoElement();
95
                            RootPanel.get("loginRegister").clear();
96
                            RootPanel.get("loginRegister").add(userInfoElement.asWidget());
97

    
98
                            NavigationManager.getInstance().update();
99

    
100
                            String finalHistoryToken = getFinalHistoryToken(initLoad.getGoTo());
101
                            History.newItem(finalHistoryToken);
102
                            NavigationManager.getInstance().navigate(finalHistoryToken);
103

    
104
                        } else {
105

    
106
                            if(initLoad.getErrorMessage()!=null) {
107

    
108
                                NavigationManager.getInstance().update();
109

    
110
                                String finalHistoryToken = getFinalHistoryToken(initLoad.getGoTo());
111
                                History.newItem(finalHistoryToken);
112
                                NavigationManager.getInstance().navigate(finalHistoryToken);
113
                                //TODO display error message
114

    
115
                            } else {
116

    
117
                                NavigationManager.getInstance().update();
118

    
119
                                String finalHistoryToken = getFinalHistoryToken(initLoad.getGoTo());
120
                                History.newItem(finalHistoryToken);
121
                                NavigationManager.getInstance().navigate(finalHistoryToken);
122
                            }
123
                        }
124
                    }
125
                });
126
    }
127

    
128
    private String getFinalHistoryToken(String historyToken) {
129

    
130
        if(GoldOAPortal.currentUser==null) {
131
            if(!(historyToken.equals("home") || historyToken.equals("support") || historyToken.equals("eligibleProjects")
132
                    || historyToken.equals("login") || historyToken.equals("register") || historyToken.equals("statistics")
133
                    || historyToken.equals("forgotYourPassword"))) {
134
                historyToken = "home";
135
            }
136
        } else {
137

    
138
            boolean isModeratorOrAdmin = false;
139
            boolean isLibrarian = false;
140
            boolean isAdminOrAccountingOfficer = false;
141

    
142
            for(UserRole personRole : GoldOAPortal.currentUser.getRoles()) {
143
                if(personRole.getPk().getRole().getId().equals("moderator") && personRole.isApproved()) {
144
                    isModeratorOrAdmin = true;
145
                }
146
                if(personRole.getPk().getRole().getId().equals("administrator") && personRole.isApproved()) {
147
                    isModeratorOrAdmin = true;
148
                    isAdminOrAccountingOfficer = true;
149
                }
150
                if(personRole.getPk().getRole().getId().equals("accounting") && personRole.isApproved()) {
151
                    isAdminOrAccountingOfficer = true;
152
                }
153
                if(personRole.getPk().getRole().getId().equals("library_staff") && personRole.isApproved()) {
154
                    isLibrarian = true;
155
                }
156
            }
157

    
158
            if(!Utils.currentUserHasRoleApproved("administrator") && historyToken.equals("monitorHelpTexts"))
159
                historyToken = "home";
160

    
161
            if((!isModeratorOrAdmin && historyToken.equals("monitorFundingRequests"))
162
                    || (!isModeratorOrAdmin && historyToken.equals("monitorBudgets"))
163
                    || (!isModeratorOrAdmin && historyToken.equals("monitorUsers"))
164
                    || (!isModeratorOrAdmin && historyToken.equals("monitorUserRoles"))
165
                    || (!isModeratorOrAdmin && historyToken.equals("monitor"))) {
166
                historyToken = "home";
167
            }
168

    
169
            if((!isAdminOrAccountingOfficer && historyToken.equals("accountingMonitor"))
170
                    || (!isAdminOrAccountingOfficer && historyToken.equals("accountingMonitorFundingRequests"))
171
                    || (!isAdminOrAccountingOfficer && historyToken.equals("accountingMonitorBudgets"))) {
172
                historyToken = "home";
173
            }
174

    
175
            if((!isLibrarian && historyToken.equals("newBudgetRequest"))
176
                    || (!isLibrarian && historyToken.equals("existingBudgetRequests"))) {
177
                historyToken = "home";
178
            }
179
        }
180

    
181
        return historyToken;
182
    }
183
}
(11-11/22)