97010234b54a891ba9d83ec2187fd6d3f417d13d
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeIssuerInEntityGroupFunctor.cpp
1 /*
2  *  Copyright 2001-2009 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 #include "attribute/filtering/MatchFunctor.h"
29
30 #include <saml/saml2/metadata/Metadata.h>
31
32 using namespace opensaml::saml2md;
33
34 namespace shibsp {
35
36     static const XMLCh groupID[] = UNICODE_LITERAL_7(g,r,o,u,p,I,D);
37
38     /**
39      * A match function that evaluates to true if the attribute issuer is found in metadata and is a member
40      * of the given entity group.
41      */
42     class SHIBSP_DLLLOCAL AttributeIssuerInEntityGroupFunctor : public MatchFunctor
43     {
44         const XMLCh* m_group;
45     public:
46         AttributeIssuerInEntityGroupFunctor(const DOMElement* e) {
47             m_group = e ? e->getAttributeNS(NULL,groupID) : NULL;
48             if (!m_group || !*m_group)
49                 throw ConfigurationException("AttributeIssuerInEntityGroup MatchFunctor requires non-empty groupID attribute.");
50         }
51
52         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
53             const RoleDescriptor* issuer = filterContext.getAttributeIssuerMetadata();
54             if (!issuer)
55                 return false;
56             const EntitiesDescriptor* group = dynamic_cast<const EntitiesDescriptor*>(issuer->getParent()->getParent());
57             while (group) {
58                 if (XMLString::equals(group->getName(), m_group))
59                     return true;
60                 group = dynamic_cast<const EntitiesDescriptor*>(group->getParent());
61             }
62             return false;
63         }
64
65         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
66             return evaluatePolicyRequirement(filterContext);
67         }
68     };
69
70     MatchFunctor* SHIBSP_DLLLOCAL AttributeIssuerInEntityGroupFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
71     {
72         return new AttributeIssuerInEntityGroupFunctor(p.second);
73     }
74
75 };