Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / CredentialsDialog.java @ 9e6e0572

History | View | Annotate | Download (9.5 kB)

1
/*
2
 *  Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client;
5

    
6
import com.google.gwt.user.client.Window;
7
import gr.grnet.pithos.web.client.rest.PostCommand;
8
import gr.grnet.pithos.web.client.rest.RestException;
9

    
10
import com.google.gwt.core.client.GWT;
11
import com.google.gwt.dom.client.NativeEvent;
12
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14
import com.google.gwt.event.dom.client.KeyCodes;
15
import com.google.gwt.user.client.DeferredCommand;
16
import com.google.gwt.user.client.Event.NativePreviewEvent;
17
import com.google.gwt.user.client.ui.AbstractImagePrototype;
18
import com.google.gwt.user.client.ui.Button;
19
import com.google.gwt.user.client.ui.DialogBox;
20
import com.google.gwt.user.client.ui.FlexTable;
21
import com.google.gwt.user.client.ui.HTML;
22
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
23
import com.google.gwt.user.client.ui.HorizontalPanel;
24
import com.google.gwt.user.client.ui.TextBox;
25
import com.google.gwt.user.client.ui.VerticalPanel;
26

    
27

    
28
/**
29
 * A dialog box that displays the user credentials for use in other client
30
 * applications, such as WebDAV clients.
31
 */
32
public class CredentialsDialog extends DialogBox {
33

    
34
        private final String WIDTH_FIELD = "35em";
35
        private final String WIDTH_TEXT = "42em";
36

    
37
        /**
38
         * The 'confirm reset password' dialog box.
39
         */
40
        private class ConfirmResetPasswordDialog extends DialogBox {
41

    
42
                /**
43
                 * The widget's constructor.
44
                 *
45
                 * @param images the supplied images
46
                 */
47
                private ConfirmResetPasswordDialog(MessagePanel.Images images) {
48
                        // Set the dialog's caption.
49
                        setText("Confirmation");
50
                        setAnimationEnabled(true);
51
                        // Create a VerticalPanel to contain the label and the buttons.
52
                        VerticalPanel outer = new VerticalPanel();
53
                        HorizontalPanel buttons = new HorizontalPanel();
54

    
55
                        HTML text;
56
                        text = new HTML("<table><tr><td>" +
57
                                        AbstractImagePrototype.create(images.warn()).getHTML() +
58
                                        "</td><td>" + "Are you sure you want to create a new " +
59
                                        "WebDAV password?</td></tr></table>");
60
                        text.setStyleName("pithos-warnMessage");
61
                        outer.add(text);
62

    
63
                        // Create the 'Yes' button, along with a listener that hides the
64
                        // dialog when the button is clicked and resets the password.
65
                        Button ok = new Button("Yes", new ClickHandler() {
66
                                @Override
67
                                public void onClick(ClickEvent event) {
68
                                        resetPassword(GSS.get().getCurrentUserResource().getUri());
69
                                        hide();
70
                                }
71
                        });
72
                        buttons.add(ok);
73
                        buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
74
                        // Create the 'No' button, along with a listener that hides the
75
                        // dialog when the button is clicked.
76
                        Button cancel = new Button("No", new ClickHandler() {
77
                                @Override
78
                                public void onClick(ClickEvent event) {
79
                                        hide();
80
                                }
81
                        });
82
                        buttons.add(cancel);
83
                        buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
84
                        buttons.setSpacing(8);
85
                        buttons.setStyleName("pithos-warnMessage");
86
                        outer.setStyleName("pithos-warnMessage");
87
                        outer.add(buttons);
88
                        outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
89
                        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
90
                        setWidget(outer);
91
                }
92

    
93
                @Override
94
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
95
                        super.onPreviewNativeEvent(preview);
96
                        NativeEvent evt = preview.getNativeEvent();
97
                        if (evt.getType().equals("keydown"))
98
                                // Use the popup's key preview hooks to close the dialog when either
99
                                // enter or escape is pressed.
100
                                switch (evt.getKeyCode()) {
101
                                        case KeyCodes.KEY_ENTER:
102
                                        case KeyCodes.KEY_ESCAPE:
103
                                                hide();
104
                                                break;
105
                                }
106
                }
107

    
108
        }
109

    
110
        private class ReauthenticateDialog extends DialogBox {
111
                /**
112
                 * The widget constructor.
113
                 */
114
                public ReauthenticateDialog() {
115
                        // Set the dialog's caption.
116
                        setText("New Password Created");
117
                        setAnimationEnabled(true);
118
                        VerticalPanel outer = new VerticalPanel();
119

    
120
                        // Create the text and set a style name so we can style it with CSS.
121
                        HTML text = new HTML("<p>A new WebDAV password has been created." +
122
                                        "</p><p>You will now be redirected to the initial screen" +
123
                                        " for the changes to take effect. Choose \"Show " +
124
                                        "Credentials\" again afterwards to see the new password.</p>");
125
                        text.setStyleName("pithos-AboutText");
126
                        outer.add(text);
127

    
128
                        // Create the 'OK' button, along with a listener that hides the
129
                        // dialog when the button is clicked.
130
                        Button confirm = new Button("Proceed", new ClickHandler() {
131
                                @Override
132
                                public void onClick(ClickEvent event) {
133
                                        GSS.get().authenticateUser();
134
                                        hide();
135
                                }
136
                        });
137
                        outer.add(confirm);
138
                        outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
139
                        outer.setSpacing(8);
140
                        setWidget(outer);
141
                }
142

    
143
                @Override
144
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
145
                        super.onPreviewNativeEvent(preview);
146
                        NativeEvent evt = preview.getNativeEvent();
147
                        if (evt.getType().equals("keydown"))
148
                                // Use the popup's key preview hooks to close the dialog when
149
                                // either enter or escape is pressed.
150
                                switch (evt.getKeyCode()) {
151
                                        case KeyCodes.KEY_ENTER:
152
                                                GSS.get().authenticateUser();
153
                                                hide();
154
                                                break;
155
                                        case KeyCodes.KEY_ESCAPE:
156
                                                hide();
157
                                                break;
158
                                }
159
                }
