Skip to content

Commit

Permalink
password panel improvements - we do not need to serialize value polic…
Browse files Browse the repository at this point in the history
…y. (cherry-picked)
  • Loading branch information
katkav committed Mar 17, 2022
1 parent c0dc500 commit 2e610e5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ protected void populateItem(ListItem<StringLimitationResult> item) {
StringLimitationPanel limitationPanel = new StringLimitationPanel(ID_VALIDATION_ITEM, item.getModel());
limitationPanel.setOutputMarkupId(true);
item.add(limitationPanel);
item.add(AttributeModifier.append("class", new IModel<String>() {
@Override
public String getObject() {
return Boolean.TRUE.equals(item.getModelObject().isSuccess()) ? " text-success" : " text-danger";
}
}));
item.add(AttributeModifier.append("class", (IModel<String>) () -> Boolean.TRUE.equals(item.getModelObject().isSuccess()) ? " text-success" : " text-danger"));
}
};
validationItems.setOutputMarkupId(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.validation.IValidatable;
import org.apache.wicket.validation.IValidator;
import org.apache.wicket.validation.ValidationError;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.authentication.api.util.AuthUtil;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.validator.StringLimitationResult;
Expand All @@ -47,6 +48,7 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.prism.InputPanel;
import com.evolveum.midpoint.web.component.util.EnableBehaviour;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.admin.PageAdminFocus;
import com.evolveum.midpoint.web.page.self.PageOrgSelfProfile;
Expand Down Expand Up @@ -84,27 +86,24 @@ public class PasswordPanel extends InputPanel {
private boolean passwordInputVisible;
private static boolean clearPasswordInput = false;
private static boolean setPasswordInput = false;
private final PageBase pageBase;
private final IModel<ProtectedStringType> model;

public PasswordPanel(String id, IModel<ProtectedStringType> model) {
this(id, model, false, model == null || model.getObject() == null);
}

public <F extends FocusType> PasswordPanel(String id, IModel<ProtectedStringType> model, PrismObject<F> object,
PageBase pageBase) {
this(id, model, false, model == null || model.getObject() == null, object, pageBase);
public <F extends FocusType> PasswordPanel(String id, IModel<ProtectedStringType> model, PrismObject<F> object) {
this(id, model, false, model == null || model.getObject() == null, object);
}

public <F extends FocusType> PasswordPanel(String id, IModel<ProtectedStringType> model, boolean isReadOnly, boolean isInputVisible) {
this(id, model, isReadOnly, isInputVisible, null, null);
public PasswordPanel(String id, IModel<ProtectedStringType> model, boolean isReadOnly, boolean isInputVisible) {
this(id, model, isReadOnly, isInputVisible, null);
}

public <F extends FocusType> PasswordPanel(String id, IModel<ProtectedStringType> model, boolean isReadOnly, boolean isInputVisible,
PrismObject<F> object, PageBase pageBase) {
PrismObject<F> object) {
super(id);
this.passwordInputVisible = isInputVisible;
this.pageBase = pageBase;
this.model = model;
initLayout(isReadOnly, object);
}
Expand All @@ -127,10 +126,10 @@ public boolean isVisible() {
inputContainer.setOutputMarkupId(true);
add(inputContainer);

ValuePolicyType valuePolicy = getValuePolicy(object);
LoadableModel<List<StringLimitationResult>> limitationsModel = new LoadableModel<>() {
LoadableDetachableModel<List<StringLimitationResult>> limitationsModel = new LoadableDetachableModel<>() {
@Override
protected List<StringLimitationResult> load() {
ValuePolicyType valuePolicy = getValuePolicy(object);
return getLimitationsForActualPassword(valuePolicy, object);
}
};
Expand All @@ -151,16 +150,7 @@ protected void onComponentTag(ComponentTag tag) {
}

};
password1.add(AttributeAppender.append("onfocus", "initPasswordValidation({\n"
+ "container: $('#progress-bar-container'),\n"
+ "hierarchy: {\n"
+ " '0': ['progress-bar-danger', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.veryWeak").getString() + "'],\n"
+ " '25': ['progress-bar-danger', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.weak").getString() + "'],\n"
+ " '50': ['progress-bar-warning', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.good").getString() + "'],\n"
+ " '75': ['progress-bar-success', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.strong").getString() + "'],\n"
+ " '100': ['progress-bar-success', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.veryStrong").getString() + "']\n"
+ "}\n"
+ "})"));
password1.add(AttributeAppender.append("onfocus", initPasswordValidation()));
password1.setRequired(false);
password1.setOutputMarkupId(true);
password1.add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
Expand All @@ -183,19 +173,7 @@ protected void onUpdate(AjaxRequestTarget target) {
}
});

