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.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              // generate random password
69              attrs.add(AttributeBuilder.buildPassword(passwordGenerator.generate(
70                      taskInfo.getResource(),
71                      realmDAO.findAncestors(userDAO.find(taskInfo.getEntityKey()).getRealm())).toCharArray()));
72  
73              // remove __PASSWORD__ from MANDATORY_MISSING attribute
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  }