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.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;
40 * A dialog box that displays the user credentials for use in other client
41 * applications, such as WebDAV clients.
45 public class CredentialsDialog extends DialogBox {
47 private final String WIDTH_FIELD = "35em";
48 private final String WIDTH_TEXT = "42em";
50 private TextBox passwordBox;
53 * The 'confirm reset password' dialog box.
55 private class ConfirmResetPasswordDialog extends DialogBox {
58 * The widget's constructor.
60 * @param images the supplied images
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();
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");
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());
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) {
93 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
94 buttons.setSpacing(8);
95 buttons.setStyleName("gss-warnMessage");
96 outer.setStyleName("gss-warnMessage");
98 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
99 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
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.
109 case KeyboardListener.KEY_ESCAPE:
119 private class ReauthenticateDialog extends DialogBox {
121 * The widget constructor.
123 public ReauthenticateDialog() {
124 // Set the dialog's caption.
125 setText("New Password Created");
126 setAnimationEnabled(true);
127 VerticalPanel outer = new VerticalPanel();
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");
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() {
140 public void onClick(Widget sender) {
141 GSS.get().authenticateUser();
146 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
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.
156 case KeyboardListener.KEY_ENTER:
157 GSS.get().authenticateUser();
160 case KeyboardListener.KEY_ESCAPE:
169 * The widget constructor.
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'
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/> " +
188 "<br/>" + tokenNote + "</p>");
189 text.setStyleName("gss-AboutText");
190 text.setWidth(WIDTH_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 () {
203 public void onClick(Widget sender) {
204 GSS.enableIESelection();
205 ((TextBox) sender).selectAll();
206 GSS.preventIESelection();
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 () {
217 public void onClick(Widget sender) {
218 GSS.enableIESelection();
219 ((TextBox) sender).selectAll();
220 GSS.preventIESelection();
224 table.setWidget(1, 1, passwordBox);
226 TextBox tokenBox = new TextBox();
227 tokenBox.setText(app.getToken());
228 tokenBox.setReadOnly(true);
229 tokenBox.setWidth(WIDTH_FIELD);
230 tokenBox.addClickListener(new ClickListener () {
232 public void onClick(Widget sender) {
233 GSS.enableIESelection();
234 ((TextBox) sender).selectAll();
235 GSS.preventIESelection();
239 table.setWidget(2, 1, tokenBox);
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");
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() {
253 public void onClick(Widget sender) {
258 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
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() {
264 public void onClick(Widget sender) {
265 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
269 outer.add(resetPassword);
270 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
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.
281 case KeyboardListener.KEY_ENTER:
282 case KeyboardListener.KEY_ESCAPE:
291 * Generate an RPC request to reset WebDAV password.
293 * @param userId the Uri of the user whose password will be reset
295 private void resetPassword(String userUri) {
297 if (userUri == null || userUri.length() == 0) {
298 GSS.get().displayError("Empty user Uri!");
301 GWT.log("resetPassword(" + userUri + ")", null);
302 PostCommand cg = new PostCommand(userUri + "users?resetWebDAV", "", 200) {
305 public void onComplete() {
306 ReauthenticateDialog dlg = new ReauthenticateDialog();
311 public void onError(Throwable 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");
320 GSS.get().displayError("Unable to reset password:"+((RestException)t).getHttpStatusText());
323 GSS.get().displayError("System error resetting password:"+t.getMessage());
326 DeferredCommand.addCommand(cg);