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.syncope.client.enduser.SyncopeEnduserSession;
23  import org.apache.syncope.client.enduser.SyncopeWebApplication;
24  import org.apache.syncope.client.enduser.commons.EnduserConstants;
25  import org.apache.syncope.client.enduser.commons.ProvisioningUtils;
26  import org.apache.syncope.client.enduser.layout.UserFormLayoutInfo;
27  import org.apache.syncope.client.enduser.pages.BasePage;
28  import org.apache.syncope.client.enduser.pages.SelfResult;
29  import org.apache.syncope.client.enduser.panels.any.Details;
30  import org.apache.syncope.client.enduser.panels.any.UserDetails;
31  import org.apache.syncope.client.enduser.rest.UserSelfRestClient;
32  import org.apache.syncope.client.ui.commons.Constants;
33  import org.apache.syncope.client.ui.commons.layout.UserForm;
34  import org.apache.syncope.client.ui.commons.panels.WizardModalPanel;
35  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
36  import org.apache.syncope.client.ui.commons.wizards.ModalPanelBuilder;
37  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
38  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
39  import org.apache.syncope.common.lib.AnyOperations;
40  import org.apache.syncope.common.lib.SyncopeClientException;
41  import org.apache.syncope.common.lib.request.UserUR;
42  import org.apache.syncope.common.lib.to.ProvisioningResult;
43  import org.apache.syncope.common.lib.to.UserTO;
44  import org.apache.syncope.common.lib.types.ExecStatus;
45  import org.apache.wicket.PageReference;
46  import org.apache.wicket.ajax.AjaxRequestTarget;
47  import org.apache.wicket.event.IEventSink;
48  import org.apache.wicket.spring.injection.annot.SpringBean;
49  
50  public class UserFormPanel extends AnyFormPanel implements UserForm {
51  
52      private static final long serialVersionUID = 6763365006334514387L;
53  
54      @SpringBean
55      protected UserSelfRestClient userSelfRestClient;
56  
57      public UserFormPanel(
58              final String id,
59              final UserTO userTO,
60              final List<String> anyTypeClasses,
61              final UserFormLayoutInfo formLayoutInfo,
62              final PageReference pageReference) {
63  
64          super(id, new UserWrapper(userTO), anyTypeClasses, formLayoutInfo, pageReference);
65  
66          UserWrapper modelObj = newModelObject();
67          buildLayout(modelObj);
68      }
69  
70      public UserFormPanel(
71              final String id,
72              final UserTO previousUserTO,
73              final UserTO userTO,
74              final List<String> anyTypeClasses,
75              final UserFormLayoutInfo formLayoutInfo,
76              final PageReference pageReference) {
77  
78          super(id, new UserWrapper(previousUserTO, userTO), anyTypeClasses, formLayoutInfo, pageReference);
79  
80          UserWrapper modelObj = newModelObject();
81          setFormModel(modelObj);
82          buildLayout(modelObj);
83      }
84  
85      @Override
86      protected Details<UserTO> addOptionalDetailsPanel(final UserWrapper modelObject) {
87          return new UserDetails(
88                  EnduserConstants.CONTENT_PANEL,
89                  UserWrapper.class.cast(modelObject),
90                  pageRef);
91      }
92  
93      @Override
94      protected void onFormSubmit(final AjaxRequestTarget target) {
95          // captcha check
96          boolean checked = true;
97          if (SyncopeWebApplication.get().isCaptchaEnabled()) {
98              checked = captcha.check();
99          }
100         if (!checked) {
101             SyncopeEnduserSession.get().error(getString(Constants.CAPTCHA_ERROR));
102             ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
103         } else {
104             try {
105                 AnyWrapper<UserTO> updatedWrapper = form.getModelObject();
106                 UserTO userTO = updatedWrapper.getInnerObject();
107 
108                 fixPlainAndVirAttrs(userTO, getOriginalItem().getInnerObject());
109                 // update and set page paramters according to provisioning result
110                 UserUR updateReq = AnyOperations.diff(userTO, getOriginalItem().getInnerObject(), false);
111                 ProvisioningResult<UserTO> provisioningResult = updateReq.isEmpty()
112                         ? new ProvisioningResult<>()
113                         : userSelfRestClient.update(getOriginalItem().getInnerObject().getETagValue(), updateReq);
114                 setResponsePage(new SelfResult(provisioningResult,
115                         ProvisioningUtils.managePageParams(UserFormPanel.this, "profile.change",
116                                 !SyncopeWebApplication.get().isReportPropagationErrors()
117                                 || provisioningResult.getPropagationStatuses().stream()
118                                         .allMatch(ps -> ExecStatus.SUCCESS == ps.getStatus()))));
119             } catch (SyncopeClientException e) {
120                 LOG.error("While changing password for {}",
121                         SyncopeEnduserSession.get().getSelfTO().getUsername(), e);
122                 SyncopeEnduserSession.get().onException(e);
123                 ((BasePage) pageRef.getPage()).getNotificationPanel().refresh(target);
124             }
125         }
126     }
127 
128     @Override
129     public IEventSink getEventSink() {
130         return null;
131     }
132 
133     @Override
134     public ModalPanelBuilder<AnyWrapper<UserTO>> setEventSink(final IEventSink eventSink) {
135         return null;
136     }
137 
138     @Override
139     public ModalPanelBuilder<AnyWrapper<UserTO>> setItem(final AnyWrapper<UserTO> item) {
140         return null;
141     }
142 
143     @Override
144     public AnyWrapper<UserTO> getDefaultItem() {
145         return null;
146     }
147 
148     @Override
149     public WizardModalPanel<AnyWrapper<UserTO>> build(final String id, final int index, final AjaxWizard.Mode mode) {
150         return null;
151     }
152 }