Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / CredentialsDialog.java @ 78137d35

History | View | Annotate | Download (10.2 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 gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.rest.PostCommand;
22
import gr.ebs.gss.client.rest.RestException;
23

    
24
import com.google.gwt.core.client.GWT;
25
import com.google.gwt.user.client.DeferredCommand;
26
import com.google.gwt.user.client.ui.Button;
27
import com.google.gwt.user.client.ui.ClickListener;
28
import com.google.gwt.user.client.ui.DialogBox;
29
import com.google.gwt.user.client.ui.FlexTable;
30
import com.google.gwt.user.client.ui.HTML;
31
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
32
import com.google.gwt.user.client.ui.HorizontalPanel;
33
import com.google.gwt.user.client.ui.KeyboardListener;
34
import com.google.gwt.user.client.ui.TextBox;
35
import com.google.gwt.user.client.ui.VerticalPanel;
36
import com.google.gwt.user.client.ui.Widget;
37

    
38

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

    
47
        private final String WIDTH_FIELD = "35em";
48
        private final String WIDTH_TEXT = "42em";
49

    
50
        private TextBox passwordBox;
51

    
52
        /**
53
         * The 'confirm reset password' dialog box.
54
         */
55
        private class ConfirmResetPasswordDialog extends DialogBox {
56

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

    
70
                        HTML text;
71
                        text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to create a new WebDAV password?</td></tr></table>");
72
                        text.setStyleName("gss-warnMessage");
73
                        outer.add(text);
74

    
75
                        // Create the 'Yes' button, along with a listener that hides the dialog
76
                        // when the button is clicked and resets the password.
77
                        Button ok = new Button("Yes", new ClickListener() {
78
                                public void onClick(Widget sender) {
79
                                        resetPassword(GSS.get().getCurrentUserResource().getUri());
80
                                        hide();
81
                                }
82
                        });
83
                        buttons.add(ok);
84
                        buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
85
                        // Create the 'No' button, along with a listener that hides the
86
                        // dialog when the button is clicked.
87
                        Button cancel = new Button("No", new ClickListener() {
88
                                public void onClick(Widget sender) {
89
                                        hide();
90
                                }
91
                        });
92
                        buttons.add(cancel);
93
                        buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
94
                        buttons.setSpacing(8);
95
                        buttons.setStyleName("gss-warnMessage");
96
                        outer.setStyleName("gss-warnMessage");
97
                        outer.add(buttons);
98
                        outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
99
                        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
100
                        setWidget(outer);
101
                }
102

    
103

    
104
                @Override
105
                public boolean onKeyDownPreview(final char key, final int modifiers) {
106
                        // Use the popup's key preview hooks to close the dialog when
107
                        // escape is pressed.
108
                        switch (key) {
109
                                case KeyboardListener.KEY_ESCAPE:
110
                                        hide();
111
                                        break;
112
                        }
113

    
114
                        return true;
115
                }
116

    
117
        }
118

    
119
        private class ReauthenticateDialog extends DialogBox {
120
                /**
121
                 * The widget constructor.
122
                 */
123
                public ReauthenticateDialog() {
124
                        // Set the dialog's caption.
125
                        setText("New Password Created");
126
                        setAnimationEnabled(true);
127
                        VerticalPanel outer = new VerticalPanel();
128

    
129
                        // Create the text and set a style name so we can style it with CSS.
130
                        HTML text = new HTML("<p>A new WebDAV password has been created.</p>"+
131
                                                "<p>You will now be redirected to the initial screen for the changes to take effect. " +
132
                                                "Choose \"Show Credentials\" again afterwards to see the new password.</p>");
133
                        text.setStyleName("gss-AboutText");
134
                        outer.add(text);
135

    
136
                        // Create the 'OK' button, along with a listener that hides the dialog
137
                        // when the button is clicked.
138
                        Button confirm = new Button("Proceed", new ClickListener() {
139

    
140
                                public void onClick(Widget sender) {
141
                                        GSS.get().authenticateUser();
142
                                        hide();
143
                                }
144
                        });
145
                        outer.add(confirm);
146
                        outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
147
                        outer.setSpacing(8);
148
                        setWidget(outer);
149
                }
150

    
151
                @Override
152
                public boolean onKeyDownPreview(char key, int modifiers) {
153
                        // Use the popup's key preview hooks to close the dialog when either
154
                        // enter or escape is pressed.
155
                        switch (key) {
156
                                case KeyboardListener.KEY_ENTER:
157
                                        GSS.get().authenticateUser();
158
                                        hide();
159
                                        break;
160
                                case KeyboardListener.KEY_ESCAPE:
161
                                        hide();
162
                                        break;
163
                        }
164
                        return true;
165
                }
166
        }
167

    
168
        /**
169
         * The widget constructor.
170
         */
