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.panels;
20  
21  import java.util.List;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.syncope.client.enduser.SyncopeEnduserSession;
24  import org.apache.syncope.client.enduser.SyncopeWebApplication;
25  import org.apache.syncope.client.enduser.commons.EnduserConstants;
26  import org.apache.syncope.client.enduser.commons.ProvisioningUtils;
27  import org.apache.syncope.client.enduser.layout.UserFormLayoutInfo;
28  import org.apache.syncope.client.enduser.pages.BasePage;
29  import org.apache.syncope.client.enduser.pages.SelfResult;
30  import org.apache.syncope.client.enduser.panels.any.Details;
31  import org.apache.syncope.client.enduser.panels.any.SelfUserDetails;
32  import org.apache.syncope.client.ui.commons.Constants;
33  import org.apache.syncope.client.ui.commons.pages.BaseWebPage;
34  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
35  import org.apache.syncope.common.lib.EntityTOUtils;
36  import org.apache.syncope.common.lib.SyncopeClientException;
37  import org.apache.syncope.common.lib.request.UserCR;
38  import org.apache.syncope.common.lib.to.ProvisioningResult;
39  import org.apache.syncope.common.lib.to.SecurityQuestionTO;
40  import org.apache.syncope.common.lib.to.UserTO;
41  import org.apache.syncope.common.lib.types.ExecStatus;
42  import org.apache.syncope.common.rest.api.service.SecurityQuestionService;
43  import org.apache.wicket.PageReference;
44  import org.apache.wicket.ajax.AjaxRequestTarget;
45  import org.apache.wicket.markup.html.form.TextField;
46  import org.apache.wicket.model.Model;
47  
48  public class UserSelfFormPanel extends UserFormPanel {
49  
50      private static final long serialVersionUID = 6763365006334514387L;
51  
52      protected TextField<String> securityQuestion;
53  
54      protected String usernameText;
55  
56      public UserSelfFormPanel(
57              final String id,
58              final UserTO previousUserTO,
59              final UserTO userTO,
60              final List<String> anyTypeClasses,
61              final UserFormLayoutInfo formLayoutInfo,
62              final PageReference pageReference) {
63          super(id, previousUserTO, userTO, anyTypeClasses, formLayoutInfo, pageReference);
64      }
65  
66      @Override
67      protected Details<UserTO> addOptionalDetailsPanel(final UserWrapper modelObject) {
68          return new SelfUserDetails(
69                  EnduserConstants.CONTENT_PANEL,
70                  UserWrapper.class.cast(modelObject),
71                  UserFormLayoutInfo.class.cast(formLayoutInfo).isPasswordManagement(),
72                  pageRef);
73      }
74  
75      @Override
76      protected void onFormSubmit(final AjaxRequestTarget target) {
77          // first check captcha
78          if (SyncopeWebApplication.get().isCaptchaEnabled() && !captcha.check()) {
79              SyncopeEnduserSession.get().error(getString(Constants.CAPTCHA_ERROR));
80              ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
81          } else {
82              UserTO userTO = form.getModelObject().getInnerObject();
83              try {
84                  // create and set page paramters according to provisioning result
85                  UserCR req = new UserCR();
86                  EntityTOUtils.toAnyCR(userTO, req);
87                  req.setStorePassword(form.getModelObject() instanceof UserWrapper
88                          ? UserWrapper.class.cast(form.getModelObject()).isStorePasswordInSyncope()
89                          : StringUtils.isNotBlank(userTO.getPassword()));
90                  // perform request and pass propagation statuses to SelfResult page
91                  ProvisioningResult<UserTO> provisioningResult = userSelfRestClient.create(req);
92                  setResponsePage(new SelfResult(provisioningResult,
93                          ProvisioningUtils.managePageParams(UserSelfFormPanel.this, "profile.change",
94                                  !SyncopeWebApplication.get().isReportPropagationErrors()
95                                  || provisioningResult.getPropagationStatuses().stream()
96                                          .allMatch(ps -> ExecStatus.SUCCESS == ps.getStatus()))));
97              } catch (SyncopeClientException e) {
98                  LOG.error("While creating user {}", userTO.getUsername(), e);
99                  SyncopeEnduserSession.get().onException(e);
100                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
101             }
102         }
103     }
104 
105     protected void loadSecurityQuestion(final PageReference pageRef, final AjaxRequestTarget target) {
106         try {
107             SecurityQuestionTO securityQuestionTO = SyncopeEnduserSession.get().getService(
108                     SecurityQuestionService.class).readByUser(usernameText);
109             // set security question field model
110             securityQuestion.setModel(Model.of(securityQuestionTO.getContent()));
111             target.add(securityQuestion);
112         } catch (Exception e) {
113             LOG.error("Unable to get security question for [{}]", usernameText, e);
114             SyncopeEnduserSession.get().onException(e);
115             ((BaseWebPage) pageRef.getPage()).getNotificationPanel().refresh(target);
116         }
117     }
118 }