Fix of Revision: ec7b8d0b2c after code review. Added a check condition before remov...
[pithos] / src / gr / ebs / gss / client / CredentialsDialog.java
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.dom.client.NativeEvent;
26 import com.google.gwt.event.dom.client.ClickEvent;
27 import com.google.gwt.event.dom.client.ClickHandler;
28 import com.google.gwt.event.dom.client.KeyCodes;
29 import com.google.gwt.user.client.DeferredCommand;
30 import com.google.gwt.user.client.Event.NativePreviewEvent;
31 import com.google.gwt.user.client.ui.AbstractImagePrototype;
32 import com.google.gwt.user.client.ui.Button;
33 import com.google.gwt.user.client.ui.DialogBox;
34 import com.google.gwt.user.client.ui.FlexTable;
35 import com.google.gwt.user.client.ui.HTML;
36 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
37 import com.google.gwt.user.client.ui.HorizontalPanel;
38 import com.google.gwt.user.client.ui.TextBox;
39 import com.google.gwt.user.client.ui.VerticalPanel;
40
41
42 /**
43  * A dialog box that displays the user credentials for use in other client
44  * applications, such as WebDAV clients.
45  *
46  * @author kman
47  */
48 public class CredentialsDialog extends DialogBox {
49
50         private final String WIDTH_FIELD = "35em";
51         private final String WIDTH_TEXT = "42em";
52
53         private TextBox passwordBox;
54
55         /**
56          * The 'confirm reset password' dialog box.
57          */
58         private class ConfirmResetPasswordDialog extends DialogBox {
59
60                 /**
61                  * The widget's constructor.
62                  *
63                  * @param images the supplied images
64                  */
65                 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
66                         // Set the dialog's caption.
67                         setText("Confirmation");
68                         setAnimationEnabled(true);
69                         // Create a VerticalPanel to contain the label and the buttons.
70                         VerticalPanel outer = new VerticalPanel();
71                         HorizontalPanel buttons = new HorizontalPanel();
72
73                         HTML text;
74                         text = new HTML("<table><tr><td>" +
75                                         AbstractImagePrototype.create(images.warn()).getHTML() +
76                                         "</td><td>" + "Are you sure you want to create a new " +
77                                         "WebDAV password?</td></tr></table>");
78                         text.setStyleName("gss-warnMessage");
79                         outer.add(text);
80
81                         // Create the 'Yes' button, along with a listener that hides the
82                         // dialog when the button is clicked and resets the password.
83                         Button ok = new Button("Yes", new ClickHandler() {
84                                 @Override
85                                 public void onClick(ClickEvent event) {
86                                         resetPassword(GSS.get().getCurrentUserResource().getUri());
87                                         hide();
88                                 }
89                         });
90                         buttons.add(ok);
91                         buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
92                         // Create the 'No' button, along with a listener that hides the
93                         // dialog when the button is clicked.
94                         Button cancel = new Button("No", new ClickHandler() {
95                                 @Override
96                                 public void onClick(ClickEvent event) {
97                                         hide();
98                                 }
99                         });
100                         buttons.add(cancel);
101                         buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
102                         buttons.setSpacing(8);
103                         buttons.setStyleName("gss-warnMessage");
104                         outer.setStyleName("gss-warnMessage");
105                         outer.add(buttons);
106                         outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
107                         outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
108                         setWidget(outer);
109                 }
110
111                 @Override
112                 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
113                         super.onPreviewNativeEvent(preview);
114                         NativeEvent evt = preview.getNativeEvent();
115                         if (evt.getType().equals("keydown"))
116                                 // Use the popup's key preview hooks to close the dialog when either
117                                 // enter or escape is pressed.
118                                 switch (evt.getKeyCode()) {
119                                         case KeyCodes.KEY_ENTER:
120                                         case KeyCodes.KEY_ESCAPE:
121                                                 hide();
122                                                 break;
123                                 }
124                 }
125
126         }
127
128         private class ReauthenticateDialog extends DialogBox {
129                 /**
130                  * The widget constructor.
131                  */
132                 public ReauthenticateDialog() {
133                         // Set the dialog's caption.
134                         setText("New Password Created");
135                         setAnimationEnabled(true);
136                         VerticalPanel outer = new VerticalPanel();
137
138                         // Create the text and set a style name so we can style it with CSS.
139                         HTML text = new HTML("<p>A new WebDAV password has been created." +
140                                         "</p><p>You will now be redirected to the initial screen" +
141                                         " for the changes to take effect. Choose \"Show " +
142                                         "Credentials\" again afterwards to see the new password.</p>");
143                         text.setStyleName("gss-AboutText");
144                         outer.add(text);
145
146                         // Create the 'OK' button, along with a listener that hides the
147                         // dialog when the button is clicked.
148                         Button confirm = new Button("Proceed", new ClickHandler() {
149                                 @Override
150                                 public void onClick(ClickEvent event) {
151                                         GSS.get().authenticateUser();
152                                         hide();
153                                 }
154                         });
155                         outer.add(confirm);
156                         outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
157                         outer.setSpacing(8);
158                         setWidget(outer);
159                 }
160
161                 @Override
162                 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
163                         super.onPreviewNativeEvent(preview);
164                         NativeEvent evt = preview.getNativeEvent();
165                         if (evt.getType().equals("keydown"))
166                                 // Use the popup's key preview hooks to close the dialog when
167                                 // either enter or escape is pressed.
168                                 switch (evt.getKeyCode()) {
169                                         case KeyCodes.KEY_ENTER:
170                                                 GSS.get().authenticateUser();
171                                                 hide();
172                                                 break;
173                                         case KeyCodes.KEY_ESCAPE:
174                                                 hide();
175                                                 break;
176                                 }
177                 }
178
179         }
180
181         /**
182          * The widget constructor.
183          */
184         public CredentialsDialog(final MessagePanel.Images images) {
185                 // Set the dialog's caption.
186                 setText("User Credentials");
187                 setAnimationEnabled(true);
188                 // A VerticalPanel that contains the 'about' label and the 'OK' button.
189                 VerticalPanel outer = new VerticalPanel();
190                 Configuration conf = (Configuration) GWT.create(Configuration.class);
191                 String service = conf.serviceName();
192                 String webdavUrl = conf.serviceHome() + conf.webdavUrl();
193                 String tokenNote = conf.tokenTTLNote();
194                 // Create the text and set a style name so we can style it with CSS.
195                 HTML text = new HTML("<p>These are the user credentials that are " +
196                                 "required for interacting with " + service + ". You can copy" +
197                                 " and paste the username and password in the WebDAV client " +
198                                 "in order to use " + service + " through the WebDAV " +
199                                 "interface, at:<br/> " + webdavUrl + "<br/>" + tokenNote +
200                                 "</p>");
201                 text.setStyleName("gss-AboutText");
202                 text.setWidth(WIDTH_TEXT);
203                 outer.add(text);
204                 FlexTable table = new FlexTable();
205                 table.setText(0, 0, "Username");
206                 table.setText(1, 0, "Password");
207                 table.setText(2, 0, "Token");
208                 TextBox username = new TextBox();
209                 final GSS app = GSS.get();
210                 username.setText(app.getCurrentUserResource().getUsername());
211                 username.setReadOnly(true);
212                 username.setWidth(WIDTH_FIELD);
213                 username.addClickHandler(new ClickHandler() {
214                         @Override
215                         public void onClick(ClickEvent event) {
216                                 GSS.enableIESelection();
217                                 ((TextBox) event.getSource()).selectAll();
218                                 GSS.preventIESelection();
219                         }
220
221                 });
222                 table.setWidget(0, 1, username);
223                 passwordBox = new TextBox();
224                 passwordBox.setText(app.getWebDAVPassword());
225                 passwordBox.setReadOnly(true);
226                 passwordBox.setWidth(WIDTH_FIELD);
227                 passwordBox.addClickHandler(new  ClickHandler() {
228                         @Override
229                         public void onClick(ClickEvent event) {
230                                 GSS.enableIESelection();
231                                 ((TextBox) event.getSource()).selectAll();
232                                 GSS.preventIESelection();
233                         }
234
235                 });
236                 table.setWidget(1, 1, passwordBox);
237
238                 TextBox tokenBox = new TextBox();
239                 tokenBox.setText(app.getToken());
240                 tokenBox.setReadOnly(true);
241                 tokenBox.setWidth(WIDTH_FIELD);
242                 tokenBox.addClickHandler(new ClickHandler() {
243                         @Override
244                         public void onClick(ClickEvent event) {
245                                 GSS.enableIESelection();
246                                 ((TextBox) event.getSource()).selectAll();
247                                 GSS.preventIESelection();
248                         }
249
250                 });
251                 table.setWidget(2, 1, tokenBox);
252
253                 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
254                 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
255                 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
256                 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
257                 table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
258                 table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
259                 outer.add(table);
260
261                 // Create the 'OK' button, along with a listener that hides the dialog
262                 // when the button is clicked.
263                 Button confirm = new Button("Close", new ClickHandler() {
264                         @Override
265                         public void onClick(ClickEvent event) {
266                                 hide();
267                         }
268                 });
269                 outer.add(confirm);
270                 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
271
272                 // Create the 'Reset password' button, along with a listener that hides
273                 // the dialog when the button is clicked.
274                 Button resetPassword = new Button("Reset Password", new ClickHandler() {
275                         @Override
276                         public void onClick(ClickEvent event) {
277                                 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
278                                 dlg.center();
279                         }
280                 });
281                 outer.add(resetPassword);
282                 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
283
284                 outer.setSpacing(8);
285                 setWidget(outer);
286         }
287
288         @Override
289         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
290                 super.onPreviewNativeEvent(preview);
291                 NativeEvent evt = preview.getNativeEvent();
292                 if (evt.getType().equals("keydown"))
293                         // Use the popup's key preview hooks to close the dialog when
294                         // either enter or escape is pressed.
295                         switch (evt.getKeyCode()) {
296                                 case KeyCodes.KEY_ENTER:
297                                 case KeyCodes.KEY_ESCAPE:
298                                         hide();
299                                         break;
300                         }
301         }
302
303
304         /**
305          * Generate an RPC request to reset WebDAV password.
306          *
307          * @param userId the Uri of the user whose password will be reset
308          */
309         private void resetPassword(String userUri) {
310
311                 if (userUri == null || userUri.length() == 0) {
312                         GSS.get().displayError("Empty user Uri!");
313                         return;
314                 }
315                 GWT.log("resetPassword(" + userUri + ")", null);
316                 PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
317
318                         @Override
319                         public void onComplete() {
320                                 ReauthenticateDialog dlg = new ReauthenticateDialog();
321                                 dlg.center();
322                         }
323
324                         @Override
325                         public void onError(Throwable t) {
326                                 GWT.log("", t);
327                                 if(t instanceof RestException){
328                                         int statusCode = ((RestException)t).getHttpStatusCode();
329                                         if(statusCode == 405)
330                                                 GSS.get().displayError("You don't have the necessary" +
331                                                                 " permissions");
332                                         else if(statusCode == 404)
333                                                 GSS.get().displayError("Resource does not exist");
334                                         else
335                                                 GSS.get().displayError("Unable to reset password:" +
336                                                                         ((RestException)t).getHttpStatusText());
337                                 }
338                                 else
339                                         GSS.get().displayError("System error resetting password:" +
340                                                                 t.getMessage());
341                         }
342                 };
343                 DeferredCommand.addCommand(cg);
344         }
345
346 }