Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / CredentialsDialog.java @ 83c3bc8e

History | View | Annotate | Download (11.3 kB)

1
/*
2
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package org.gss_project.gss.web.client;
20

    
21
import com.google.gwt.user.client.Window;
22
import org.gss_project.gss.web.client.rest.PostCommand;
23
import org.gss_project.gss.web.client.rest.RestException;
24

    
25
import com.google.gwt.core.client.GWT;
26
import com.google.gwt.dom.client.NativeEvent;
27
import com.google.gwt.event.dom.client.ClickEvent;
28
import com.google.gwt.event.dom.client.ClickHandler;
29
import com.google.gwt.event.dom.client.KeyCodes;
30
import com.google.gwt.user.client.DeferredCommand;
31
import com.google.gwt.user.client.Event.NativePreviewEvent;
32
import com.google.gwt.user.client.ui.AbstractImagePrototype;
33
import com.google.gwt.user.client.ui.Button;
34
import com.google.gwt.user.client.ui.DialogBox;
35
import com.google.gwt.user.client.ui.FlexTable;
36
import com.google.gwt.user.client.ui.HTML;
37
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
38
import com.google.gwt.user.client.ui.HorizontalPanel;
39
import com.google.gwt.user.client.ui.TextBox;
40
import com.google.gwt.user.client.ui.VerticalPanel;
41

    
42

    
43
/**
44
 * A dialog box that displays the user credentials for use in other client
45
 * applications, such as WebDAV clients.
46
 *
47
 * @author kman
48
 */
49
public class CredentialsDialog extends DialogBox {
50

    
51
        private final String WIDTH_FIELD = "35em";
52
        private final String WIDTH_TEXT = "42em";
53

    
54
        private TextBox passwordBox;
55

    
56
        /**
57
         * The 'confirm reset password' dialog box.
58
         */
59
        private class ConfirmResetPasswordDialog extends DialogBox {
60

    
61
                /**
62
                 * The widget's constructor.
63
                 *
64
                 * @param images the supplied images
65
                 */
66
                private ConfirmResetPasswordDialog(MessagePanel.Images images) {
67
                        // Set the dialog's caption.
68
                        setText("Confirmation");
69
                        setAnimationEnabled(true);
70
                        // Create a VerticalPanel to contain the label and the buttons.
71
                        VerticalPanel outer = new VerticalPanel();
72
                        HorizontalPanel buttons = new HorizontalPanel();
73

    
74
                        HTML text;
75
                        text = new HTML("<table><tr><td>" +
76
                                        AbstractImagePrototype.create(images.warn()).getHTML() +
77
                                        "</td><td>" + "Are you sure you want to create a new " +
78
                                        "WebDAV password?</td></tr></table>");
79
                        text.setStyleName("gss-warnMessage");
80
                        outer.add(text);
81

    
82
                        // Create the 'Yes' button, along with a listener that hides the
83
                        // dialog when the button is clicked and resets the password.
84
                        Button ok = new Button("Yes", new ClickHandler() {
85
                                @Override
86
                                public void onClick(ClickEvent event) {
87
                                        resetPassword(GSS.get().getCurrentUserResource().getUri());
88
                                        hide();
89
                                }
90
                        });
91
                        buttons.add(ok);
92
                        buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
93
                        // Create the 'No' button, along with a listener that hides the
94
                        // dialog when the button is clicked.
95
                        Button cancel = new Button("No", new ClickHandler() {
96
                                @Override
97
                                public void onClick(ClickEvent event) {
98
                                        hide();
99
                                }
100
                        });
101
                        buttons.add(cancel);
102
                        buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
103
                        buttons.setSpacing(8);
104
                        buttons.setStyleName("gss-warnMessage");
105
                        outer.setStyleName("gss-warnMessage");
106
                        outer.add(buttons);
107
                        outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
108
                        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
109
                        setWidget(outer);
110
                }
111

    
112
                @Override
113
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
114
                        super.onPreviewNativeEvent(preview);
115
                        NativeEvent evt = preview.getNativeEvent();
116
                        if (evt.getType().equals("keydown"))
117
                                // Use the popup's key preview hooks to close the dialog when either
118
                                // enter or escape is pressed.
119
                                switch (evt.getKeyCode()) {
120
                                        case KeyCodes.KEY_ENTER:
121
                                        case KeyCodes.KEY_ESCAPE:
122
                                                hide();
123
                                                break;
124
                                }
125
                }
126

    
127
        }
