857050dc145ff2afe4f9229f15a50d230b28a48a
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AndMatchFunctor.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  * AndMatchFunctor.cpp
19  * 
20  * A MatchFunctor that logical ANDs the results of contained functors.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "attribute/filtering/FilterPolicyContext.h"
26 #include "util/SPConstants.h"
27
28 #include <xercesc/util/XMLUniDefs.hpp>
29 #include <xmltooling/util/XMLHelper.h>
30
31 using namespace shibsp;
32 using namespace xmltooling;
33 using namespace std;
34
35 namespace shibsp {
36
37     /**
38      * A MatchFunctor that logical ANDs the results of contained functors.
39      */
40     class SHIBSP_DLLLOCAL AndMatchFunctor : public MatchFunctor
41     {
42     public:
43         AndMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p);
44
45         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
46             if (m_functors.empty())
47                 return false;
48             for (vector<const MatchFunctor*>::const_iterator mf = m_functors.begin(); mf!=m_functors.end(); ++mf)
49                 if (!(*mf)->evaluatePolicyRequirement(filterContext))
50                     return false;
51             return true;
52         }
53
54         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
55             if (m_functors.empty())
56                 return false;
57             for (vector<const MatchFunctor*>::const_iterator mf = m_functors.begin(); mf!=m_functors.end(); ++mf)
58                 if (!(*mf)->evaluatePermitValue(filterContext, attribute, index))
59                     return false;
60             return true;
61         }
62
63     private:
64         MatchFunctor* buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap);
65
66         vector<const MatchFunctor*> m_functors;
67     };
68
69     MatchFunctor* SHIBSP_DLLLOCAL AndMatchFunctorFactory(const pair<const FilterPolicyContext*,const DOMElement*>& p)
70     {
71         return new AndMatchFunctor(p);
72     }
73
74     static XMLCh _id[] =            UNICODE_LITERAL_2(i,d);
75     static XMLCh _ref[] =           UNICODE_LITERAL_3(r,e,f);
76     static XMLCh Rule[] =           UNICODE_LITERAL_4(R,u,l,e);
77     static XMLCh RuleReference[] =  UNICODE_LITERAL_13(R,u,l,e,R,e,f,e,r,e,n,c,e);
78 };
79
80 AndMatchFunctor::AndMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p)
81 {
82     MatchFunctor* func;
83     const DOMElement* e = XMLHelper::getFirstChildElement(p.second);
84     while (e) {
85         func = NULL;
86         if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, Rule)) {
87             func = buildFunctor(e, p.first);
88         }
89         else if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, RuleReference)) {
90             auto_ptr_char ref(e->getAttributeNS(NULL, _ref));
91             if (ref.get() && *ref.get()) {
92                 multimap<string,MatchFunctor*>::const_iterator rule = p.first->getMatchFunctors().find(ref.get());
93                 func = (rule!=p.first->getMatchFunctors().end()) ? rule->second : NULL;
94             }
95         }
96
97         if (func)
98             m_functors.push_back(func);
99
100         e = XMLHelper::getNextSiblingElement(e);
101     }
102 }
103
104 MatchFunctor* AndMatchFunctor::buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap)
105 {
106     // We'll track and map IDs just for consistency, but don't require them or worry about dups.
107     auto_ptr_char temp(e->getAttributeNS(NULL,_id));
108     const char* id = (temp.get() && *temp.get()) ? temp.get() : "";
109     if (*id && functorMap->getMatchFunctors().count(id))
110         id = "";
111
112     auto_ptr<QName> type(XMLHelper::getXSIType(e));
113     if (!type.get())
114         throw ConfigurationException("Child Rule found with no xsi:type.");
115
116     MatchFunctor* func = SPConfig::getConfig().MatchFunctorManager.newPlugin(*type.get(), make_pair(functorMap,e));
117     functorMap->getMatchFunctors().insert(multimap<string,MatchFunctor*>::value_type(id, func));
118     return func;
119 }