View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.match.saml;
19  
20  import org.opensaml.saml2.metadata.EntitiesDescriptor;
21  import org.opensaml.saml2.metadata.EntityDescriptor;
22  import org.opensaml.xml.util.DatatypeHelper;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import edu.internet2.middleware.shibboleth.common.attribute.filtering.provider.match.basic.AbstractMatchFunctor;
27  
28  /**
29   * Base class for match functors that check if a given entity is in an entity group.
30   */
31  public abstract class AbstractEntityGroupMatchFunctor extends AbstractMatchFunctor {
32  
33      /** Class logger. */
34      private final Logger log = LoggerFactory.getLogger(AbstractEntityGroupMatchFunctor.class);
35  
36      /** The entity group to match against. */
37      private String entityGroup;
38  
39      /**
40       * Gets the entity group to match against.
41       * 
42       * @return entity group to match against
43       */
44      public String getEntityGroup() {
45          return entityGroup;
46      }
47  
48      /**
49       * Sets the entity group to match against.
50       * 
51       * @param group entity group to match against
52       */
53      public void setEntityGroup(String group) {
54          entityGroup = DatatypeHelper.safeTrimOrNullString(group);
55      }
56  
57      /**
58       * Checks if the given entity is in the provided entity group.
59       * 
60       * @param entity the entity to check
61       * 
62       * @return true if the entity is in the group, false if not
63       */
64      protected boolean isEntityInGroup(EntityDescriptor entity) {
65          if (entityGroup == null) {
66              log.debug("No entity group specified, unable to check if entity is in group");
67              return false;
68          }
69  
70          if (entity == null) {
71              log.debug("No entity metadata available, unable to check if entity is in group {}", entityGroup);
72              return false;
73          }
74  
75          EntitiesDescriptor currentGroup = (EntitiesDescriptor) entity.getParent();
76          if (currentGroup == null) {
77              log.debug("Entity descriptor does not have a parent object, unable to check if entity is in group {}",
78                      entityGroup);
79              return false;
80          }
81  
82          do {
83              if (entityGroup.equals(currentGroup.getName())) {
84                  return true;
85              }
86              currentGroup = (EntitiesDescriptor) currentGroup.getParent();
87          } while (currentGroup != null);
88  
89          return false;
90      }
91  }