128

    
129
        private class ReauthenticateDialog extends DialogBox {
130
                /**
131
                 * The widget constructor.
132
                 */
133
                public ReauthenticateDialog() {
134
                        // Set the dialog's caption.
135
                        setText("New Password Created");
136
                        setAnimationEnabled(true);
137
                        VerticalPanel outer = new VerticalPanel();
138

    
139
                        // Create the text and set a style name so we can style it with CSS.
140
                        HTML text = new HTML("<p>A new WebDAV password has been created." +
141
                                        "</p><p>You will now be redirected to the initial screen" +
142
                                        " for the changes to take effect. Choose \"Show " +
143
                                        "Credentials\" again afterwards to see the new password.</p>");
144
                        text.setStyleName("gss-AboutText");
145
                        outer.add(text);
146

    
147
                        // Create the 'OK' button, along with a listener that hides the
148
                        // dialog when the button is clicked.
149
                        Button confirm = new Button("Proceed", new ClickHandler() {
150
                                @Override
151
                                public void onClick(ClickEvent event) {
152
                                        GSS.get().authenticateUser();
153
                                        hide();
154
                                }
155
                        });
156
                        outer.add(confirm);
157
                        outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
158
                        outer.setSpacing(8);
159
                        setWidget(outer);
160
                }
161

    
162
                @Override
163
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
164
                        super.onPreviewNativeEvent(preview);
165
                        NativeEvent evt = preview.getNativeEvent();
166
                        if (evt.getType().equals("keydown"))
167
                                // Use the popup's key preview hooks to close the dialog when
168
                                // either enter or escape is pressed.
169
                                switch (evt.getKeyCode()) {
170
                                        case KeyCodes.KEY_ENTER:
171
                                                GSS.get().authenticateUser();
172
                                                hide();
173
                                                break;
174
                                        case KeyCodes.KEY_ESCAPE:
175
                                                hide();
176
                                                break;
177
                                }
178
                }
179

    
180
        }
181

    
182
        /**
183
         * The widget constructor.
184
         */
