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.console.wizards.any;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import java.util.Optional;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.syncope.client.console.SyncopeWebApplication;
26  import org.apache.syncope.client.console.layout.UserFormLayoutInfo;
27  import org.apache.syncope.client.console.rest.UserRestClient;
28  import org.apache.syncope.client.ui.commons.layout.UserForm;
29  import org.apache.syncope.client.ui.commons.wizards.AjaxWizard;
30  import org.apache.syncope.client.ui.commons.wizards.any.AnyWrapper;
31  import org.apache.syncope.client.ui.commons.wizards.any.UserWrapper;
32  import org.apache.syncope.common.lib.AnyOperations;
33  import org.apache.syncope.common.lib.EntityTOUtils;
34  import org.apache.syncope.common.lib.request.PasswordPatch;
35  import org.apache.syncope.common.lib.request.UserCR;
36  import org.apache.syncope.common.lib.request.UserUR;
37  import org.apache.syncope.common.lib.to.ProvisioningResult;
38  import org.apache.syncope.common.lib.to.UserTO;
39  import org.apache.wicket.PageReference;
40  
41  public class UserWizardBuilder extends AnyWizardBuilder<UserTO> implements UserForm {
42  
43      private static final long serialVersionUID = 6716803168859873877L;
44  
45      protected final UserRestClient userRestClient;
46  
47      public UserWizardBuilder(
48              final List<String> anyTypeClasses,
49              final UserFormLayoutInfo formLayoutInfo,
50              final UserRestClient userRestClient,
51              final PageReference pageRef) {
52  
53          super(new UserWrapper(null), anyTypeClasses, formLayoutInfo, pageRef);
54          this.userRestClient = userRestClient;
55      }
56  
57      public UserWizardBuilder(
58              final UserTO previousUserTO,
59              final UserTO userTO,
60              final List<String> anyTypeClasses,
61              final UserFormLayoutInfo formLayoutInfo,
62              final UserRestClient userRestClient,
63              final PageReference pageRef) {
64  
65          super(new UserWrapper(previousUserTO, userTO), anyTypeClasses, formLayoutInfo, pageRef);
66          this.userRestClient = userRestClient;
67      }
68  
69      @Override
70      protected Serializable onApplyInternal(final AnyWrapper<UserTO> modelObject) {
71          UserTO inner = modelObject.getInnerObject();
72  
73          ProvisioningResult<UserTO> result;
74          if (inner.getKey() == null) {
75              UserCR req = new UserCR();
76              EntityTOUtils.toAnyCR(inner, req);
77              req.setStorePassword(modelObject instanceof UserWrapper
78                      ? UserWrapper.class.cast(modelObject).isStorePasswordInSyncope()
79                      : StringUtils.isNotBlank(inner.getPassword()));
80  
81              result = userRestClient.create(req);
82          } else {
83              fixPlainAndVirAttrs(inner, getOriginalItem().getInnerObject());
84              UserUR userUR = AnyOperations.diff(inner, getOriginalItem().getInnerObject(), false);
85  
86              if (StringUtils.isNotBlank(inner.getPassword())) {
87                  PasswordPatch passwordPatch = new PasswordPatch.Builder().
88                          value(inner.getPassword()).onSyncope(true).resources(inner.getResources()).build();
89                  userUR.setPassword(passwordPatch);
90              }
91  
92              // update just if it is changed
93              if (userUR.isEmpty()) {
94                  result = new ProvisioningResult<>();
95                  result.setEntity(inner);
96              } else {
97                  List<UserFormFinalizer> finalizers = SyncopeWebApplication.get().getFormFinalizers(mode);
98  
99                  finalizers.forEach(finalizer -> finalizer.beforeUpdate(userUR.getKey()));
100 
101                 result = userRestClient.update(getOriginalItem().getInnerObject().getETagValue(), userUR);
102 
103                 finalizers.forEach(finalizer -> finalizer.afterUpdate(userUR.getKey()));
104             }
105         }
106 
107         return result;
108     }
109 
110     @Override
111     protected Optional<Details<UserTO>> addOptionalDetailsPanel(final AnyWrapper<UserTO> modelObject) {
112         return Optional.of(new UserDetails(
113                 UserWrapper.class.cast(modelObject),
114                 mode == AjaxWizard.Mode.TEMPLATE,
115                 modelObject.getInnerObject().getKey() != null,
116                 UserFormLayoutInfo.class.cast(formLayoutInfo).isPasswordManagement(),
117                 pageRef));
118     }
119 
120     /**
121      * Overrides default setItem() in order to clean statusModel as well.
122      *
123      * @param item item to be set.
124      * @return the current wizard.
125      */
126     @Override
127     public UserWizardBuilder setItem(final AnyWrapper<UserTO> item) {
128         super.setItem(Optional.ofNullable(item).
129                 map(userTOAnyWrapper -> new UserWrapper(userTOAnyWrapper.getInnerObject())).orElse(null));
130         return this;
131     }
132 }