Metadata based functors.
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeIssuerInEntityGroupFunctor.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * AttributeIssuerInEntityGroupFunctor.cpp
19  * 
20  * A match function that evaluates to true if the attribute issuer is found in metadata and is a member
21  * of the given entity group.
22  */
23
24 #include "internal.h"
25 #include "exceptions.h"
26 #include "attribute/filtering/FilteringContext.h"
27 #include "attribute/filtering/FilterPolicyContext.h"
28
29 using namespace opensaml::saml2md;
30
31 namespace shibsp {
32
33     static const XMLCh groupID[] = UNICODE_LITERAL_7(g,r,o,u,p,I,D);
34
35     /**
36      * A match function that evaluates to true if the attribute issuer is found in metadata and is a member
37      * of the given entity group.
38      */
39     class SHIBSP_DLLLOCAL AttributeIssuerInEntityGroupFunctor : public MatchFunctor
40     {
41         const XMLCh* m_group;
42     public:
43         AttributeIssuerInEntityGroupFunctor(const DOMElement* e) {
44             m_group = e ? e->getAttributeNS(NULL,groupID) : NULL;
45             if (!m_group || !*m_group)
46                 throw ConfigurationException("AttributeIssuerInEntityGroup MatchFunctor requires non-empty groupID attribute.");
47         }
48
49         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
50             const RoleDescriptor* issuer = filterContext.getAttributeIssuerMetadata();
51             if (!issuer)
52                 return false;
53             const EntitiesDescriptor* group = dynamic_cast<const EntitiesDescriptor*>(issuer->getParent()->getParent());
54             while (group) {
55                 if (XMLString::equals(group->getName(), m_group))
56                     return true;
57                 group = dynamic_cast<const EntitiesDescriptor*>(group->getParent());
58             }
59             return false;
60         }
61
62         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
63             return evaluatePolicyRequirement(filterContext);
64         }
65     };
66
67     MatchFunctor* SHIBSP_DLLLOCAL AttributeIssuerInEntityGroupFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
68     {
69         return new AttributeIssuerInEntityGroupFunctor(p.second);
70     }
71
72 };