/
DefaultUserProvisioningManager.java
382 lines (313 loc) · 16.4 KB
/
DefaultUserProvisioningManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.syncope.core.provisioning.java;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.syncope.common.lib.patch.PasswordPatch;
import org.apache.syncope.common.lib.patch.StatusPatch;
import org.apache.syncope.common.lib.patch.StringPatchItem;
import org.apache.syncope.common.lib.patch.UserPatch;
import org.apache.syncope.common.lib.to.PropagationStatus;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.PatchOperation;
import org.apache.syncope.core.persistence.api.dao.UserDAO;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.provisioning.api.UserProvisioningManager;
import org.apache.syncope.core.provisioning.api.UserWorkflowResult;
import org.apache.syncope.core.provisioning.api.PropagationByResource;
import org.apache.syncope.common.lib.types.ResourceOperation;
import org.apache.syncope.common.lib.types.StatusPatchType;
import org.apache.syncope.core.provisioning.api.propagation.PropagationException;
import org.apache.syncope.core.provisioning.api.propagation.PropagationManager;
import org.apache.syncope.core.provisioning.api.propagation.PropagationReporter;
import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor;
import org.apache.syncope.core.provisioning.api.VirAttrHandler;
import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
import org.apache.syncope.common.lib.to.ProvisioningReport;
import org.apache.syncope.core.workflow.api.UserWorkflowAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
public class DefaultUserProvisioningManager implements UserProvisioningManager {
protected static final Logger LOG = LoggerFactory.getLogger(UserProvisioningManager.class);
@Autowired
protected UserWorkflowAdapter uwfAdapter;
@Autowired
protected PropagationManager propagationManager;
@Autowired
protected PropagationTaskExecutor taskExecutor;
@Autowired
protected VirAttrHandler virtAttrHandler;
@Autowired
protected UserDAO userDAO;
@Override
public Pair<String, List<PropagationStatus>> create(final UserTO userTO, final boolean nullPriorityAsync) {
return create(userTO, true, false, null, Collections.<String>emptySet(), nullPriorityAsync);
}
@Override
public Pair<String, List<PropagationStatus>> create(
final UserTO userTO, final boolean storePassword, final boolean nullPriorityAsync) {
return create(userTO, storePassword, false, null, Collections.<String>emptySet(), nullPriorityAsync);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Pair<String, List<PropagationStatus>> create(
final UserTO userTO,
final boolean storePassword,
final boolean disablePwdPolicyCheck,
final Boolean enabled,
final Set<String> excludedResources,
final boolean nullPriorityAsync) {
UserWorkflowResult<Pair<String, Boolean>> created =
uwfAdapter.create(userTO, disablePwdPolicyCheck, enabled, storePassword);
List<PropagationTaskInfo> taskInfos = propagationManager.getUserCreateTasks(
created.getResult().getLeft(),
userTO.getPassword(),
created.getResult().getRight(),
created.getPropByRes(),
created.getPropByLinkedAccount(),
userTO.getVirAttrs(),
excludedResources);
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
return Pair.of(created.getResult().getLeft(), propagationReporter.getStatuses());
}
@Override
public Pair<UserPatch, List<PropagationStatus>> update(final UserPatch userPatch, final boolean nullPriorityAsync) {
UserWorkflowResult<Pair<UserPatch, Boolean>> updated = uwfAdapter.update(userPatch);
List<PropagationTaskInfo> taskInfos = propagationManager.getUserUpdateTasks(updated);
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
return Pair.of(updated.getResult().getLeft(), propagationReporter.getStatuses());
}
@Override
public Pair<UserPatch, List<PropagationStatus>> update(
final UserPatch userPatch, final Set<String> excludedResources, final boolean nullPriorityAsync) {
return update(userPatch, new ProvisioningReport(), null, excludedResources, nullPriorityAsync);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public Pair<UserPatch, List<PropagationStatus>> update(
final UserPatch userPatch,
final ProvisioningReport result,
final Boolean enabled,
final Set<String> excludedResources,
final boolean nullPriorityAsync) {
UserWorkflowResult<Pair<UserPatch, Boolean>> updated;
try {
updated = uwfAdapter.update(userPatch);
} catch (Exception e) {
LOG.error("Update of user {} failed, trying to pull its status anyway (if configured)",
userPatch.getKey(), e);
result.setStatus(ProvisioningReport.Status.FAILURE);
result.setMessage("Update failed, trying to pull status anyway (if configured)\n" + e.getMessage());
updated = new UserWorkflowResult<>(
Pair.of(userPatch, false),
new PropagationByResource<>(),
new PropagationByResource<>(),
new HashSet<>());
}
if (enabled != null) {
User user = userDAO.find(userPatch.getKey());
UserWorkflowResult<String> enableUpdate = null;
if (user.isSuspended() == null) {
enableUpdate = uwfAdapter.activate(userPatch.getKey(), null);
} else if (enabled && user.isSuspended()) {
enableUpdate = uwfAdapter.reactivate(userPatch.getKey());
} else if (!enabled && !user.isSuspended()) {
enableUpdate = uwfAdapter.suspend(userPatch.getKey());
}
if (enableUpdate != null) {
if (enableUpdate.getPropByRes() != null) {
updated.getPropByRes().merge(enableUpdate.getPropByRes());
updated.getPropByRes().purge();
}
updated.getPerformedTasks().addAll(enableUpdate.getPerformedTasks());
}
}
List<PropagationTaskInfo> taskInfos = propagationManager.getUserUpdateTasks(
updated, updated.getResult().getLeft().getPassword() != null, excludedResources);
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
return Pair.of(updated.getResult().getLeft(), propagationReporter.getStatuses());
}
@Override
public List<PropagationStatus> delete(final String key, final boolean nullPriorityAsync) {
return delete(key, Collections.<String>emptySet(), nullPriorityAsync);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public List<PropagationStatus> delete(
final String key, final Set<String> excludedResources, final boolean nullPriorityAsync) {
PropagationByResource<String> propByRes = new PropagationByResource<>();
propByRes.set(ResourceOperation.DELETE, userDAO.findAllResourceKeys(key));
PropagationByResource<Pair<String, String>> propByLinkedAccount = new PropagationByResource<>();
userDAO.findLinkedAccounts(key).forEach(account -> propByLinkedAccount.add(
ResourceOperation.DELETE,
Pair.of(account.getResource().getKey(), account.getConnObjectKeyValue())));
// Note here that we can only notify about "delete", not any other
// task defined in workflow process definition: this because this
// information could only be available after uwfAdapter.delete(), which
// will also effectively remove user from db, thus making virtually
// impossible by NotificationManager to fetch required user information
List<PropagationTaskInfo> taskInfos = propagationManager.getUserDeleteTasks(
key,
propByRes,
propByLinkedAccount,
excludedResources);
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
try {
uwfAdapter.delete(key);
} catch (PropagationException e) {
throw e;
}
return propagationReporter.getStatuses();
}
@Override
public String unlink(final UserPatch userPatch) {
UserWorkflowResult<Pair<UserPatch, Boolean>> updated = uwfAdapter.update(userPatch);
return updated.getResult().getLeft().getKey();
}
@Override
public String link(final UserPatch userPatch) {
return uwfAdapter.update(userPatch).getResult().getLeft().getKey();
}
@Override
public Pair<String, List<PropagationStatus>> activate(
final StatusPatch statusPatch, final boolean nullPriorityAsync) {
UserWorkflowResult<String> updated = statusPatch.isOnSyncope()
? uwfAdapter.activate(statusPatch.getKey(), statusPatch.getToken())
: new UserWorkflowResult<>(
statusPatch.getKey(), null, null, statusPatch.getType().name().toLowerCase());
return Pair.of(updated.getResult(), propagateStatus(statusPatch, nullPriorityAsync));
}
@Override
public Pair<String, List<PropagationStatus>> reactivate(
final StatusPatch statusPatch, final boolean nullPriorityAsync) {
UserWorkflowResult<String> updated = statusPatch.isOnSyncope()
? uwfAdapter.reactivate(statusPatch.getKey())
: new UserWorkflowResult<>(
statusPatch.getKey(), null, null, statusPatch.getType().name().toLowerCase());
return Pair.of(updated.getResult(), propagateStatus(statusPatch, nullPriorityAsync));
}
@Override
public Pair<String, List<PropagationStatus>> suspend(
final StatusPatch statusPatch, final boolean nullPriorityAsync) {
UserWorkflowResult<String> updated = statusPatch.isOnSyncope()
? uwfAdapter.suspend(statusPatch.getKey())
: new UserWorkflowResult<>(
statusPatch.getKey(), null, null, statusPatch.getType().name().toLowerCase());
return Pair.of(updated.getResult(), propagateStatus(statusPatch, nullPriorityAsync));
}
protected List<PropagationStatus> propagateStatus(
final StatusPatch statusPatch, final boolean nullPriorityAsync) {
PropagationByResource<String> propByRes = new PropagationByResource<>();
propByRes.addAll(ResourceOperation.UPDATE, statusPatch.getResources());
List<PropagationTaskInfo> taskInfos = propagationManager.getUpdateTasks(
AnyTypeKind.USER,
statusPatch.getKey(),
false,
statusPatch.getType() != StatusPatchType.SUSPEND,
propByRes,
null,
null,
null);
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
return propagationReporter.getStatuses();
}
@Override
public void internalSuspend(final String key) {
Pair<UserWorkflowResult<String>, Boolean> updated = uwfAdapter.internalSuspend(key);
// propagate suspension if and only if it is required by policy
if (updated != null && updated.getRight()) {
UserPatch userPatch = new UserPatch();
userPatch.setKey(updated.getLeft().getResult());
List<PropagationTaskInfo> taskInfos = propagationManager.getUserUpdateTasks(new UserWorkflowResult<>(
Pair.of(userPatch, Boolean.FALSE),
updated.getLeft().getPropByRes(),
updated.getLeft().getPropByLinkedAccount(),
updated.getLeft().getPerformedTasks()));
taskExecutor.execute(taskInfos, false);
}
}
@Override
public List<PropagationStatus> provision(
final String key,
final boolean changePwd,
final String password,
final Collection<String> resources,
final boolean nullPriorityAsync) {
UserPatch userPatch = new UserPatch();
userPatch.setKey(key);
userPatch.getResources().addAll(resources.stream().map(resource
-> new StringPatchItem.Builder().operation(PatchOperation.ADD_REPLACE).value(resource).build()).
collect(Collectors.toSet()));
if (changePwd) {
PasswordPatch passwordPatch = new PasswordPatch();
passwordPatch.setOnSyncope(false);
passwordPatch.getResources().addAll(resources);
passwordPatch.setValue(password);
userPatch.setPassword(passwordPatch);
}
PropagationByResource<String> propByRes = new PropagationByResource<>();
propByRes.addAll(ResourceOperation.UPDATE, resources);
UserWorkflowResult<Pair<UserPatch, Boolean>> wfResult = new UserWorkflowResult<>(
Pair.of(userPatch, (Boolean) null), propByRes, null, "update");
List<PropagationTaskInfo> taskInfos = propagationManager.getUserUpdateTasks(wfResult, changePwd, null);
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
return propagationReporter.getStatuses();
}
@Override
public List<PropagationStatus> deprovision(
final String key, final Collection<String> resources, final boolean nullPriorityAsync) {
PropagationByResource<String> propByRes = new PropagationByResource<>();
propByRes.set(ResourceOperation.DELETE, resources);
PropagationByResource<Pair<String, String>> propByLinkedAccount = new PropagationByResource<>();
userDAO.findLinkedAccounts(key).stream().
filter(account -> resources.contains(account.getResource().getKey())).
forEach(account -> propByLinkedAccount.add(
ResourceOperation.DELETE,
Pair.of(account.getResource().getKey(), account.getConnObjectKeyValue())));
List<PropagationTaskInfo> taskInfos = propagationManager.getDeleteTasks(
AnyTypeKind.USER,
key,
propByRes,
propByLinkedAccount,
userDAO.findAllResourceKeys(key).stream().
filter(resource -> !resources.contains(resource)).
collect(Collectors.toList()));
PropagationReporter propagationReporter = taskExecutor.execute(taskInfos, nullPriorityAsync);
return propagationReporter.getStatuses();
}
@Override
public void requestPasswordReset(final String key) {
uwfAdapter.requestPasswordReset(key);
}
@Override
public void confirmPasswordReset(final String key, final String token, final String password) {
UserWorkflowResult<Pair<UserPatch, Boolean>> updated = uwfAdapter.confirmPasswordReset(key, token, password);
List<PropagationTaskInfo> taskInfos = propagationManager.getUserUpdateTasks(updated);
taskExecutor.execute(taskInfos, false);
}
}