Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset user password button to the console #3734

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@
*******************************************************************************/
package org.eclipse.kapua.app.console.module.authentication.client.tabs.credentials;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Label;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

import org.eclipse.kapua.app.console.module.api.client.util.ConsoleInfo;
import org.eclipse.kapua.app.console.module.api.client.util.DateUtils;
import org.eclipse.kapua.app.console.module.api.client.util.DialogUtils;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.KapuaSafeHtmlUtils;
import org.eclipse.kapua.app.console.module.api.client.util.validator.ConfirmPasswordUpdateFieldValidator;
import org.eclipse.kapua.app.console.module.api.client.util.validator.PasswordUpdateFieldValidator;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredential;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredentialType;
Expand Down Expand Up @@ -106,37 +99,17 @@ private void loadCredential() {
@Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
password.setVisible(false);
confirmPassword.setVisible(false);
passwordTooltip.setVisible(false);
credentialFormPanel.remove(credentialType);
credentialTypeLabel.setVisible(true);
credentialTypeLabel.setValue(selectedCredential.getCredentialType());
password.setFieldLabel(MSGS.dialogEditFieldNewPassword());
password.setAllowBlank(true);
password.addListener(Events.Change, new Listener<BaseEvent>() {

@Override
public void handleEvent(BaseEvent be) {
confirmPassword.setAllowBlank(password.getValue() == null || password.getValue().equals(""));
}
});
confirmPassword.setFieldLabel(MSGS.dialogEditFieldConfirmNewPassword());
confirmPassword.setAllowBlank(true);
if (selectedCredential.getLockoutReset() != null && selectedCredential.getLockoutReset().after(new Date())) {
lockedUntil.setText(MSGS.dialogEditLockedUntil(DateUtils.formatDateTime(selectedCredential.getLockoutReset())));
credentialFormPanel.add(lockedUntil);
}
GWT_CREDENTIAL_SERVICE.getMinPasswordLength(selectedCredential.getScopeId(), new AsyncCallback<Integer>() {

@Override
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}

@Override
public void onSuccess(Integer result) {
confirmPassword.setValidator(new ConfirmPasswordUpdateFieldValidator(confirmPassword, password, result));
password.setValidator(new PasswordUpdateFieldValidator(password, result));
}
});
DialogUtils.resizeDialog(this, 400, 230);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.eclipse.kapua.app.console.module.authentication.client.messages.ConsoleCredentialMessages;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredential;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredentialQuery;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredentialType;
import org.eclipse.kapua.app.console.module.authentication.shared.model.permission.CredentialSessionPermission;
import org.eclipse.kapua.app.console.module.authentication.shared.service.GwtCredentialService;
import org.eclipse.kapua.app.console.module.authentication.shared.service.GwtCredentialServiceAsync;
Expand Down Expand Up @@ -237,6 +238,10 @@ private void updateToolbarButtons() {
getSelectionModel().getSelectedItem() != null &&
getSelectionModel().getSelectedItem().getLockoutReset() != null &&
currentSession.hasPermission(CredentialSessionPermission.write()));
getToolbar().getResetPasswordButton().setEnabled(
getSelectionModel().getSelectedItem() != null &&
getSelectionModel().getSelectedItem().getCredentialTypeEnum().equals(GwtCredentialType.PASSWORD) &&
currentSession.hasPermission(CredentialSessionPermission.write()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*******************************************************************************
* Copyright (c) 2023, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.authentication.client.tabs.credentials;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Label;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.eclipse.kapua.app.console.module.api.client.util.ConsoleInfo;
import org.eclipse.kapua.app.console.module.api.client.util.DateUtils;
import org.eclipse.kapua.app.console.module.api.client.util.DialogUtils;
import org.eclipse.kapua.app.console.module.api.client.util.FailureHandler;
import org.eclipse.kapua.app.console.module.api.client.util.validator.ConfirmPasswordUpdateFieldValidator;
import org.eclipse.kapua.app.console.module.api.client.util.validator.PasswordUpdateFieldValidator;
import org.eclipse.kapua.app.console.module.api.shared.model.session.GwtSession;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredential;
import org.eclipse.kapua.app.console.module.authentication.shared.model.GwtCredentialType;

import java.util.Date;

public class CredentialResetDialog extends CredentialAddDialog {
private final GwtCredential selectedCredential;

private final Label lockedUntil = new Label();


public CredentialResetDialog(GwtSession currentSession, String selectedUserId, String selectedUserName, GwtCredential selectedCredential) {
super(currentSession, selectedUserId, selectedUserName);
this.selectedCredential = selectedCredential;
}


@Override
public void submit() {
selectedCredential.setCredentialKey(password.getValue());
selectedCredential.setExpirationDate(expirationDate.getValue());
selectedCredential.setCredentialStatus(credentialStatus.getValue().getValue().toString());
selectedCredential.setOptlock(optlock.getValue().intValue());
GWT_CREDENTIAL_SERVICE.resetPassword(xsrfToken, selectedCredential.getScopeId(), selectedCredential.getId(), password.getValue(), new AsyncCallback<Void>() {

@Override
public void onFailure(Throwable caught) {
unmask();

submitButton.enable();
cancelButton.enable();
status.hide();

exitStatus = false;
if (!isPermissionErrorMessage(caught)) {
exitMessage = MSGS.dialogEditError(caught.getLocalizedMessage());
}
}

@Override
public void onSuccess(Void result) {
exitStatus = true;
exitMessage = MSGS.dialogEditConfirmationPassword();

hide();
}
});
}


@Override
public void createBody() {
super.createBody();
loadCredential();
}


private void loadCredential() {
credentialType.setSimpleValue(selectedCredential.getCredentialTypeEnum());
expirationDate.setValue(selectedCredential.getExpirationDate());
credentialStatus.setSimpleValue(selectedCredential.getCredentialStatusEnum());
optlock.setValue(selectedCredential.getOptlock());
if (selectedCredential.getCredentialTypeEnum() == GwtCredentialType.API_KEY) {
expirationDate.setToolTip(MSGS.dialogAddFieldExpirationDateApiKeyTooltip());
credentialStatus.setToolTip(MSGS.dialogAddStatusApiKeyTooltip());
} else if (selectedCredential.getCredentialTypeEnum() == GwtCredentialType.PASSWORD) {
passwordTooltip.show();
DialogUtils.resizeDialog(CredentialResetDialog.this, 400, 355);
expirationDate.setToolTip(MSGS.dialogAddFieldExpirationDatePasswordTooltip());
credentialStatus.setToolTip(MSGS.dialogAddStatusPasswordTooltip());
}
}


@Override
protected void onRender(Element parent, int pos) {
super.onRender(parent, pos);
DialogUtils.resizeDialog(this, 400, 250);
password.setVisible(true);
confirmPassword.setVisible(true);
passwordTooltip.setVisible(true);
credentialFormPanel.remove(credentialType);
credentialTypeLabel.setVisible(false);
expirationDate.setVisible(false);
credentialStatus.setVisible(false);
password.setFieldLabel(MSGS.dialogEditFieldNewPassword());
password.setAllowBlank(true);
password.addListener(Events.Change, new Listener<BaseEvent>() {

@Override
public void handleEvent(BaseEvent be) {
confirmPassword.setAllowBlank(password.getValue() == null || password.getValue().equals(""));
}
});
confirmPassword.setFieldLabel(MSGS.dialogEditFieldConfirmNewPassword());
confirmPassword.setAllowBlank(true);
if (selectedCredential.getLockoutReset() != null && selectedCredential.getLockoutReset().after(new Date())) {
lockedUntil.setText(MSGS.dialogEditLockedUntil(DateUtils.formatDateTime(selectedCredential.getLockoutReset())));
credentialFormPanel.add(lockedUntil);
}
GWT_CREDENTIAL_SERVICE.getMinPasswordLength(selectedCredential.getScopeId(), new AsyncCallback<Integer>() {

@Override
public void onFailure(Throwable caught) {
FailureHandler.handle(caught);
}

@Override
public void onSuccess(Integer result) {
confirmPassword.setValidator(new ConfirmPasswordUpdateFieldValidator(confirmPassword, password, result));
password.setValidator(new PasswordUpdateFieldValidator(password, result));
}
});
}


@Override
public void validateUserCredential() {
if (password.getValue() != null && confirmPassword.getValue() == null) {
ConsoleInfo.display(CMSGS.popupError(), MSGS.credentialConfirmPasswordRequired());
} else if (!password.isValid()) {
ConsoleInfo.display(CMSGS.popupError(), password.getErrorMessage());
} else if (password.getValue() != null && !password.getValue().equals(confirmPassword.getValue())) {
ConsoleInfo.display(CMSGS.popupError(), confirmPassword.getErrorMessage());
}
}


@Override
protected void preSubmit() {
super.preSubmit();
}


@Override
public String getHeaderMessage() {
return MSGS.dialogResetPasswordHeader(selectedCredential.getUsername());
}


@Override
public String getInfoMessage() {
return MSGS.dialogResetPassword();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CredentialToolbar extends EntityCRUDToolbar<GwtCredential> {
private String selectedUserName;

private final KapuaButton unlockButton;
private final KapuaButton resetPasswordButton;

private static final ConsoleCredentialMessages MSGS = GWT.create(ConsoleCredentialMessages.class);

Expand All @@ -48,13 +49,25 @@ public void componentSelected(ButtonEvent buttonEvent) {
}
}
});
resetPasswordButton = new KapuaButton(MSGS.resetPasswordButton(), new KapuaIcon(IconSet.UNDO), new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent buttonEvent) {
GwtCredential selectedCredential = gridSelectionModel.getSelectedItem();
if (selectedUserId != null && selectedCredential != null) {
showResetPasswordDialog(selectedCredential);
}
}
});
}

@Override
protected void onRender(Element target, int index) {
super.onRender(target, index);
add(new SeparatorToolItem());
add(getUnlockButton());
add(new SeparatorToolItem());
add(getResetPasswordButton());

updateButtonsEnabled();
getEditEntityButton().disable();
Expand All @@ -64,6 +77,7 @@ protected void onRender(Element target, int index) {
if (getFilterButton() != null) {
getFilterButton().hide();
}
resetPasswordButton.disable();
setBorders(true);
}

Expand Down Expand Up @@ -115,10 +129,22 @@ public KapuaButton getUnlockButton() {
return unlockButton;
}


public KapuaButton getResetPasswordButton() {
return resetPasswordButton;
}


private void showUnlockDialog(GwtCredential selectedCredential) {
CredentialUnlockDialog dialog = new CredentialUnlockDialog(selectedCredential);
dialog.addListener(Events.Hide, getHideDialogListener());
dialog.show();
}


private void showResetPasswordDialog(GwtCredential selectedCredential) {
CredentialResetDialog dialog = new CredentialResetDialog(currentSession, selectedUserId, selectedUserName, selectedCredential);
dialog.addListener(Events.Hide, getHideDialogListener());
dialog.show();
}
}
Loading