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.wa.bootstrap.mapping;
20  
21  import java.util.HashSet;
22  import org.apache.syncope.common.lib.policy.AttrReleasePolicyConf;
23  import org.apache.syncope.common.lib.policy.AttrReleasePolicyTO;
24  import org.apache.syncope.common.lib.policy.DefaultAttrReleasePolicyConf;
25  import org.apereo.cas.authentication.principal.DefaultPrincipalAttributesRepository;
26  import org.apereo.cas.authentication.principal.cache.AbstractPrincipalAttributesRepository;
27  import org.apereo.cas.authentication.principal.cache.CachingPrincipalAttributesRepository;
28  import org.apereo.cas.configuration.model.core.authentication.PrincipalAttributesCoreProperties.MergingStrategyTypes;
29  import org.apereo.cas.services.AbstractRegisteredServiceAttributeReleasePolicy;
30  import org.apereo.cas.services.ChainingAttributeReleasePolicy;
31  import org.apereo.cas.services.DenyAllAttributeReleasePolicy;
32  import org.apereo.cas.services.RegisteredServiceAttributeReleasePolicy;
33  import org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy;
34  import org.apereo.cas.services.ReturnMappedAttributeReleasePolicy;
35  import org.apereo.cas.services.consent.DefaultRegisteredServiceConsentPolicy;
36  import org.apereo.cas.util.model.TriStateBoolean;
37  
38  public class DefaultAttrReleaseMapper implements AttrReleaseMapper {
39  
40      @Override
41      public boolean supports(final AttrReleasePolicyConf conf) {
42          return DefaultAttrReleasePolicyConf.class.equals(conf.getClass());
43      }
44  
45      @Override
46      public RegisteredServiceAttributeReleasePolicy build(final AttrReleasePolicyTO policy) {
47          DefaultAttrReleasePolicyConf conf = (DefaultAttrReleasePolicyConf) policy.getConf();
48  
49          ReturnMappedAttributeReleasePolicy returnMapped = null;
50          if (!conf.getReleaseAttrs().isEmpty()) {
51              returnMapped = new ReturnMappedAttributeReleasePolicy();
52              returnMapped.setAllowedAttributes(conf.getReleaseAttrs());
53          }
54  
55          ReturnAllowedAttributeReleasePolicy returnAllowed = null;
56          if (!conf.getAllowedAttrs().isEmpty()) {
57              returnAllowed = new ReturnAllowedAttributeReleasePolicy();
58              returnAllowed.setAllowedAttributes(conf.getAllowedAttrs());
59          }
60  
61          AbstractRegisteredServiceAttributeReleasePolicy attributeReleasePolicy;
62          if (returnMapped == null && returnAllowed == null) {
63              attributeReleasePolicy = new DenyAllAttributeReleasePolicy();
64          } else if (returnMapped != null) {
65              attributeReleasePolicy = returnMapped;
66          } else {
67              attributeReleasePolicy = returnAllowed;
68          }
69  
70          DefaultRegisteredServiceConsentPolicy consentPolicy = new DefaultRegisteredServiceConsentPolicy(
71                  new HashSet<>(conf.getExcludedAttrs()), new HashSet<>(conf.getIncludeOnlyAttrs()));
72          consentPolicy.setOrder(policy.getOrder());
73          consentPolicy.setStatus(TriStateBoolean.fromBoolean(policy.getStatus()));
74          attributeReleasePolicy.setConsentPolicy(consentPolicy);
75  
76          if (conf.getPrincipalIdAttr() != null) {
77              attributeReleasePolicy.setPrincipalIdAttribute(conf.getPrincipalIdAttr());
78          }
79  
80          if (conf.getPrincipalAttrRepoConf() != null && !conf.getPrincipalAttrRepoConf().getAttrRepos().isEmpty()) {
81              DefaultAttrReleasePolicyConf.PrincipalAttrRepoConf parc = conf.getPrincipalAttrRepoConf();
82  
83              AbstractPrincipalAttributesRepository par = parc.getExpiration() > 0
84                      ? new CachingPrincipalAttributesRepository(parc.getTimeUnit().name(), parc.getExpiration())
85                      : new DefaultPrincipalAttributesRepository();
86  
87              par.setMergingStrategy(MergingStrategyTypes.valueOf(parc.getMergingStrategy().name()));
88              par.setIgnoreResolvedAttributes(par.isIgnoreResolvedAttributes());
89              par.setAttributeRepositoryIds(new HashSet<>(parc.getAttrRepos()));
90              attributeReleasePolicy.setPrincipalAttributesRepository(par);
91          }
92  
93          if (returnMapped != null && returnAllowed != null) {
94              ChainingAttributeReleasePolicy chain = new ChainingAttributeReleasePolicy();
95              chain.addPolicies(returnMapped, returnAllowed);
96  
97              if (conf.getPrincipalAttrRepoConf() != null && !conf.getPrincipalAttrRepoConf().getAttrRepos().isEmpty()) {
98                  DefaultAttrReleasePolicyConf.PrincipalAttrRepoConf parc = conf.getPrincipalAttrRepoConf();
99                  chain.setMergingPolicy(MergingStrategyTypes.valueOf(parc.getMergingStrategy().name()));
100             }
101 
102             return chain;
103         }
104 
105         return attributeReleasePolicy;
106     }
107 }