1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.core.provisioning.java.propagation;
20
21 import java.util.Optional;
22 import java.util.Set;
23 import java.util.stream.Collectors;
24 import org.apache.syncope.common.lib.types.AnyTypeKind;
25 import org.apache.syncope.core.persistence.api.dao.RealmDAO;
26 import org.apache.syncope.core.persistence.api.dao.UserDAO;
27 import org.apache.syncope.core.provisioning.api.propagation.PropagationActions;
28 import org.apache.syncope.core.provisioning.api.propagation.PropagationManager;
29 import org.apache.syncope.core.provisioning.api.propagation.PropagationTaskInfo;
30 import org.apache.syncope.core.spring.implementation.InstanceScope;
31 import org.apache.syncope.core.spring.implementation.SyncopeImplementation;
32 import org.apache.syncope.core.spring.security.PasswordGenerator;
33 import org.identityconnectors.framework.common.objects.Attribute;
34 import org.identityconnectors.framework.common.objects.AttributeBuilder;
35 import org.identityconnectors.framework.common.objects.AttributeUtil;
36 import org.identityconnectors.framework.common.objects.OperationalAttributes;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.transaction.annotation.Transactional;
41
42 @SyncopeImplementation(scope = InstanceScope.PER_CONTEXT)
43 public class GenerateRandomPasswordPropagationActions implements PropagationActions {
44
45 protected static final Logger LOG = LoggerFactory.getLogger(GenerateRandomPasswordPropagationActions.class);
46
47 @Autowired
48 protected UserDAO userDAO;
49
50 @Autowired
51 protected RealmDAO realmDAO;
52
53 @Autowired
54 protected PasswordGenerator passwordGenerator;
55
56 protected boolean generateRandomPassword(final PropagationTaskInfo taskInfo) {
57 return AnyTypeKind.USER == taskInfo.getAnyTypeKind()
58 && taskInfo.getBeforeObj().isEmpty()
59 && AttributeUtil.getPasswordValue(taskInfo.getPropagationData().getAttributes()) == null;
60 }
61
62 @Transactional(readOnly = true)
63 @Override
64 public void before(final PropagationTaskInfo taskInfo) {
65 if (generateRandomPassword(taskInfo)) {
66 Set<Attribute> attrs = taskInfo.getPropagationData().getAttributes();
67
68
69 attrs.add(AttributeBuilder.buildPassword(passwordGenerator.generate(
70 taskInfo.getResource(),
71 realmDAO.findAncestors(userDAO.find(taskInfo.getEntityKey()).getRealm())).toCharArray()));
72
73
74 Optional.ofNullable(AttributeUtil.find(PropagationManager.MANDATORY_MISSING_ATTR_NAME, attrs)).
75 ifPresent(mandatoryMissing -> {
76 attrs.remove(mandatoryMissing);
77
78 Set<Object> newMandatoryMissing = mandatoryMissing.getValue().stream().
79 filter(v -> !OperationalAttributes.PASSWORD_NAME.equals(v)).
80 collect(Collectors.toSet());
81 if (!newMandatoryMissing.isEmpty()) {
82 attrs.add(AttributeBuilder.build(
83 PropagationManager.MANDATORY_MISSING_ATTR_NAME, newMandatoryMissing));
84 }
85 });
86 }
87 }
88 }