View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.enduser.pages;
20  
21  import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.password.strength.PasswordStrengthBehavior;
22  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
23  import org.apache.syncope.client.enduser.commons.EnduserConstants;
24  import org.apache.syncope.client.ui.commons.Constants;
25  import org.apache.syncope.client.ui.commons.markup.html.form.SyncopePasswordStrengthConfig;
26  import org.apache.syncope.client.ui.commons.panels.CardPanel;
27  import org.apache.syncope.client.ui.commons.wizards.any.PasswordPanel;
28  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
29  import org.apache.syncope.common.lib.SyncopeClientException;
30  import org.apache.syncope.common.lib.to.UserTO;
31  import org.apache.syncope.common.rest.api.service.UserSelfService;
32  import org.apache.wicket.ajax.AjaxRequestTarget;
33  import org.apache.wicket.ajax.markup.html.form.AjaxButton;
34  import org.apache.wicket.markup.html.WebMarkupContainer;
35  import org.apache.wicket.markup.html.form.Button;
36  import org.apache.wicket.markup.html.form.Form;
37  import org.apache.wicket.markup.html.form.StatelessForm;
38  import org.apache.wicket.model.Model;
39  import org.apache.wicket.request.mapper.parameter.PageParameters;
40  
41  public class SelfConfirmPasswordReset extends BasePage {
42  
43      private static final long serialVersionUID = -2166782304542750726L;
44  
45      private static final String CONFIRM_PASSWORD_RESET = "confirmPasswordReset";
46  
47      public SelfConfirmPasswordReset(final PageParameters parameters) {
48          super(parameters, CONFIRM_PASSWORD_RESET);
49  
50          setDomain(parameters);
51          disableSidebar();
52  
53          if (parameters == null || parameters.get("token").isEmpty()) {
54              LOG.error("No token parameter found in the request url");
55  
56              PageParameters homeParameters = new PageParameters();
57              homeParameters.add("errorMessage", getString("self.confirm.pwd.reset.error.empty"));
58              setResponsePage(getApplication().getHomePage(), homeParameters);
59          }
60  
61          WebMarkupContainer content = new WebMarkupContainer("content");
62          content.setOutputMarkupId(true);
63          contentWrapper.add(content);
64  
65          Form<?> form = new StatelessForm<>("selfConfirmPwdResetForm");
66          form.setOutputMarkupId(true);
67          content.add(form);
68  
69          UserTO fakeUserTO = new UserTO();
70          PasswordPanel passwordPanel = new PasswordPanel(
71                  EnduserConstants.CONTENT_PANEL,
72                  new UserWrapper(fakeUserTO),
73                  false,
74                  false,
75                  new PasswordStrengthBehavior(new SyncopePasswordStrengthConfig()));
76          passwordPanel.setOutputMarkupId(true);
77  
78          form.add(new CardPanel.Builder<PasswordPanel>()
79                  .setName("selfConfirmPasswordResetPanel")
80                  .setComponent(passwordPanel)
81                  .isVisible(true)
82                  .build("selfConfirmPasswordResetPanelCard"));
83  
84          AjaxButton submit = new AjaxButton("submit", new Model<>(getString("submit"))) {
85  
86              private static final long serialVersionUID = 509325877101838812L;
87  
88              @Override
89              protected void onSubmit(final AjaxRequestTarget target) {
90                  PageParameters params = new PageParameters();
91                  try {
92                      SyncopeEnduserSession.get().getService(UserSelfService.class).confirmPasswordReset(
93                              parameters.get("token").toString(), fakeUserTO.getPassword());
94                      params.add(EnduserConstants.STATUS, Constants.OPERATION_SUCCEEDED);
95                      params.add(Constants.NOTIFICATION_TITLE_PARAM, getString("self.confirm.pwd.reset.success"));
96                      params.add(Constants.NOTIFICATION_MSG_PARAM, getString("self.confirm.pwd.reset.success.msg"));
97                      SyncopeEnduserSession.get().success(getString(Constants.OPERATION_SUCCEEDED));
98                      parameters.add(EnduserConstants.LANDING_PAGE, Login.class.getName());
99                      setResponsePage(SelfResult.class, params);
100                 } catch (SyncopeClientException sce) {
101                     LOG.error("Unable to complete the 'Password Reset Confirmation' process", sce);
102                     params.add(EnduserConstants.STATUS, Constants.OPERATION_ERROR);
103                     params.add(Constants.NOTIFICATION_TITLE_PARAM, getString("self.confirm.pwd.reset.error"));
104                     params.add(Constants.NOTIFICATION_MSG_PARAM, getString("self.confirm.pwd.reset.error.msg"));
105                     SyncopeEnduserSession.get().onException(sce);
106                     ((BasePage) getPageReference().getPage()).getNotificationPanel().refresh(target);
107                 }
108             }
109 
110             @Override
111             protected void onError(final AjaxRequestTarget target) {
112                 notificationPanel.refresh(target);
113             }
114         };
115         form.setDefaultButton(submit);
116         form.add(submit);
117 
118         Button cancel = new Button("cancel") {
119 
120             private static final long serialVersionUID = 3669569969172391336L;
121 
122             @Override
123             public void onSubmit() {
124                 setResponsePage(getApplication().getHomePage());
125             }
126         };
127         cancel.setOutputMarkupId(true);
128         cancel.setDefaultFormProcessing(false);
129         form.add(cancel);
130     }
131 }