Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / CredentialsDialog.java @ 63366925

History | View | Annotate | Download (11 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import com.google.gwt.user.client.Window;
38
import gr.grnet.pithos.web.client.rest.PostCommand;
39
import gr.grnet.pithos.web.client.rest.RestException;
40

    
41
import com.google.gwt.core.client.GWT;
42
import com.google.gwt.dom.client.NativeEvent;
43
import com.google.gwt.event.dom.client.ClickEvent;
44
import com.google.gwt.event.dom.client.ClickHandler;
45
import com.google.gwt.event.dom.client.KeyCodes;
46
import com.google.gwt.user.client.DeferredCommand;
47
import com.google.gwt.user.client.Event.NativePreviewEvent;
48
import com.google.gwt.user.client.ui.AbstractImagePrototype;
49
import com.google.gwt.user.client.ui.Button;
50
import com.google.gwt.user.client.ui.DialogBox;
51
import com.google.gwt.user.client.ui.FlexTable;
52
import com.google.gwt.user.client.ui.HTML;
53
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54
import com.google.gwt.user.client.ui.HorizontalPanel;
55
import com.google.gwt.user.client.ui.TextBox;
56
import com.google.gwt.user.client.ui.VerticalPanel;
57

    
58

    
59
/**
60
 * A dialog box that displays the user credentials for use in other client
61
 * applications, such as WebDAV clients.
62
 */
63
public class CredentialsDialog extends DialogBox {
64

    
65
        private final String WIDTH_FIELD = "35em";
66
        private final String WIDTH_TEXT = "42em";
67

    
68
        /**
69
         * The 'confirm reset password' dialog box.
70
         */
71
        private class ConfirmResetPasswordDialog extends DialogBox {
72

    
73
                /**
74
                 * The widget's constructor.
75
                 *
76
                 * @param images the supplied images
77
                 */
78
                private ConfirmResetPasswordDialog(MessagePanel.Images images) {
79
                        // Set the dialog's caption.
80
                        setText("Confirmation");
81
                        setAnimationEnabled(true);
82
                        // Create a VerticalPanel to contain the label and the buttons.
83
                        VerticalPanel outer = new VerticalPanel();
84
                        HorizontalPanel buttons = new HorizontalPanel();
85

    
86
                        HTML text;
87
                        text = new HTML("<table><tr><td>" +
88
                                        AbstractImagePrototype.create(images.warn()).getHTML() +
89
                                        "</td><td>" + "Are you sure you want to create a new " +
90
                                        "WebDAV password?</td></tr></table>");
91
                        text.setStyleName("pithos-warnMessage");
92
                        outer.add(text);
93

    
94
                        // Create the 'Yes' button, along with a listener that hides the
95
                        // dialog when the button is clicked and resets the password.
96
                        Button ok = new Button("Yes", new ClickHandler() {
97
                                @Override
98
                                public void onClick(ClickEvent event) {
99
                                        resetPassword(GSS.get().getCurrentUserResource().getUri());
100
                                        hide();
101
                                }
102
                        });
103
                        buttons.add(ok);
104
                        buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
105
                        // Create the 'No' button, along with a listener that hides the
106
                        // dialog when the button is clicked.
107
                        Button cancel = new Button("No", new ClickHandler() {
108
                                @Override
109
                                public void onClick(ClickEvent event) {
110
                                        hide();
111
                                }
112
                        });
113
                        buttons.add(cancel);
114
                        buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
115
                        buttons.setSpacing(8);
116
                        buttons.setStyleName("pithos-warnMessage");
117
                        outer.setStyleName("pithos-warnMessage");
118
                        outer.add(buttons);
119
                        outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
120
                        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
121
                        setWidget(outer);
122
                }
123

    
124
                @Override
125
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
126
                        super.onPreviewNativeEvent(preview);
127
                        NativeEvent evt = preview.getNativeEvent();
128
                        if (evt.getType().equals("keydown"))
129
                                // Use the popup's key preview hooks to close the dialog when either
130
                                // enter or escape is pressed.
131
                                switch (evt.getKeyCode()) {
132
                                        case KeyCodes.KEY_ENTER:
133
                                        case KeyCodes.KEY_ESCAPE:
134
                                                hide();
135
                                                break;
136
                                }
137
                }
138

    
139
        }
140

    
141
        private class ReauthenticateDialog extends DialogBox {
142
                /**
143
                 * The widget constructor.
144
                 */
145
                public ReauthenticateDialog() {
146
                        // Set the dialog's caption.
147
                        setText("New Password Created");
148
                        setAnimationEnabled(true);
149
                        VerticalPanel outer = new VerticalPanel();
150

    
151
                        // Create the text and set a style name so we can style it with CSS.
152
                        HTML text = new HTML("<p>A new WebDAV password has been created." +
153
                                        "</p><p>You will now be redirected to the initial screen" +
154
                                        " for the changes to take effect. Choose \"Show " +
155
                                        "Credentials\" again afterwards to see the new password.</p>");
156
                        text.setStyleName("pithos-AboutText");
157
                        outer.add(text);
158

    
159
                        // Create the 'OK' button, along with a listener that hides the
160
                        // dialog when the button is clicked.
161
                        Button confirm = new Button("Proceed", new ClickHandler() {
162
                                @Override
163
                                public void onClick(ClickEvent event) {
164
                                        GSS.get().authenticateUser();
165
                                        hide();
166
                                }
167
                        });
168
                        outer.add(confirm);
169
                        outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
170
                        outer.setSpacing(8);
171
                        setWidget(outer);
172
                }
173

    
174
                @Override
175
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
176
                        super.onPreviewNativeEvent(preview);
177
                        NativeEvent evt = preview.getNativeEvent();
178
                        if (evt.getType().equals("keydown"))
179
                                // Use the popup's key preview hooks to close the dialog when
180
                                // either enter or escape is pressed.
181
                                switch (evt.getKeyCode()) {
182
                                        case KeyCodes.KEY_ENTER:
183
                                                GSS.get().authenticateUser();
184
                                                hide();
185
                                                break;
186
                                        case KeyCodes.KEY_ESCAPE:
187
                                                hide();
188
                                                break;
189
                                }
190
                }
191

    
192
        }
