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.core.provisioning.java;
20  
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Map;
24  import java.util.Set;
25  import java.util.stream.Collectors;
26  import org.apache.commons.lang3.tuple.Pair;
27  import org.apache.syncope.common.lib.request.AnyObjectCR;
28  import org.apache.syncope.common.lib.request.AnyObjectUR;
29  import org.apache.syncope.common.lib.to.PropagationStatus;
30  import org.apache.syncope.common.lib.types.AnyTypeKind;
31  import org.apache.syncope.common.lib.types.ResourceOperation;
32  import org.apache.syncope.core.persistence.api.dao.AnyObjectDAO;
33  import org.apache.syncope.core.provisioning.api.AnyObjectProvisioningManager;
34  import org.apache.syncope.core.provisioning.api.PropagationByResource;
35  import org.apache.syncope.core.provisioning.api.VirAttrHandler;
36  import org.apache.syncope.core.provisioning.api.WorkflowResult;
37  import org.apache.syncope.core.provisioning.api.propagation.PropagationManager;
38  import org.apache.syncope.core.provisioning.api.propagation.PropagationReporter;
39  import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor;
40  import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
41  import org.apache.syncope.core.workflow.api.AnyObjectWorkflowAdapter;
42  import org.identityconnectors.framework.common.objects.Attribute;
43  import org.springframework.transaction.annotation.Propagation;
44  import org.springframework.transaction.annotation.Transactional;
45  
46  public class DefaultAnyObjectProvisioningManager implements AnyObjectProvisioningManager {
47  
48      protected final AnyObjectWorkflowAdapter awfAdapter;
49  
50      protected final PropagationManager propagationManager;
51  
52      protected final PropagationTaskExecutor taskExecutor;
53  
54      protected final AnyObjectDAO anyObjectDAO;
55  
56      protected final VirAttrHandler virtAttrHandler;
57  
58      public DefaultAnyObjectProvisioningManager(
59              final AnyObjectWorkflowAdapter awfAdapter,
60              final PropagationManager propagationManager,
61              final PropagationTaskExecutor taskExecutor,
62              final AnyObjectDAO anyObjectDAO,
63              final VirAttrHandler virtAttrHandler) {
64  
65          this.awfAdapter = awfAdapter;
66          this.propagationManager = propagationManager;
67          this.taskExecutor = taskExecutor;
68          this.anyObjectDAO = anyObjectDAO;
69          this.virtAttrHandler = virtAttrHandler;
70      }
71  
72      @Override
73      public Pair<String, List<PropagationStatus>> create(
74              final AnyObjectCR anyObjectCR,
75              final boolean nullPriorityAsync,
76              final String creator,
77              final String context) {
78  
79          return create(anyObjectCR, Set.of(), nullPriorityAsync, creator, context);
80      }
81  
82      @Transactional(propagation = Propagation.REQUIRES_NEW)
83      @Override
84      public Pair<String, List<PropagationStatus>> create(
85              final AnyObjectCR anyObjectCR,
86              final Set<String> excludedResources,
87              final boolean nullPriorityAsync,
88              final String creator,
89              final String context) {
90  
91          WorkflowResult<String> created = awfAdapter.create(anyObjectCR, creator, context);
92  
93          List<PropagationTaskInfo> taskInfos = propagationManager.getCreateTasks(
94                  AnyTypeKind.ANY_OBJECT,
95                  created.getResult(),
96                  null,
97                  created.getPropByRes(),
98                  anyObjectCR.getVirAttrs(),
99                  excludedResources);
100         PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync, creator);
101 
102         return Pair.of(created.getResult(), propagationReporter.getStatuses());
103     }
104 
105     @Transactional(propagation = Propagation.REQUIRES_NEW)
106     @Override
107     public Pair<AnyObjectUR, List<PropagationStatus>> update(
108             final AnyObjectUR anyObjectUR,
109             final Set<String> excludedResources,
110             final boolean nullPriorityAsync,
111             final String updater,
112             final String context) {
113 
114         Map<Pair<String, String>, Set<Attribute>> beforeAttrs = propagationManager.prepareAttrs(
115                 AnyTypeKind.ANY_OBJECT,
116                 anyObjectUR.getKey(),
117                 null,
118                 false,
119                 null,
120                 excludedResources);
121 
122         WorkflowResult<AnyObjectUR> updated = awfAdapter.update(anyObjectUR, updater, context);
123 
124         List<PropagationTaskInfo> taskInfos = propagationManager.setAttributeDeltas(
125                 propagationManager.getUpdateTasks(
126                         AnyTypeKind.ANY_OBJECT,
127                         updated.getResult().getKey(),
128                         false,
129                         null,
130                         updated.getPropByRes(),
131                         null,
132                         anyObjectUR.getVirAttrs(),
133                         excludedResources),
134                 beforeAttrs,
135                 updated.getResult());
136         PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync, updater);
137 
138         return Pair.of(updated.getResult(), propagationReporter.getStatuses());
139     }
140 
141     @Override
142     public List<PropagationStatus> delete(
143             final String key, final boolean nullPriorityAsync, final String eraser, final String context) {
144 
145         return delete(key, Set.of(), nullPriorityAsync, eraser, context);
146     }
147 
148     @Transactional(propagation = Propagation.REQUIRES_NEW)
149     @Override
150     public List<PropagationStatus> delete(
151             final String key,
152             final Set<String> excludedResources,
153             final boolean nullPriorityAsync,
154             final String eraser,
155             final String context) {
156 
157         PropagationByResource<String> propByRes = new PropagationByResource<>();
158         propByRes.set(ResourceOperation.DELETE, anyObjectDAO.findAllResourceKeys(key));
159 
160         // Note here that we can only notify about "delete", not any other
161         // task defined in workflow process definition: this because this
162         // information could only be available after awfAdapter.delete(), which
163         // will also effectively remove user from db, thus making virtually
164         // impossible by NotificationManager to fetch required any object information
165         List<PropagationTaskInfo> taskInfos = propagationManager.getDeleteTasks(
166                 AnyTypeKind.ANY_OBJECT,
167                 key,
168                 propByRes,
169                 null,
170                 excludedResources);
171         PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync, eraser);
172 
173         awfAdapter.delete(key, eraser, context);
174 
175         return propagationReporter.getStatuses();
176     }
177 
178     @Override
179     public String unlink(final AnyObjectUR anyObjectUR, final String updater, final String context) {
180         return awfAdapter.update(anyObjectUR, updater, context).getResult().getKey();
181     }
182 
183     @Override
184     public String link(final AnyObjectUR anyObjectUR, final String updater, final String context) {
185         return awfAdapter.update(anyObjectUR, updater, context).getResult().getKey();
186     }
187 
188     @Override
189     public List<PropagationStatus> provision(
190             final String key,
191             final Collection<String> resources,
192             final boolean nullPriorityAsync,
193             final String executor) {
194 
195         PropagationByResource<String> propByRes = new PropagationByResource<>();
196         propByRes.addAll(ResourceOperation.UPDATE, resources);
197 
198         List<PropagationTaskInfo> taskInfos = propagationManager.getUpdateTasks(
199                 AnyTypeKind.ANY_OBJECT,
200                 key,
201                 false,
202                 null,
203                 propByRes,
204                 null,
205                 null,
206                 null);
207         PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync, executor);
208 
209         return propagationReporter.getStatuses();
210     }
211 
212     @Override
213     public List<PropagationStatus> deprovision(
214             final String key,
215             final Collection<String> resources,
216             final boolean nullPriorityAsync,
217             final String executor) {
218 
219         PropagationByResource<String> propByRes = new PropagationByResource<>();
220         propByRes.addAll(ResourceOperation.DELETE, resources);
221 
222         List<PropagationTaskInfo> taskInfos = propagationManager.getDeleteTasks(
223                 AnyTypeKind.ANY_OBJECT,
224                 key,
225                 propByRes,
226                 null,
227                 anyObjectDAO.findAllResourceKeys(key).stream().
228                         filter(resource -> !resources.contains(resource)).
229                         collect(Collectors.toList()));
230         PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync, executor);
231 
232         return propagationReporter.getStatuses();
233     }
234 }