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.fit.core.reference;
20  
21  import java.util.Optional;
22  import org.apache.syncope.common.lib.to.Provision;
23  import org.apache.syncope.common.lib.types.MatchType;
24  import org.apache.syncope.core.persistence.api.dao.UserDAO;
25  import org.apache.syncope.core.persistence.api.dao.search.AttrCond;
26  import org.apache.syncope.core.persistence.api.dao.search.SearchCond;
27  import org.apache.syncope.core.persistence.api.entity.Any;
28  import org.apache.syncope.core.provisioning.api.rules.PullCorrelationRule;
29  import org.apache.syncope.core.provisioning.api.rules.PullCorrelationRuleConfClass;
30  import org.apache.syncope.core.provisioning.api.rules.PullMatch;
31  import org.identityconnectors.framework.common.objects.Attribute;
32  import org.identityconnectors.framework.common.objects.SyncDelta;
33  import org.springframework.beans.factory.annotation.Autowired;
34  import org.springframework.transaction.annotation.Transactional;
35  import org.springframework.util.CollectionUtils;
36  
37  @PullCorrelationRuleConfClass(LinkedAccountSamplePullCorrelationRuleConf.class)
38  public class LinkedAccountSamplePullCorrelationRule implements PullCorrelationRule {
39  
40      public static final String VIVALDI_KEY = "b3cbc78d-32e6-4bd4-92e0-bbe07566a2ee";
41  
42      @Autowired
43      private UserDAO userDAO;
44  
45      @Override
46      public SearchCond getSearchCond(final SyncDelta syncDelta, final Provision provision) {
47          AttrCond cond = new AttrCond();
48  
49          Attribute email = syncDelta.getObject().getAttributeByName("email");
50          if (email != null && !CollectionUtils.isEmpty(email.getValue())) {
51              cond.setSchema("email");
52              cond.setType(AttrCond.Type.EQ);
53              cond.setExpression(email.getValue().get(0).toString());
54          } else {
55              cond.setSchema("");
56          }
57  
58          return SearchCond.getLeaf(cond);
59      }
60  
61      @Transactional(readOnly = true)
62      @Override
63      public PullMatch matching(final Any<?> any, final SyncDelta syncDelta, final Provision provision) {
64          // if match with internal user vivaldi was found but firstName is different, update linked account
65          // instead of updating user
66          Attribute firstName = syncDelta.getObject().getAttributeByName("firstName");
67          if (VIVALDI_KEY.equals(any.getKey())
68                  && firstName != null && !CollectionUtils.isEmpty(firstName.getValue())
69                  && !"Antonio".equals(firstName.getValue().get(0).toString())) {
70  
71              return new PullMatch(MatchType.LINKED_ACCOUNT, any);
72          }
73  
74          return PullCorrelationRule.super.matching(any, syncDelta, provision);
75      }
76  
77      @Transactional(readOnly = true)
78      @Override
79      public Optional<PullMatch> unmatching(final SyncDelta syncDelta, final Provision provision) {
80          // if no match with internal user was found, link account to vivaldi instead of creating new user
81          return Optional.of(new PullMatch(MatchType.LINKED_ACCOUNT, userDAO.find(VIVALDI_KEY)));
82      }
83  }