summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.console/src/org/gluster/storage/management/console/dialogs/ChangePasswordDialog.java
blob: 60386b753b5709c5d9b35d8d027a1063fcbc2982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Console is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.console.dialogs;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.gluster.storage.management.client.UsersClient;
import org.gluster.storage.management.console.utils.GUIHelper;
import org.gluster.storage.management.console.validators.StringRequiredValidator;
import org.gluster.storage.management.core.constants.CoreConstants;
import org.gluster.storage.management.core.model.ConnectionDetails;


public class ChangePasswordDialog extends Dialog {
	public static final int RETURN_CODE_ERROR = 2;
	private Text oldPassword;
	private Text newPassword;
	private Text confirmPassword;
	private Button okButton;

	private final GUIHelper guiHelper = GUIHelper.getInstance();
	private Composite composite;

	private final ConnectionDetails connectionDetails = new ConnectionDetails("gluster", "");

	public ChangePasswordDialog(Shell shell) {
		super(shell);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);

		newShell.setText("Gluster Management Console");
		addEscapeListener(newShell);
	}

	private void addEscapeListener(Shell shell) {
		shell.addTraverseListener(new TraverseListener() {

			@Override
			public void keyTraversed(TraverseEvent e) {
				if (e.keyCode == SWT.ESC) {
					cancelPressed();
				}
			}
		});
	}

	/**
	 * Overriding to make sure that the dialog is centered in screen
	 */
	@Override
	protected void initializeBounds() {
		super.initializeBounds();

		guiHelper.centerShellInScreen(getShell());
	}

	private void configureDialogLayout(Composite composite) {
		GridLayout layout = (GridLayout) composite.getLayout();
		layout.numColumns = 2;
		layout.marginLeft = 20;
		layout.marginRight = 20;
		layout.marginTop = 20;
		layout.horizontalSpacing = 20;
		layout.verticalSpacing = 20;
	}

	// ------------------------------------------

	private void createLabel(Composite composite, String label) {
		Label passwordLabel = new Label(composite, SWT.NONE);
		passwordLabel.setText(label);
		passwordLabel.setLayoutData(new GridData(GridData.END, GridData.CENTER, false, false));
	}

	private Text createPasswordText(Composite composite) {
		Text field = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		GridData layoutData = new GridData(SWT.FILL, GridData.FILL, true, false);
		layoutData.widthHint = convertWidthInCharsToPixels(32);
		field.setLayoutData(layoutData);
		return field;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		// parent.setBackgroundImage(guiHelper.getImage(IImageKeys.DIALOG_SPLASH_IMAGE));
		parent.setBackgroundMode(SWT.INHERIT_FORCE);

		composite = (Composite) super.createDialogArea(parent);
		configureDialogLayout(composite);

		createLabel(composite, "Old Password:");
		oldPassword = createPasswordText(composite);

		createLabel(composite, "New Password:");
		newPassword = createPasswordText(composite);
		
		createLabel(composite, "Confirm Password:");
		confirmPassword = createPasswordText(composite);
		
		createListeners();

		return composite;
	}

	/**
	 * 
	 */
	private void createListeners() {
		ModifyListener listener = new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				updateButtonStatus();
			}
		};
		
		oldPassword.addModifyListener(listener);
		newPassword.addModifyListener(listener);
		confirmPassword.addModifyListener(listener);
	}

	private void updateButtonStatus() {
		String oldPwd = oldPassword.getText();
		String newPwd = newPassword.getText();
		String confirmPwd = confirmPassword.getText();
		if(oldPwd.isEmpty() || newPwd.isEmpty() || confirmPwd.isEmpty()) {
			okButton.setEnabled(false);
			return;
		}
		
		if(!newPwd.equals(confirmPwd)) {
			okButton.setEnabled(false);
			return;
		}

		if (confirmPwd.equals(CoreConstants.DEFAULT_PASSWORD) || isContainsWhiteSpace(confirmPwd)) {
			okButton.setEnabled(false);
			return;
		}

		if (newPwd.length() < 4 ) { // Minimum  password length is 4
			okButton.setEnabled(false);
			return;
		}

		okButton.setEnabled(true);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		okButton = createButton(parent, IDialogConstants.OK_ID, "&Change", true);
		okButton.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);

		setupDataBinding();
	}

	public boolean isContainsWhiteSpace(String text) {
		return text.matches(".*[\\s\\\\].*"); // White space or backslash
	}
	
	public class ConfirmPasswordValidator extends StringRequiredValidator {
		public ConfirmPasswordValidator(String errorText, ControlDecoration controlDecoration, Control linkedControl) {
			super(errorText, controlDecoration, linkedControl);
		}

		@Override
		public IStatus validate(Object value) {

			IStatus status = super.validate(value);
			if (status.isOK()) {
				String errMsg = null;
				if (errMsg == null && isContainsWhiteSpace( newPassword.getText())) {
					errMsg = "Password should not contain space or back slash characters";
				}
				
				if (!value.equals(newPassword.getText())) {
					errMsg = "Passwords mismatched";
				}

				if (errMsg == null && value.equals(CoreConstants.DEFAULT_PASSWORD)) {
					errMsg = "New password should not be a default password";
				}

				if(errMsg != null) {
					controlDecoration.setDescriptionText(errMsg);
					controlDecoration.show();
					linkedControl.setEnabled(false);
					return ValidationStatus.error(errMsg);
				}
			}
			return status;
		}
	};

	private void setupDataBinding() {
		DataBindingContext dataBindingContext = new DataBindingContext(SWTObservables.getRealm(Display.getCurrent()));
		UpdateValueStrategy passwordBindingStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
		UpdateValueStrategy newPwdBindingStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);
		UpdateValueStrategy confirmPwdBindingStrategy = new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE);

		// The Validator shows error decoration and disables OK button on
		// validation failure
		passwordBindingStrategy.setBeforeSetValidator(new StringRequiredValidator("Please enter old password!",
				guiHelper.createErrorDecoration(oldPassword), null));

		dataBindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(oldPassword),
				PojoProperties.value("password").observe(connectionDetails), passwordBindingStrategy,
				passwordBindingStrategy);

		newPwdBindingStrategy.setBeforeSetValidator(new StringRequiredValidator("Please enter new password!", guiHelper
				.createErrorDecoration(newPassword), null));

		dataBindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(newPassword),
				PojoProperties.value("newPassword").observe(connectionDetails), newPwdBindingStrategy,
				newPwdBindingStrategy);

		confirmPwdBindingStrategy.setBeforeSetValidator(new ConfirmPasswordValidator("Please enter confirm password!",
				guiHelper.createErrorDecoration(confirmPassword), null));

		dataBindingContext.bindValue(WidgetProperties.text(SWT.Modify).observe(confirmPassword),
				PojoProperties.value("confirmNewPassword").observe(connectionDetails), confirmPwdBindingStrategy,
				confirmPwdBindingStrategy);
	}

	protected void okPressed() {
		String user = connectionDetails.getUserId();
		String oldPassword = connectionDetails.getPassword();
		String newPassword = connectionDetails.getNewPassword();

		UsersClient usersClient = new UsersClient();
		try {
			usersClient.changePassword(user, oldPassword, newPassword);
			MessageDialog.openInformation(getShell(), "Change password", "Password changed successfully!");
			this.close();
		} catch (Exception e) {
			MessageDialog.openError(getShell(), "Change password Failed", e.getMessage());
		}
	}
}