IModel<String> password2ValidationModel = (IModel<String>) () -> {
String s1 = password1.getModelObject();
String s2 = password2.getValue();

if (StringUtils.isEmpty(s1) || StringUtils.isEmpty(s2)) {
return "";
}

if (!Objects.equals(s1, s2)) {
return PageBase.createStringResourceStatic("passwordPanel.error").getString();
}
return "";
};
IModel<String> password2ValidationModel = () -> getPasswordMatched(password1.getModelObject(), password2.getValue());
Label password2ValidationMessage = new Label(ID_PASSWORD_TWO_VALIDATION_MESSAGE, password2ValidationModel);
password2ValidationMessage.setOutputMarkupId(true);
inputContainer.add(password2ValidationMessage);
Expand All @@ -205,7 +183,7 @@ protected void onUpdate(AjaxRequestTarget target) {

@Override
protected void onUpdate(AjaxRequestTarget target) {
limitationsModel.reset();
// limitationsModel.reset();
validationPanel.refreshItems(target);
updatePasswordValidation(target);
target.add(password2ValidationMessage);
Expand Down Expand Up @@ -256,9 +234,7 @@ public boolean isVisible() {
passwordRemoveLabel.setVisible(false);
linkContainer.add(passwordRemoveLabel);

AjaxLink<Void> link = new AjaxLink<Void>(ID_CHANGE_PASSWORD_LINK) {
private static final long serialVersionUID = 1L;

AjaxLink<Void> link = new AjaxLink<>(ID_CHANGE_PASSWORD_LINK) {
@Override
public void onClick(AjaxRequestTarget target) {
clearPasswordInput = true;
Expand All @@ -272,8 +248,6 @@ public boolean isVisible() {
}
};
link.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;

@Override
public boolean isVisible() {
return !isReadOnly;
Expand All @@ -285,18 +259,14 @@ public boolean isVisible() {
linkContainer.add(link);

final WebMarkupContainer removeButtonContainer = new WebMarkupContainer(ID_REMOVE_BUTTON_CONTAINER);
AjaxLink<Void> removePassword = new AjaxLink<Void>(ID_REMOVE_PASSWORD_LINK) {
private static final long serialVersionUID = 1L;

AjaxLink<Void> removePassword = new AjaxLink<>(ID_REMOVE_PASSWORD_LINK) {
@Override
public void onClick(AjaxRequestTarget target) {
onRemovePassword(model, target);
}

};
removePassword.add(new VisibleEnableBehaviour() {
private static final long serialVersionUID = 1L;

@Override
public boolean isVisible() {
PageBase pageBase = getPageBase();
Expand All @@ -307,11 +277,8 @@ public boolean isVisible() {
|| pageBase instanceof PageRoleSelfProfile || pageBase instanceof PageServiceSelfProfile) {
return false;
}
if (pageBase instanceof PageAdminFocus && !((PageAdminFocus) pageBase).isLoggedInFocusPage()
&& model.getObject() != null) {
return true;
}
return false;
return pageBase instanceof PageAdminFocus && !((PageAdminFocus) pageBase).isLoggedInFocusPage()
&& model.getObject() != null;
}
});
removePassword.setBody(new ResourceModel("passwordPanel.passwordRemove"));
Expand All @@ -320,6 +287,30 @@ public boolean isVisible() {
add(removeButtonContainer);
}

private String initPasswordValidation() {
return "initPasswordValidation({\n"
+ "container: $('#progress-bar-container'),\n"
+ "hierarchy: {\n"
+ " '0': ['progress-bar-danger', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.veryWeak").getString() + "'],\n"
+ " '25': ['progress-bar-danger', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.weak").getString() + "'],\n"
+ " '50': ['progress-bar-warning', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.good").getString() + "'],\n"
+ " '75': ['progress-bar-success', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.strong").getString() + "'],\n"
+ " '100': ['progress-bar-success', '" + PageBase.createStringResourceStatic("PasswordPanel.strength.veryStrong").getString() + "']\n"
+ "}\n"
+ "})";
}

private String getPasswordMatched(String password1, String password2) {
if (StringUtils.isEmpty(password1) || StringUtils.isEmpty(password2)) {
return "";
}

if (!Objects.equals(password1, password2)) {
return PageBase.createStringResourceStatic("passwordPanel.error").getString();
}
return "";
}

protected <F extends FocusType> ValuePolicyType getValuePolicy(PrismObject<F> object) {
ValuePolicyType valuePolicyType = null;
try {
Expand All @@ -329,14 +320,9 @@ protected <F extends FocusType> ValuePolicyType getValuePolicy(PrismObject<F> ob
Task task = getPageBase().createSimpleTask("load value policy");
valuePolicyType = searchValuePolicy(object, task);
} else {
valuePolicyType = getPageBase().getSecurityContextManager().runPrivileged(new Producer<ValuePolicyType>() {
private static final long serialVersionUID = 1L;

@Override
public ValuePolicyType run() {
Task task = getPageBase().createAnonymousTask("load value policy");
return searchValuePolicy(object, task);
}
valuePolicyType = getPageBase().getSecurityContextManager().runPrivileged((Producer<ValuePolicyType>) () -> {
Task task = getPageBase().createAnonymousTask("load value policy");
return searchValuePolicy(object, task);
});
}
}
Expand All @@ -363,10 +349,6 @@ private <F extends FocusType> ValuePolicyType searchValuePolicy(PrismObject<F> o
return null;
}

public PageBase getPageBase() {
return pageBase;
}

private void onLinkClick(AjaxRequestTarget target) {
passwordInputVisible = true;
target.add(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected Component createValuePanel(ListItem<PrismPropertyValueWrapper<Protecte
PasswordPanel passwordPanel = new PasswordPanel(ID_PASSWORD_PANEL, new ItemRealValueModel<>(item.getModel()),
getModelObject() != null && getModelObject().isReadOnly(),
item.getModelObject() == null || item.getModelObject().getRealValue() == null,
getPrismObjectParentIfExist(), getPageBase()){
getPrismObjectParentIfExist()){
private static final long serialVersionUID = 1L;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public boolean isEnabled() {
private void createPasswordPanel(WebMarkupContainer staticRegistrationForm) {
// ProtectedStringType initialPassword = null;
PasswordPanel password = new PasswordPanel(ID_PASSWORD,
new PropertyModel<>(getUserModel(), "credentials.password.value"), false, true, null, this);
new PropertyModel<>(getUserModel(), "credentials.password.value"), false, true, null);
password.getBaseFormComponent().add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
password.getBaseFormComponent().setRequired(true);
staticRegistrationForm.add(password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ public boolean isVisible() {
add(passwordLabel);

PasswordPanel passwordPanel = new PasswordPanel(ID_PASSWORD_PANEL, new PropertyModel<>(getModel(), MyPasswordsDto.F_PASSWORD),
getModelObject().getFocus(), getPageBase()) {
getModelObject().getFocus()) {
private static final long serialVersionUID = 1L;

@Override
protected <F extends FocusType> ValuePolicyType getValuePolicy(PrismObject<F> object) {
return getModelObject().getFocusPolicy();
Expand Down

0 comments on commit 2e610e5

Please sign in to comment.