-
Notifications
You must be signed in to change notification settings - Fork 174
/
UserDataBinderImpl.java
809 lines (693 loc) · 36.4 KB
/
UserDataBinderImpl.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
/*
* 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.data;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.syncope.common.lib.SyncopeClientCompositeException;
import org.apache.syncope.common.lib.SyncopeClientException;
import org.apache.syncope.common.lib.patch.AttrPatch;
import org.apache.syncope.common.lib.patch.PasswordPatch;
import org.apache.syncope.common.lib.patch.StringPatchItem;
import org.apache.syncope.common.lib.patch.UserPatch;
import org.apache.syncope.common.lib.to.AttrTO;
import org.apache.syncope.common.lib.to.ConnObjectTO;
import org.apache.syncope.common.lib.to.LinkedAccountTO;
import org.apache.syncope.common.lib.to.MembershipTO;
import org.apache.syncope.common.lib.to.UserTO;
import org.apache.syncope.common.lib.types.AnyTypeKind;
import org.apache.syncope.common.lib.types.CipherAlgorithm;
import org.apache.syncope.common.lib.types.ClientExceptionType;
import org.apache.syncope.common.lib.types.PatchOperation;
import org.apache.syncope.common.lib.types.ResourceOperation;
import org.apache.syncope.core.persistence.api.dao.AccessTokenDAO;
import org.apache.syncope.core.persistence.api.dao.ConfDAO;
import org.apache.syncope.core.persistence.api.dao.SecurityQuestionDAO;
import org.apache.syncope.core.persistence.api.entity.group.Group;
import org.apache.syncope.core.persistence.api.entity.user.SecurityQuestion;
import org.apache.syncope.core.persistence.api.entity.user.User;
import org.apache.syncope.core.provisioning.api.PropagationByResource;
import org.apache.syncope.core.provisioning.api.data.UserDataBinder;
import org.apache.syncope.core.spring.security.AuthContextUtils;
import org.apache.syncope.core.persistence.api.dao.AnyTypeDAO;
import org.apache.syncope.core.persistence.api.dao.ApplicationDAO;
import org.apache.syncope.core.persistence.api.dao.DelegationDAO;
import org.apache.syncope.core.persistence.api.dao.RoleDAO;
import org.apache.syncope.core.persistence.api.entity.AccessToken;
import org.apache.syncope.core.persistence.api.entity.AnyUtils;
import org.apache.syncope.core.persistence.api.entity.Delegation;
import org.apache.syncope.core.persistence.api.entity.Entity;
import org.apache.syncope.core.persistence.api.entity.PlainSchema;
import org.apache.syncope.core.persistence.api.entity.Privilege;
import org.apache.syncope.core.persistence.api.entity.Realm;
import org.apache.syncope.core.persistence.api.entity.RelationshipType;
import org.apache.syncope.core.persistence.api.entity.Role;
import org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject;
import org.apache.syncope.core.persistence.api.entity.resource.ExternalResource;
import org.apache.syncope.core.persistence.api.entity.user.LAPlainAttr;
import org.apache.syncope.core.persistence.api.entity.user.LinkedAccount;
import org.apache.syncope.core.persistence.api.entity.user.UMembership;
import org.apache.syncope.core.persistence.api.entity.user.UPlainAttr;
import org.apache.syncope.core.persistence.api.entity.user.URelationship;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
@Transactional(rollbackFor = { Throwable.class })
public class UserDataBinderImpl extends AbstractAnyDataBinder implements UserDataBinder {
@Autowired
private RoleDAO roleDAO;
@Autowired
private ConfDAO confDAO;
@Autowired
private SecurityQuestionDAO securityQuestionDAO;
@Autowired
private AnyTypeDAO anyTypeDAO;
@Autowired
private ApplicationDAO applicationDAO;
@Autowired
private AccessTokenDAO accessTokenDAO;
@Autowired
private DelegationDAO delegationDAO;
@Resource(name = "adminUser")
private String adminUser;
@Resource(name = "anonymousUser")
private String anonymousUser;
@Transactional(readOnly = true)
@Override
public UserTO getAuthenticatedUserTO() {
UserTO authUserTO;
String authUsername = AuthContextUtils.getUsername();
if (anonymousUser.equals(authUsername)) {
authUserTO = new UserTO();
authUserTO.setKey(null);
authUserTO.setUsername(anonymousUser);
} else if (adminUser.equals(authUsername)) {
authUserTO = new UserTO();
authUserTO.setKey(null);
authUserTO.setUsername(adminUser);
} else {
User authUser = userDAO.findByUsername(authUsername);
authUserTO = getUserTO(authUser, true);
}
return authUserTO;
}
private void setPassword(final User user, final String password, final SyncopeClientCompositeException scce) {
try {
String algorithm = confDAO.find("password.cipher.algorithm", CipherAlgorithm.AES.name());
user.setPassword(password, CipherAlgorithm.valueOf(algorithm));
} catch (IllegalArgumentException e) {
throw aggregateException(scce, e, ClientExceptionType.NotFound);
}
}
private void setSecurityAnswer(
final User user,
final String securityAnswer,
final SyncopeClientCompositeException scce) {
try {
String algorithm = confDAO.find("password.cipher.algorithm", CipherAlgorithm.AES.name());
user.setSecurityAnswer(securityAnswer, CipherAlgorithm.valueOf(algorithm));
} catch (IllegalArgumentException e) {
throw aggregateException(scce, e, ClientExceptionType.NotFound);
}
}
private RuntimeException aggregateException(
final SyncopeClientCompositeException scce,
final RuntimeException e,
final ClientExceptionType clientExceptionType) {
SyncopeClientException sce = SyncopeClientException.build(clientExceptionType);
sce.getElements().add(e.getMessage());
scce.addException(sce);
return scce;
}
private void linkedAccount(
final User user,
final LinkedAccountTO accountTO,
final AnyUtils anyUtils,
final SyncopeClientException invalidValues) {
ExternalResource resource = resourceDAO.find(accountTO.getResource());
if (resource == null) {
LOG.debug("Ignoring invalid resource {}", accountTO.getResource());
} else {
Optional<? extends LinkedAccount> found =
user.getLinkedAccount(resource.getKey(), accountTO.getConnObjectKeyValue());
LinkedAccount account = found.isPresent()
? found.get()
: new Supplier<LinkedAccount>() {
@Override
public LinkedAccount get() {
LinkedAccount acct = entityFactory.newEntity(LinkedAccount.class);
acct.setOwner(user);
user.add(acct);
acct.setConnObjectKeyValue(accountTO.getConnObjectKeyValue());
acct.setResource(resource);
return acct;
}
}.get();
account.setUsername(accountTO.getUsername());
if (StringUtils.isBlank(accountTO.getPassword())) {
account.setEncodedPassword(null, null);
} else if (!accountTO.getPassword().equals(account.getPassword())) {
account.setPassword(accountTO.getPassword(), CipherAlgorithm.AES);
}
account.setSuspended(accountTO.isSuspended());
accountTO.getPlainAttrs().stream().
filter(attrTO -> !attrTO.getValues().isEmpty()).
forEach(attrTO -> {
PlainSchema schema = getPlainSchema(attrTO.getSchema());
if (schema != null) {
LAPlainAttr attr = account.getPlainAttr(schema.getKey()).orElse(null);
if (attr == null) {
attr = entityFactory.newEntity(LAPlainAttr.class);
attr.setSchema(schema);
attr.setOwner(user);
attr.setAccount(account);
}
fillAttr(attrTO.getValues(), anyUtils, schema, attr, invalidValues);
if (attr.getValuesAsStrings().isEmpty()) {
attr.setOwner(null);
} else {
account.add(attr);
}
}
});
accountTO.getPrivileges().forEach(key -> {
Privilege privilege = applicationDAO.findPrivilege(key);
if (privilege == null) {
LOG.debug("Invalid privilege {}, ignoring", key);
} else {
account.add(privilege);
}
});
}
}
private LinkedAccountTO getLinkedAccountTO(final LinkedAccount account, final boolean returnPasswordValue) {
LinkedAccountTO accountTO = new LinkedAccountTO.Builder(
account.getKey(), account.getResource().getKey(), account.getConnObjectKeyValue()).
username(account.getUsername()).
suspended(BooleanUtils.isTrue(account.isSuspended())).
build();
if (returnPasswordValue) {
accountTO.setPassword(account.getPassword());
}
account.getPlainAttrs().forEach(plainAttr -> {
accountTO.getPlainAttrs().add(new AttrTO.Builder().
schema(plainAttr.getSchema().getKey()).
values(plainAttr.getValuesAsStrings()).build());
});
accountTO.getPrivileges().addAll(account.getPrivileges().stream().
map(Entity::getKey).collect(Collectors.toList()));
return accountTO;
}
@Override
public void create(final User user, final UserTO userTO, final boolean storePassword) {
SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
// set username
user.setUsername(userTO.getUsername());
// set password
if (StringUtils.isBlank(userTO.getPassword()) || !storePassword) {
LOG.debug("Password was not provided or not required to be stored");
} else {
setPassword(user, userTO.getPassword(), scce);
user.setChangePwdDate(new Date());
}
user.setMustChangePassword(userTO.isMustChangePassword());
// security question / answer
if (userTO.getSecurityQuestion() != null) {
SecurityQuestion securityQuestion = securityQuestionDAO.find(userTO.getSecurityQuestion());
if (securityQuestion != null) {
user.setSecurityQuestion(securityQuestion);
}
}
setSecurityAnswer(user, userTO.getSecurityAnswer(), scce);
// roles
userTO.getRoles().forEach(roleKey -> {
Role role = roleDAO.find(roleKey);
if (role == null) {
LOG.warn("Ignoring unknown role with id {}", roleKey);
} else {
user.add(role);
}
});
// realm
Realm realm = realmDAO.findByFullPath(userTO.getRealm());
if (realm == null) {
SyncopeClientException noRealm = SyncopeClientException.build(ClientExceptionType.InvalidRealm);
noRealm.getElements().add("Invalid or null realm specified: " + userTO.getRealm());
scce.addException(noRealm);
}
user.setRealm(realm);
// relationships
Set<Pair<String, String>> relationships = new HashSet<>();
userTO.getRelationships().forEach(relationshipTO -> {
AnyObject otherEnd = anyObjectDAO.find(relationshipTO.getOtherEndKey());
if (otherEnd == null) {
LOG.debug("Ignoring invalid anyObject " + relationshipTO.getOtherEndKey());
} else if (relationships.contains(Pair.of(otherEnd.getKey(), relationshipTO.getType()))) {
LOG.error("{} was already in relationship {} with {}", otherEnd, relationshipTO.getType(), user);
SyncopeClientException assigned =
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
assigned.getElements().add(otherEnd.getType().getKey() + " " + otherEnd.getName()
+ " in relationship " + relationshipTO.getType());
scce.addException(assigned);
} else if (user.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
relationships.add(Pair.of(otherEnd.getKey(), relationshipTO.getType()));
RelationshipType relationshipType = relationshipTypeDAO.find(relationshipTO.getType());
if (relationshipType == null) {
LOG.debug("Ignoring invalid relationship type {}", relationshipTO.getType());
} else {
URelationship relationship = entityFactory.newEntity(URelationship.class);
relationship.setType(relationshipType);
relationship.setRightEnd(otherEnd);
relationship.setLeftEnd(user);
user.add(relationship);
}
} else {
LOG.error("{} cannot be related to {}", otherEnd, user);
SyncopeClientException unrelatable =
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
unrelatable.getElements().add(otherEnd.getType().getKey() + " " + otherEnd.getName()
+ " cannot be related");
scce.addException(unrelatable);
}
});
// memberships
Set<String> groups = new HashSet<>();
userTO.getMemberships().forEach(membershipTO -> {
Group group = membershipTO.getGroupKey() == null
? groupDAO.findByName(membershipTO.getGroupName())
: groupDAO.find(membershipTO.getGroupKey());
if (group == null) {
LOG.debug("Ignoring invalid group {}",
membershipTO.getGroupKey() + " / " + membershipTO.getGroupName());
} else if (groups.contains(group.getKey())) {
LOG.error("{} was already assigned to {}", group, user);
SyncopeClientException assigned =
SyncopeClientException.build(ClientExceptionType.InvalidMembership);
assigned.getElements().add("Group " + group.getName() + " was already assigned");
scce.addException(assigned);
} else if (user.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
groups.add(group.getKey());
UMembership membership = entityFactory.newEntity(UMembership.class);
membership.setRightEnd(group);
membership.setLeftEnd(user);
user.add(membership);
// membership attributes
fill(user, membership, membershipTO, anyUtilsFactory.getInstance(AnyTypeKind.USER), scce);
} else {
LOG.error("{} cannot be assigned to {}", group, user);
SyncopeClientException unassignable =
SyncopeClientException.build(ClientExceptionType.InvalidMembership);
unassignable.getElements().add("Group " + group.getName() + " cannot be assigned");
scce.addException(unassignable);
}
});
// linked accounts
SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
userTO.getLinkedAccounts().forEach(accountTO
-> linkedAccount(user, accountTO, anyUtilsFactory.getLinkedAccountInstance(), invalidValues));
if (!invalidValues.isEmpty()) {
scce.addException(invalidValues);
}
// attributes and resources
fill(user, userTO, anyUtilsFactory.getInstance(AnyTypeKind.USER), scce);
// Throw composite exception if there is at least one element set in the composing exceptions
if (scce.hasExceptions()) {
throw scce;
}
}
private boolean isPasswordMapped(final ExternalResource resource) {
return resource.getProvision(anyTypeDAO.findUser()).
filter(provision -> provision.getMapping() != null).
map(provision -> provision.getMapping().getItems().stream().anyMatch(item -> item.isPassword())).
orElse(false);
}
@Override
public Pair<PropagationByResource<String>, PropagationByResource<Pair<String, String>>> update(
final User toBeUpdated, final UserPatch userPatch) {
// Re-merge any pending change from workflow tasks
User user = userDAO.save(toBeUpdated);
PropagationByResource<String> propByRes = new PropagationByResource<>();
PropagationByResource<Pair<String, String>> propByLinkedAccount = new PropagationByResource<>();
SyncopeClientCompositeException scce = SyncopeClientException.buildComposite();
AnyUtils anyUtils = anyUtilsFactory.getInstance(AnyTypeKind.USER);
// password
String password = null;
boolean changePwd = false;
if (userPatch.getPassword() != null) {
if (userPatch.getPassword().getOperation() == PatchOperation.DELETE) {
user.setEncodedPassword(null, null);
changePwd = true;
} else if (StringUtils.isNotBlank(userPatch.getPassword().getValue())) {
if (userPatch.getPassword().isOnSyncope()) {
setPassword(user, userPatch.getPassword().getValue(), scce);
user.setChangePwdDate(new Date());
}
password = userPatch.getPassword().getValue();
changePwd = true;
}
if (changePwd) {
propByRes.addAll(ResourceOperation.UPDATE, userPatch.getPassword().getResources());
}
}
// Save projection on Resources (before update)
Map<String, ConnObjectTO> beforeOnResources =
onResources(user, userDAO.findAllResourceKeys(user.getKey()), password, changePwd);
// realm
setRealm(user, userPatch);
// username
if (userPatch.getUsername() != null && StringUtils.isNotBlank(userPatch.getUsername().getValue())) {
String oldUsername = user.getUsername();
user.setUsername(userPatch.getUsername().getValue());
if (oldUsername.equals(AuthContextUtils.getUsername())) {
AuthContextUtils.updateUsername(userPatch.getUsername().getValue());
}
AccessToken accessToken = accessTokenDAO.findByOwner(oldUsername);
if (accessToken != null) {
accessToken.setOwner(userPatch.getUsername().getValue());
accessTokenDAO.save(accessToken);
}
}
// security question / answer:
if (userPatch.getSecurityQuestion() != null) {
if (userPatch.getSecurityQuestion().getValue() == null) {
user.setSecurityQuestion(null);
user.setSecurityAnswer(null, null);
} else {
SecurityQuestion securityQuestion =
securityQuestionDAO.find(userPatch.getSecurityQuestion().getValue());
if (securityQuestion != null) {
user.setSecurityQuestion(securityQuestion);
setSecurityAnswer(user, userPatch.getSecurityAnswer().getValue(), scce);
}
}
}
// roles
for (StringPatchItem patch : userPatch.getRoles()) {
Role role = roleDAO.find(patch.getValue());
if (role == null) {
LOG.warn("Ignoring unknown role with key {}", patch.getValue());
} else {
switch (patch.getOperation()) {
case ADD_REPLACE:
user.add(role);
break;
case DELETE:
default:
user.getRoles().remove(role);
}
}
}
// attributes and resources
fill(user, userPatch, anyUtils, scce);
// relationships
Set<Pair<String, String>> relationships = new HashSet<>();
userPatch.getRelationships().stream().filter(patch -> patch.getRelationshipTO() != null).forEach(patch -> {
RelationshipType relationshipType = relationshipTypeDAO.find(patch.getRelationshipTO().getType());
if (relationshipType == null) {
LOG.debug("Ignoring invalid relationship type {}", patch.getRelationshipTO().getType());
} else {
user.getRelationship(relationshipType, patch.getRelationshipTO().getOtherEndKey()).
ifPresent(relationship -> {
user.getRelationships().remove(relationship);
relationship.setLeftEnd(null);
});
if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
AnyObject otherEnd = anyObjectDAO.find(patch.getRelationshipTO().getOtherEndKey());
if (otherEnd == null) {
LOG.debug("Ignoring invalid any object {}", patch.getRelationshipTO().getOtherEndKey());
} else if (relationships.contains(
Pair.of(otherEnd.getKey(), patch.getRelationshipTO().getType()))) {
LOG.error("{} was already in relationship {} with {}",
user, patch.getRelationshipTO().getType(), otherEnd);
SyncopeClientException assigned =
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
assigned.getElements().add("User was already in relationship "
+ patch.getRelationshipTO().getType() + " with "
+ otherEnd.getType().getKey() + " " + otherEnd.getName());
scce.addException(assigned);
} else if (user.getRealm().getFullPath().startsWith(otherEnd.getRealm().getFullPath())) {
relationships.add(Pair.of(otherEnd.getKey(), patch.getRelationshipTO().getType()));
URelationship newRelationship = entityFactory.newEntity(URelationship.class);
newRelationship.setType(relationshipType);
newRelationship.setRightEnd(otherEnd);
newRelationship.setLeftEnd(user);
user.add(newRelationship);
} else {
LOG.error("{} cannot be related to {}", otherEnd, user);
SyncopeClientException unrelatable =
SyncopeClientException.build(ClientExceptionType.InvalidRelationship);
unrelatable.getElements().add(otherEnd.getType().getKey() + " " + otherEnd.getName()
+ " cannot be related");
scce.addException(unrelatable);
}
}
}
});
SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
// memberships
Set<String> groups = new HashSet<>();
userPatch.getMemberships().stream().filter(patch -> patch.getGroup() != null).forEach(patch -> {
user.getMembership(patch.getGroup()).ifPresent(membership -> {
user.remove(membership);
membership.setLeftEnd(null);
user.getPlainAttrs(membership).forEach(attr -> {
user.remove(attr);
attr.setOwner(null);
attr.setMembership(null);
plainAttrValueDAO.deleteAll(attr, anyUtils);
plainAttrDAO.delete(attr);
});
if (patch.getOperation() == PatchOperation.DELETE) {
propByRes.addAll(
ResourceOperation.UPDATE,
groupDAO.findAllResourceKeys((membership.getRightEnd().getKey())));
}
});
if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
Group group = groupDAO.find(patch.getGroup());
if (group == null) {
LOG.debug("Ignoring invalid group {}", patch.getGroup());
} else if (groups.contains(group.getKey())) {
LOG.error("Multiple patches for group {} of {} were found", group, user);
SyncopeClientException assigned =
SyncopeClientException.build(ClientExceptionType.InvalidMembership);
assigned.getElements().add("Multiple patches for group " + group.getName() + " were found");
scce.addException(assigned);
} else if (user.getRealm().getFullPath().startsWith(group.getRealm().getFullPath())) {
groups.add(group.getKey());
UMembership newMembership = entityFactory.newEntity(UMembership.class);
newMembership.setRightEnd(group);
newMembership.setLeftEnd(user);
user.add(newMembership);
patch.getPlainAttrs().forEach(attrTO -> {
PlainSchema schema = getPlainSchema(attrTO.getSchema());
if (schema == null) {
LOG.debug("Invalid " + PlainSchema.class.getSimpleName()
+ "{}, ignoring...", attrTO.getSchema());
} else {
UPlainAttr attr = user.getPlainAttr(schema.getKey(), newMembership).orElse(null);
if (attr == null) {
LOG.debug("No plain attribute found for {} and membership of {}",
schema, newMembership.getRightEnd());
attr = anyUtils.newPlainAttr();
attr.setOwner(user);
attr.setMembership(newMembership);
attr.setSchema(schema);
user.add(attr);
processAttrPatch(
user,
new AttrPatch.Builder().attrTO(attrTO).build(),
schema,
attr,
anyUtils,
invalidValues);
}
}
});
if (!invalidValues.isEmpty()) {
scce.addException(invalidValues);
}
propByRes.addAll(ResourceOperation.UPDATE, groupDAO.findAllResourceKeys(group.getKey()));
// SYNCOPE-686: if password is invertible and we are adding resources with password mapping,
// ensure that they are counted for password propagation
if (toBeUpdated.canDecodePassword()) {
if (userPatch.getPassword() == null) {
userPatch.setPassword(new PasswordPatch());
}
group.getResources().stream().
filter(resource -> isPasswordMapped(resource)).
forEach(resource -> userPatch.getPassword().getResources().add(resource.getKey()));
}
} else {
LOG.error("{} cannot be assigned to {}", group, user);
SyncopeClientException unassignable =
SyncopeClientException.build(ClientExceptionType.InvalidMembership);
unassignable.getElements().add("Group " + group.getName() + " cannot be assigned");
scce.addException(unassignable);
}
}
});
// linked accounts
userPatch.getLinkedAccounts().stream().filter(patch -> patch.getLinkedAccountTO() != null).forEach(patch -> {
user.getLinkedAccount(
patch.getLinkedAccountTO().getResource(),
patch.getLinkedAccountTO().getConnObjectKeyValue()).ifPresent(account -> {
if (patch.getOperation() == PatchOperation.DELETE) {
user.getLinkedAccounts().remove(account);
account.setOwner(null);
propByLinkedAccount.add(
ResourceOperation.DELETE,
Pair.of(account.getResource().getKey(), account.getConnObjectKeyValue()));
}
account.getPlainAttrs().stream().collect(Collectors.toSet()).forEach(attr -> {
account.remove(attr);
attr.setOwner(null);
attr.setAccount(null);
plainAttrValueDAO.deleteAll(attr, anyUtilsFactory.getLinkedAccountInstance());
plainAttrDAO.delete(attr);
});
});
if (patch.getOperation() == PatchOperation.ADD_REPLACE) {
linkedAccount(
user,
patch.getLinkedAccountTO(),
anyUtilsFactory.getLinkedAccountInstance(),
invalidValues);
}
});
user.getLinkedAccounts().forEach(account -> propByLinkedAccount.add(
ResourceOperation.CREATE,
Pair.of(account.getResource().getKey(), account.getConnObjectKeyValue())));
// Throw composite exception if there is at least one element set in the composing exceptions
if (scce.hasExceptions()) {
throw scce;
}
// Re-merge any pending change from above
User saved = userDAO.save(user);
// Build final information for next stage (propagation)
Map<String, ConnObjectTO> afterOnResources =
onResources(user, userDAO.findAllResourceKeys(user.getKey()), password, changePwd);
propByRes.merge(propByRes(beforeOnResources, afterOnResources));
if (userPatch.getMustChangePassword() != null) {
user.setMustChangePassword(userPatch.getMustChangePassword().getValue());
propByRes.addAll(
ResourceOperation.UPDATE,
anyUtils.getAllResources(saved).stream().
map(resource -> resource.getProvision(saved.getType())).
filter(Optional::isPresent).map(Optional::get).
filter(mappingManager::hasMustChangePassword).
map(provision -> provision.getResource().getKey()).
collect(Collectors.toSet()));
}
return Pair.of(propByRes, propByLinkedAccount);
}
@Transactional(readOnly = true)
@Override
public LinkedAccountTO getLinkedAccountTO(final LinkedAccount account) {
return getLinkedAccountTO(account, true);
}
@Transactional(readOnly = true)
@Override
public UserTO getUserTO(final User user, final boolean details) {
boolean returnPasswordValue = confDAO.find("return.password.value", false);
UserTO userTO = new UserTO();
userTO.setKey(user.getKey());
userTO.setUsername(user.getUsername());
if (returnPasswordValue) {
userTO.setPassword(user.getPassword());
userTO.setSecurityAnswer(user.getSecurityAnswer());
}
userTO.setType(user.getType().getKey());
userTO.setCreationDate(user.getCreationDate());
userTO.setCreator(user.getCreator());
userTO.setLastChangeDate(user.getLastChangeDate());
userTO.setLastModifier(user.getLastModifier());
userTO.setStatus(user.getStatus());
userTO.setSuspended(BooleanUtils.isTrue(user.isSuspended()));
userTO.setChangePwdDate(user.getChangePwdDate());
userTO.setFailedLogins(user.getFailedLogins());
userTO.setLastLoginDate(user.getLastLoginDate());
userTO.setMustChangePassword(user.isMustChangePassword());
userTO.setToken(user.getToken());
userTO.setTokenExpireTime(user.getTokenExpireTime());
if (user.getSecurityQuestion() != null) {
userTO.setSecurityQuestion(user.getSecurityQuestion().getKey());
}
fillTO(userTO, user.getRealm().getFullPath(),
user.getAuxClasses(),
user.getPlainAttrs(),
derAttrHandler.getValues(user),
details ? virAttrHandler.getValues(user) : Collections.emptyMap(),
userDAO.findAllResources(user),
details);
// dynamic realms
userTO.getDynRealms().addAll(userDAO.findDynRealms(user.getKey()));
if (details) {
// roles
userTO.getRoles().addAll(user.getRoles().stream().map(Entity::getKey).collect(Collectors.toList()));
// dynamic roles
userTO.getDynRoles().addAll(
userDAO.findDynRoles(user.getKey()).stream().map(Entity::getKey).collect(Collectors.toList()));
// privileges
userTO.getPrivileges().addAll(userDAO.findAllRoles(user).stream().
flatMap(role -> role.getPrivileges().stream()).map(Entity::getKey).collect(Collectors.toSet()));
// relationships
userTO.getRelationships().addAll(user.getRelationships().stream().
map(relationship -> getRelationshipTO(relationship.getType().getKey(), relationship.getRightEnd())).
collect(Collectors.toList()));
// memberships
userTO.getMemberships().addAll(
user.getMemberships().stream().map(membership -> getMembershipTO(
user.getPlainAttrs(membership),
derAttrHandler.getValues(user, membership),
virAttrHandler.getValues(user, membership),
membership)).collect(Collectors.toList()));
// dynamic memberships
userTO.getDynMemberships().addAll(
userDAO.findDynGroups(user.getKey()).stream().map(group -> new MembershipTO.Builder().
group(group.getKey(), group.getName()).
build()).collect(Collectors.toList()));
// linked accounts
userTO.getLinkedAccounts().addAll(
user.getLinkedAccounts().stream().map(a -> getLinkedAccountTO(a, returnPasswordValue))
.collect(Collectors.toList()));
// delegations
userTO.getDelegatingDelegations().addAll(
delegationDAO.findByDelegating(user).stream().map(Delegation::getKey).collect(Collectors.toList()));
userTO.getDelegatedDelegations().addAll(
delegationDAO.findByDelegated(user).stream().map(Delegation::getKey).collect(Collectors.toList()));
}
return userTO;
}
@Transactional(readOnly = true)
@Override
public UserTO getUserTO(final String key) {
return getUserTO(userDAO.authFind(key), true);
}
}