/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.bricket.plugin.authentication.web;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.string.StringValue;
import org.bricket.plugin.authentication.service.AuthenticationService;
import org.bricket.service.BricketServiceException;
import org.bricket.web.BricketPanel;
import brix.web.nodepage.BrixPageParameters;
import brix.web.nodepage.PageParametersAware;
/**
* @author Ingo Renner
* @author Henning Teek
*/
public class ActivateUserPanel extends BricketPanel implements PageParametersAware {
@SpringBean
private AuthenticationService authenticationService;
private String email;
private String activationKey;
public ActivateUserPanel(String id) {
super(id);
}
@Override
protected void initGui() {
Form<Void> form = new Form<Void>("form");
StringResourceModel lemail = new StringResourceModel("email.label", ActivateUserPanel.this, null);
form.add(new Label("email.label", lemail));
form.add(new TextField<String>("email", new PropertyModel<String>(this, "email")).setRequired(true).setLabel(
lemail));
StringResourceModel lactivationKey = new StringResourceModel("activationKey.label", ActivateUserPanel.this,
null);
form.add(new Label("activationKey.label", lactivationKey));
form.add(new TextField<String>("activationKey", new PropertyModel<String>(this, "activationKey")).setRequired(
true).setLabel(lactivationKey));
form.add(new CancelAjaxLink("cancel"));
form.add(new AjaxButton("submit") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
getSession().cleanupFeedbackMessages();
try {
authenticationService.activateUser(email, activationKey);
getSession().info(
new StringResourceModel("feedback.success", ActivateUserPanel.this, null).getString());
setResponsePage(getApplication().getHomePage());
} catch (BricketServiceException e) {
form.error(new StringResourceModel(e.getKey(), ActivateUserPanel.this, null).getString());
target.addComponent(getFeedback());
}
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {
target.addComponent(getFeedback());
}
});
add(form);
}
private static final class CancelAjaxLink extends AjaxLink<Void> {
private CancelAjaxLink(String id) {
super(id);
}
@Override
public void onClick(AjaxRequestTarget target) {
getSession().cleanupFeedbackMessages();
setResponsePage(getApplication().getHomePage());
}
}
@Override
public void contributeToPageParameters(BrixPageParameters params) {
}
@Override
public void initializeFromPageParameters(BrixPageParameters params) {
StringValue emailParam = params.getQueryParam("email");
if (!emailParam.isEmpty()) {
this.email = emailParam.toString();
}
StringValue keyParam = params.getQueryParam("key");
if (!keyParam.isEmpty()) {
this.activationKey = keyParam.toString();
}
if (!emailParam.isEmpty() && !keyParam.isEmpty()) {
getSession().cleanupFeedbackMessages();
try {
authenticationService.activateUser(email, activationKey);
getSession()
.info(new StringResourceModel("feedback.success", ActivateUserPanel.this, null).getString());
getRequestCycle().setRequestTarget(new RedirectRequestTarget(getI18nLink("/login.html")));
} catch (BricketServiceException e) {
error(new StringResourceModel(e.getKey(), ActivateUserPanel.this, null).getString());
}
}
}
}