2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.rest.PostCommand;
22 import gr.ebs.gss.client.rest.RestException;
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;
43 * A dialog box that displays the user credentials for use in other client
44 * applications, such as WebDAV clients.
48 public class CredentialsDialog extends DialogBox {
50 private final String WIDTH_FIELD = "35em";
51 private final String WIDTH_TEXT = "42em";
53 private TextBox passwordBox;
56 * The 'confirm reset password' dialog box.
58 private class ConfirmResetPasswordDialog extends DialogBox {
61 * The widget's constructor.
63 * @param images the supplied images
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();
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");
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() {
85 public void onClick(ClickEvent event) {
86 resetPassword(GSS.get().getCurrentUserResource().getUri());
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() {
96 public void onClick(ClickEvent event) {
101 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
102 buttons.setSpacing(8);
103 buttons.setStyleName("gss-warnMessage");
104 outer.setStyleName("gss-warnMessage");
106 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
107 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
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:
128 private class ReauthenticateDialog extends DialogBox {
130 * The widget constructor.
132 public ReauthenticateDialog() {
133 // Set the dialog's caption.
134 setText("New Password Created");
135 setAnimationEnabled(true);
136 VerticalPanel outer = new VerticalPanel();
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");
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() {
150 public void onClick(ClickEvent event) {
151 GSS.get().authenticateUser();
156 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
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();
173 case KeyCodes.KEY_ESCAPE:
182 * The widget constructor.
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 +
201 text.setStyleName("gss-AboutText");
202 text.setWidth(WIDTH_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() {
215 public void onClick(ClickEvent event) {
216 GSS.enableIESelection();
217 ((TextBox) event.getSource()).selectAll();
218 GSS.preventIESelection();
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() {
229 public void onClick(ClickEvent event) {
230 GSS.enableIESelection();
231 ((TextBox) event.getSource()).selectAll();
232 GSS.preventIESelection();
236 table.setWidget(1, 1, passwordBox);
238 TextBox tokenBox = new TextBox();
239 tokenBox.setText(app.getToken());
240 tokenBox.setReadOnly(true);
241 tokenBox.setWidth(WIDTH_FIELD);
242 tokenBox.addClickHandler(new ClickHandler() {
244 public void onClick(ClickEvent event) {
245 GSS.enableIESelection();
246 ((TextBox) event.getSource()).selectAll();
247 GSS.preventIESelection();
251 table.setWidget(2, 1, tokenBox);
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");
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() {
265 public void onClick(ClickEvent event) {
270 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
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() {
276 public void onClick(ClickEvent event) {
277 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
281 outer.add(resetPassword);
282 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
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:
305 * Generate an RPC request to reset WebDAV password.
307 * @param userId the Uri of the user whose password will be reset
309 private void resetPassword(String userUri) {
311 if (userUri == null || userUri.length() == 0) {
312 GSS.get().displayError("Empty user Uri!");
315 GWT.log("resetPassword(" + userUri + ")", null);
316 PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
319 public void onComplete() {
320 ReauthenticateDialog dlg = new ReauthenticateDialog();
325 public void onError(Throwable 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" +
332 else if(statusCode == 404)
333 GSS.get().displayError("Resource does not exist");
335 GSS.get().displayError("Unable to reset password:" +
336 ((RestException)t).getHttpStatusText());
339 GSS.get().displayError("System error resetting password:" +
343 DeferredCommand.addCommand(cg);