160

    
161
        }
162

    
163
        /**
164
         * The widget constructor.
165
         */
166
        public CredentialsDialog(final MessagePanel.Images images) {
167
                // Set the dialog's caption.
168
                setText("User Credentials");
169
                setAnimationEnabled(true);
170
                // A VerticalPanel that contains the 'about' label and the 'OK' button.
171
                VerticalPanel outer = new VerticalPanel();
172
                Configuration conf = (Configuration) GWT.create(Configuration.class);
173
                String service = conf.serviceName();
174
                // Create the text and set a style name so we can style it with CSS.
175
                HTML text = new HTML("<p>These are the user credentials that are " +
176
                                "required for interacting with " + service + ".");
177
                text.setStyleName("pithos-AboutText");
178
                text.setWidth(WIDTH_TEXT);
179
                outer.add(text);
180
                FlexTable table = new FlexTable();
181
                table.setText(0, 0, "Username");
182
                table.setText(1, 0, "Token");
183
                TextBox username = new TextBox();
184
                final GSS app = GSS.get();
185
                username.setText(app.getCurrentUserResource().getUsername());
186
                username.setReadOnly(true);
187
                username.setWidth(WIDTH_FIELD);
188
                username.addClickHandler(new ClickHandler() {
189
                        @Override
190
                        public void onClick(ClickEvent event) {
191
                                GSS.enableIESelection();
192
                                ((TextBox) event.getSource()).selectAll();
193
                                GSS.preventIESelection();
194
                        }
195

    
196
                });
197
                table.setWidget(0, 1, username);
198

    
199
                TextBox tokenBox = new TextBox();
200
                tokenBox.setText(app.getToken());
201
                tokenBox.setReadOnly(true);
202
                tokenBox.setWidth(WIDTH_FIELD);
203
                tokenBox.addClickHandler(new ClickHandler() {
204
                        @Override
205
                        public void onClick(ClickEvent event) {
206
                                GSS.enableIESelection();
207
                                ((TextBox) event.getSource()).selectAll();
208
                                GSS.preventIESelection();
209
                        }
210

    
211
                });
212
                table.setWidget(1, 1, tokenBox);
213

    
214
                table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
215
                table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
216
                table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
217
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
218
                outer.add(table);
219

    
220
                // Create the 'OK' button, along with a listener that hides the dialog
221
                // when the button is clicked.
222
                Button confirm = new Button("Close", new ClickHandler() {
223
                        @Override
224
                        public void onClick(ClickEvent event) {
225
                                hide();
226
                        }
227
                });
228
                outer.add(confirm);
229
                outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
230

    
231
                // Create the 'Reset password' button, along with a listener that hides
232
                // the dialog when the button is clicked.
233
                Button resetPassword = new Button("Reset Password", new ClickHandler() {
234
                        @Override
235
                        public void onClick(ClickEvent event) {
236
                                ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
237
                                dlg.center();
238
                        }
239
                });
240
                outer.add(resetPassword);
241
                outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
242

    
243
                outer.setSpacing(8);
244
                setWidget(outer);
245
        }
246

    
247
        @Override
248
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
249
                super.onPreviewNativeEvent(preview);
250
                NativeEvent evt = preview.getNativeEvent();
251
                if (evt.getType().equals("keydown"))
252
                        // Use the popup's key preview hooks to close the dialog when
253
                        // either enter or escape is pressed.
254
                        switch (evt.getKeyCode()) {
255
                                case KeyCodes.KEY_ENTER:
256
                                case KeyCodes.KEY_ESCAPE:
257
                                        hide();
258
                                        break;
259
                        }
260
        }
261

    
262

    
263
        /**
264
         * Generate an RPC request to reset WebDAV password.
265
         *
266
         */
267
        private void resetPassword(String userUri) {
268

    
269
                if (userUri == null || userUri.length() == 0) {
270
                        GSS.get().displayError("Empty user Uri!");
271
                        return;
272
                }
273
                GWT.log("resetPassword(" + userUri + ")", null);
274
                PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
275

    
276
                        @Override
277
                        public void onComplete() {
278
                                ReauthenticateDialog dlg = new ReauthenticateDialog();
279
                                dlg.center();
280
                        }
281

    
282
                        @Override
283
                        public void onError(Throwable t) {
284
                                GWT.log("", t);
285
                                if(t instanceof RestException){
286
                                        int statusCode = ((RestException)t).getHttpStatusCode();
287
                                        if(statusCode == 405)
288
                                                GSS.get().displayError("You don't have the necessary" +
289
                                                                " permissions");
290
                                        else if(statusCode == 404)
291
                                                GSS.get().displayError("Resource does not exist");
292
                                        else
293
                                                GSS.get().displayError("Unable to reset password:" +
294
                                                                        ((RestException)t).getHttpStatusText());
295
                                }
296
                                else
297
                                        GSS.get().displayError("System error resetting password:" +
298
                                                                t.getMessage());
299
                        }
300
                };
301
                DeferredCommand.addCommand(cg);
302
        }
303

    
304
}