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