Skip to content

Commit

Permalink
one more captcha fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kateryna Honchar committed May 29, 2023
1 parent 469df8e commit 407b890
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.Model;

import com.evolveum.midpoint.gui.api.component.BasePanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.web.component.message.FeedbackAlerts;

public class CaptchaPanel extends BasePanel<Void> {

private static final long serialVersionUID = 1L;

private static final String CAPTCHA_TEXT_ID = "text";
private static final String CAPTCHA_IMAGE_ID = "image";
/**
* The text provided by the user.
*/
Expand All @@ -49,7 +50,7 @@ public CaptchaPanel(String id, PageAdminLTE pageBase) {
add(feedback);

captchaImageResource = createCaptchaImageResource();
final Image captchaImage = new Image("image", captchaImageResource);
final Image captchaImage = new Image(CAPTCHA_IMAGE_ID, captchaImageResource);
captchaImage.setOutputMarkupId(true);
add(captchaImage);

Expand All @@ -69,8 +70,7 @@ public void onClick(AjaxRequestTarget target) {
add(new Label("textDescriptionLabel",
pageBase.createStringResource("CaptchaPanel.textDescriptionLabel")));

add(new RequiredTextField<String>("text",
new PropertyModel<>(CaptchaPanel.this, "captchaText"), String.class) {
add(new RequiredTextField<String>(CAPTCHA_TEXT_ID, Model.of()) {
private static final long serialVersionUID = 1L;

@Override
Expand Down Expand Up @@ -104,7 +104,8 @@ static String randomString() {
}

public String getCaptchaText() {
return captchaText;
RequiredTextField<String> captchaField = (RequiredTextField) get(CAPTCHA_TEXT_ID);
return captchaField.getInput();
}

public String getRandomText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.AjaxButton;
import com.evolveum.midpoint.web.component.AjaxSubmitButton;
import com.evolveum.midpoint.web.component.form.MidpointForm;
import com.evolveum.midpoint.web.component.prism.DynamicFormPanel;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;

Expand Down Expand Up @@ -118,7 +116,6 @@ private void doRegistration(AjaxRequestTarget target) {
if (!validateCaptcha(target)) {
return;
}

submitRegistration(target);
isSubmitted = true;
}
Expand All @@ -132,7 +129,7 @@ private boolean validateCaptcha(AjaxRequestTarget target) {
}

CaptchaPanel captcha = getCaptcha();
if (captcha.getRandomText() == null) {
if (captcha.getRandomText() == null || captcha.getCaptchaText() == null) {
String message = createStringResource("PageSelfRegistration.captcha.validation.failed")
.getString();
LOGGER.error(message);
Expand All @@ -142,16 +139,14 @@ private boolean validateCaptcha(AjaxRequestTarget target) {
return false;
}

if (captcha.getCaptchaText() != null && captcha.getRandomText() != null) {
if (!captcha.getCaptchaText().equals(captcha.getRandomText())) {
String message = createStringResource("PageSelfRegistration.captcha.validation.failed")
.getString();
LOGGER.error(message);
getSession().error(message);
updateCaptcha(target);
target.add(getFeedbackPanel());
return false;
}
if (!captcha.getCaptchaText().equals(captcha.getRandomText())) {
String message = createStringResource("PageSelfRegistration.captcha.validation.failed")
.getString();
LOGGER.error(message);
getSession().error(message);
updateCaptcha(target);
target.add(getFeedbackPanel());
return false;
}
LOGGER.trace("CAPTCHA Validation OK");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,7 @@ private void initInputProperties(FeedbackPanel feedback, String placeholderKey,
input.getBaseFormComponent().add(new EmptyOnBlurAjaxFormUpdatingBehaviour());
input.getBaseFormComponent().setRequired(true);
feedback.setFilter(new ContainerFeedbackMessageFilter(input.getBaseFormComponent()));

input.add(new VisibleEnableBehaviour() {

private static final long serialVersionUID = 1L;

@Override
public boolean isEnabled() {
return getUserModel().getObject() == null;
}

});
input.setRenderBodyOnly(true);

}

private void createPasswordPanel(WebMarkupContainer staticRegistrationForm) {
Expand Down

0 comments on commit 407b890

Please sign in to comment.