185
        public CredentialsDialog(final MessagePanel.Images images) {
186
                // Set the dialog's caption.
187
                setText("User Credentials");
188
                setAnimationEnabled(true);
189
                // A VerticalPanel that contains the 'about' label and the 'OK' button.
190
                VerticalPanel outer = new VerticalPanel();
191
                Configuration conf = (Configuration) GWT.create(Configuration.class);
192
                String service = conf.serviceName();
193
        String path = Window.Location.getPath();
194
        String baseUrl = GWT.getModuleBaseURL();
195
        String homeUrl = baseUrl.substring(0, baseUrl.indexOf(path));
196
                String webdavUrl = homeUrl + conf.webdavUrl();
197
                String tokenNote = conf.tokenTTLNote();
198
                // Create the text and set a style name so we can style it with CSS.
199
                HTML text = new HTML("<p>These are the user credentials that are " +
200
                                "required for interacting with " + service + ". You can copy" +
201
                                " and paste the username and password in the WebDAV client " +
202
                                "in order to use " + service + " through the WebDAV " +
203
                                "interface, at:<br/> " + webdavUrl + "<br/>" + tokenNote +
204
                                "</p>");
205
                text.setStyleName("gss-AboutText");
206
                text.setWidth(WIDTH_TEXT);
207
                outer.add(text);
208
                FlexTable table = new FlexTable();
209
                table.setText(0, 0, "Username");
210
                table.setText(1, 0, "Password");
211
                table.setText(2, 0, "Token");
212
                TextBox username = new TextBox();
213
                final GSS app = GSS.get();
214
                username.setText(app.getCurrentUserResource().getUsername());
215
                username.setReadOnly(true);
216
                username.setWidth(WIDTH_FIELD);
217
                username.addClickHandler(new ClickHandler() {
218
                        @Override
219
                        public void onClick(ClickEvent event) {
220
                                GSS.enableIESelection();
221
                                ((TextBox) event.getSource()).selectAll();
222
                                GSS.preventIESelection();
223
                        }
224

    
225
                });
226
                table.setWidget(0, 1, username);
227
                passwordBox = new TextBox();
228
                passwordBox.setText(app.getWebDAVPassword());
229
                passwordBox.setReadOnly(true);
230
                passwordBox.setWidth(WIDTH_FIELD);
231
                passwordBox.addClickHandler(new  ClickHandler() {
232
                        @Override
233
                        public void onClick(ClickEvent event) {
234
                                GSS.enableIESelection();
235
                                ((TextBox) event.getSource()).selectAll();
236
                                GSS.preventIESelection();
237
                        }
238

    
239
                });
240
                table.setWidget(1, 1, passwordBox);
241

    
242
                TextBox tokenBox = new TextBox();
243
                tokenBox.setText(app.getToken());
244
                tokenBox.setReadOnly(true);
245
                tokenBox.setWidth(WIDTH_FIELD);
246
                tokenBox.addClickHandler(new ClickHandler() {
247
                        @Override
248
                        public void onClick(ClickEvent event) {
249
                                GSS.enableIESelection();
250
                                ((TextBox) event.getSource()).selectAll();
251
                                GSS.preventIESelection();
252
                        }
253

    
254
                });
255
                table.setWidget(2, 1, tokenBox);
256

    
257
                table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
258
                table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
259
                table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
260
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
261
                table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
262
                table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
263
                outer.add(table);
264

    
265
                // Create the 'OK' button, along with a listener that hides the dialog
266
                // when the button is clicked.
267
                Button confirm = new Button("Close", new ClickHandler() {
268
                        @Override
269
                        public void onClick(ClickEvent event) {
270
                                hide();
271
                        }
272
                });
273
                outer.add(confirm);
274
                outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
275

    
276
                // Create the 'Reset password' button, along with a listener that hides
277
                // the dialog when the button is clicked.
278
                Button resetPassword = new Button("Reset Password", new ClickHandler() {
279
                        @Override
280
                        public void onClick(ClickEvent event) {
281
                                ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
282
                                dlg.center();
283
                        }
284
                });
285
                outer.add(resetPassword);
286
                outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
287

    
288
                outer.setSpacing(8);
289
                setWidget(outer);
290
        }
291

    
292
        @Override
293
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
294
                super.onPreviewNativeEvent(preview);
295
                NativeEvent evt = preview.getNativeEvent();
296
                if (evt.getType().equals("keydown"))
297
                        // Use the popup's key preview hooks to close the dialog when
298
                        // either enter or escape is pressed.
299
                        switch (evt.getKeyCode()) {
300
                                case KeyCodes.KEY_ENTER:
301
                                case KeyCodes.KEY_ESCAPE:
302
                                        hide();
303
                                        break;
304
                        }
305
        }
306

    
307

    
308
        /**
309
         * Generate an RPC request to reset WebDAV password.
310
         *
311
         * @param userId the Uri of the user whose password will be reset
312
         */
313
        private void resetPassword(String userUri) {
314

    
315
                if (userUri == null || userUri.length() == 0) {
316
                        GSS.get().displayError("Empty user Uri!");
317
                        return;
318
                }
319
                GWT.log("resetPassword(" + userUri + ")", null);
320
                PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
321

    
322
                        @Override
323
                        public void onComplete() {
324
                                ReauthenticateDialog dlg = new ReauthenticateDialog();
325
                                dlg.center();
326
                        }
327

    
328
                        @Override
329
                        public void onError(Throwable t) {
330
                                GWT.log("", t);
331
                                if(t instanceof RestException){
332
                                        int statusCode = ((RestException)t).getHttpStatusCode();
333
                                        if(statusCode == 405)
334
                                                GSS.get().displayError("You don't have the necessary" +
335
                                                                " permissions");
336
                                        else if(statusCode == 404)
337
                                                GSS.get().displayError("Resource does not exist");
338
                                        else
339
                                                GSS.get().displayError("Unable to reset password:" +
340
                                                                        ((RestException)t).getHttpStatusText());
341
                                }
342
                                else
343
                                        GSS.get().displayError("System error resetting password:" +
344
                                                                t.getMessage());
345
                        }
346
                };
347
                DeferredCommand.addCommand(cg);
348
        }
349

    
350
}