193

    
194
        /**
195
         * The widget constructor.
196
         */
197
        public CredentialsDialog(final MessagePanel.Images images) {
198
                // Set the dialog's caption.
199
                setText("User Credentials");
200
                setAnimationEnabled(true);
201
                // A VerticalPanel that contains the 'about' label and the 'OK' button.
202
                VerticalPanel outer = new VerticalPanel();
203
                Configuration conf = (Configuration) GWT.create(Configuration.class);
204
                String service = conf.serviceName();
205
                // Create the text and set a style name so we can style it with CSS.
206
                HTML text = new HTML("<p>These are the user credentials that are " +
207
                                "required for interacting with " + service + ".");
208
                text.setStyleName("pithos-AboutText");
209
                text.setWidth(WIDTH_TEXT);
210
                outer.add(text);
211
                FlexTable table = new FlexTable();
212
                table.setText(0, 0, "Username");
213
                table.setText(1, 0, "Token");
214
                TextBox username = new TextBox();
215
                final GSS app = GSS.get();
216
                username.setText(app.getCurrentUserResource().getUsername());
217
                username.setReadOnly(true);
218
                username.setWidth(WIDTH_FIELD);
219
                username.addClickHandler(new ClickHandler() {
220
                        @Override
221
                        public void onClick(ClickEvent event) {
222
                                GSS.enableIESelection();
223
                                ((TextBox) event.getSource()).selectAll();
224
                                GSS.preventIESelection();
225
                        }
226

    
227
                });
228
                table.setWidget(0, 1, username);
229

    
230
                TextBox tokenBox = new TextBox();
231
                tokenBox.setText(app.getToken());
232
                tokenBox.setReadOnly(true);
233
                tokenBox.setWidth(WIDTH_FIELD);
234
                tokenBox.addClickHandler(new ClickHandler() {
235
                        @Override
236
                        public void onClick(ClickEvent event) {
237
                                GSS.enableIESelection();
238
                                ((TextBox) event.getSource()).selectAll();
239
                                GSS.preventIESelection();
240
                        }
241

    
242
                });
243
                table.setWidget(1, 1, tokenBox);
244

    
245
                table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
246
                table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
247
                table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
248
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
249
                outer.add(table);
250

    
251
                // Create the 'OK' button, along with a listener that hides the dialog
252
                // when the button is clicked.
253
                Button confirm = new Button("Close", new ClickHandler() {
254
                        @Override
255
                        public void onClick(ClickEvent event) {
256
                                hide();
257
                        }
258
                });
259
                outer.add(confirm);
260
                outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
261

    
262
                // Create the 'Reset password' button, along with a listener that hides
263
                // the dialog when the button is clicked.
264
                Button resetPassword = new Button("Reset Password", new ClickHandler() {
265
                        @Override
266
                        public void onClick(ClickEvent event) {
267
                                ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
268
                                dlg.center();
269
                        }
270
                });
271
                outer.add(resetPassword);
272
                outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
273

    
274
                outer.setSpacing(8);
275
                setWidget(outer);
276
        }
277

    
278
        @Override
279
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
280
                super.onPreviewNativeEvent(preview);
281
                NativeEvent evt = preview.getNativeEvent();
282
                if (evt.getType().equals("keydown"))
283
                        // Use the popup's key preview hooks to close the dialog when
284
                        // either enter or escape is pressed.
285
                        switch (evt.getKeyCode()) {
286
                                case KeyCodes.KEY_ENTER:
287
                                case KeyCodes.KEY_ESCAPE:
288
                                        hide();
289
                                        break;
290
                        }
291
        }
292

    
293

    
294
        /**
295
         * Generate an RPC request to reset WebDAV password.
296
         *
297
         */
298
        private void resetPassword(String userUri) {
299

    
300
                if (userUri == null || userUri.length() == 0) {
301
                        GSS.get().displayError("Empty user Uri!");
302
                        return;
303
                }
304
                GWT.log("resetPassword(" + userUri + ")", null);
305
                PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
306

    
307
                        @Override
308
                        public void onComplete() {
309
                                ReauthenticateDialog dlg = new ReauthenticateDialog();
310
                                dlg.center();
311
                        }
312

    
313
                        @Override
314
                        public void onError(Throwable t) {
315
                                GWT.log("", t);
316
                                if(t instanceof RestException){
317
                                        int statusCode = ((RestException)t).getHttpStatusCode();
318
                                        if(statusCode == 405)
319
                                                GSS.get().displayError("You don't have the necessary" +
320
                                                                " permissions");
321
                                        else if(statusCode == 404)
322
                                                GSS.get().displayError("Resource does not exist");
323
                                        else
324
                                                GSS.get().displayError("Unable to reset password:" +
325
                                                                        ((RestException)t).getHttpStatusText());
326
                                }
327
                                else
328
                                        GSS.get().displayError("System error resetting password:" +
329
                                                                t.getMessage());
330
                        }
331
                };
332
                DeferredCommand.addCommand(cg);
333
        }
334

    
335
}