171
        public CredentialsDialog(final MessagePanel.Images images) {
172
                // Set the dialog's caption.
173
                setText("User Credentials");
174
                setAnimationEnabled(true);
175
                // Create a VerticalPanel to contain the 'about' label and the 'OK'
176
                // button.
177
                VerticalPanel outer = new VerticalPanel();
178
                Configuration conf = (Configuration) GWT.create(Configuration.class);
179
                String service = conf.serviceName();
180
                String webdavUrl = conf.serviceHome() + conf.webdavUrl();
181
                String tokenNote = conf.tokenTTLNote();
182
                // Create the text and set a style name so we can style it with CSS.
183
                HTML text = new HTML("<p>These are the user credentials that are required " +
184
                                "for interacting with " + service + ". " +
185
                                "You can copy and paste the username and password in the WebDAV client" +
186
                                " in order to use " + service + " through the WebDAV interface, at:<br/> " +
187
                                webdavUrl +
188
                                "<br/>" + tokenNote + "</p>");
189
                text.setStyleName("gss-AboutText");
190
                text.setWidth(WIDTH_TEXT);
191
                outer.add(text);
192
                FlexTable table = new FlexTable();
193
                table.setText(0, 0, "Username");
194
                table.setText(1, 0, "Password");
195
                table.setText(2, 0, "Token");
196
                TextBox username = new TextBox();
197
                final GSS app = GSS.get();
198
                username.setText(app.getCurrentUserResource().getUsername());
199
                username.setReadOnly(true);
200
                username.setWidth(WIDTH_FIELD);
201
                username.addClickListener(new ClickListener () {
202

    
203
                        public void onClick(Widget sender) {
204
                                GSS.enableIESelection();
205
                                ((TextBox) sender).selectAll();
206
                                GSS.preventIESelection();
207
                        }
208

    
209
                });
210
                table.setWidget(0, 1, username);
211
                passwordBox = new TextBox();
212
                passwordBox.setText(app.getWebDAVPassword());
213
                passwordBox.setReadOnly(true);
214
                passwordBox.setWidth(WIDTH_FIELD);
215
                passwordBox.addClickListener(new ClickListener () {
216

    
217
                        public void onClick(Widget sender) {
218
                                GSS.enableIESelection();
219
                                ((TextBox) sender).selectAll();
220
                                GSS.preventIESelection();
221
                        }
222

    
223
                });
224
                table.setWidget(1, 1, passwordBox);
225

    
226
                TextBox tokenBox = new TextBox();
227
                tokenBox.setText(app.getToken());
228
                tokenBox.setReadOnly(true);
229
                tokenBox.setWidth(WIDTH_FIELD);
230
                tokenBox.addClickListener(new ClickListener () {
231

    
232
                        public void onClick(Widget sender) {
233
                                GSS.enableIESelection();
234
                                ((TextBox) sender).selectAll();
235
                                GSS.preventIESelection();
236
                        }
237

    
238
                });
239
                table.setWidget(2, 1, tokenBox);
240

    
241
                table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
242
                table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
243
                table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
244
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
245
                table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
246
                table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
247
                outer.add(table);
248

    
249
                // Create the 'OK' button, along with a listener that hides the dialog
250
                // when the button is clicked.
251
                Button confirm = new Button("Close", new ClickListener() {
252

    
253
                        public void onClick(Widget sender) {
254
                                hide();
255
                        }
256
                });
257
                outer.add(confirm);
258
                outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
259

    
260
                // Create the 'Reset password' button, along with a listener that hides the dialog
261
                // when the button is clicked.
262
                Button resetPassword = new Button("Reset Password", new ClickListener() {
263

    
264
                        public void onClick(Widget sender) {
265
                                ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
266
                                dlg.center();
267
                        }
268
                });
269
                outer.add(resetPassword);
270
                outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
271

    
272
                outer.setSpacing(8);
273
                setWidget(outer);
274
        }
275

    
276
        @Override
277
        public boolean onKeyDownPreview(char key, int modifiers) {
278
                // Use the popup's key preview hooks to close the dialog when either
279
                // enter or escape is pressed.
280
                switch (key) {
281
                        case KeyboardListener.KEY_ENTER:
282
                        case KeyboardListener.KEY_ESCAPE:
283
                                hide();
284
                                break;
285
                }
286
                return true;
287
        }
288

    
289

    
290
        /**
291
         * Generate an RPC request to reset WebDAV password.
292
         *
293
         * @param userId the Uri of the user whose password will be reset
294
         */
295
        private void resetPassword(String userUri) {
296

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

    
304
                        @Override
305
                        public void onComplete() {
306
                                ReauthenticateDialog dlg = new ReauthenticateDialog();
307
                                dlg.center();
308
                        }
309

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

    
328
        }
329

    
330
}