0f79a84490f003f503858faca58214b219861db5
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / NotMatchFunctor.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  * NotMatchFunctor.cpp
19  * 
20  * A MatchFunctor that negates the result of a contained functor.
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 negates the result of a contained functor.
39      */
40     class SHIBSP_DLLLOCAL NotMatchFunctor : public MatchFunctor
41     {
42     public:
43         NotMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p);
44
45         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
46             if (m_functor)
47                 return !(m_functor->evaluatePolicyRequirement(filterContext));
48             return false;
49         }
50
51         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
52             if (m_functor)
53                 return !(m_functor->evaluatePermitValue(filterContext, attribute, index));
54             return false;
55         }
56
57     private:
58         MatchFunctor* buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap);
59
60         const MatchFunctor* m_functor;
61     };
62
63     MatchFunctor* SHIBSP_DLLLOCAL NotMatchFunctorFactory(const pair<const FilterPolicyContext*,const DOMElement*>& p)
64     {
65         return new NotMatchFunctor(p);
66     }
67
68     static XMLCh _id[] =            UNICODE_LITERAL_2(i,d);
69     static XMLCh _ref[] =           UNICODE_LITERAL_3(r,e,f);
70     static XMLCh Rule[] =           UNICODE_LITERAL_4(R,u,l,e);
71     static XMLCh RuleReference[] =  UNICODE_LITERAL_13(R,u,l,e,R,e,f,e,r,e,n,c,e);
72 };
73
74 NotMatchFunctor::NotMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p) : m_functor(NULL)
75 {
76     const DOMElement* e = XMLHelper::getFirstChildElement(p.second);
77     if (e) {
78         if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, Rule)) {
79             m_functor = buildFunctor(e, p.first);
80         }
81         else if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, RuleReference)) {
82             auto_ptr_char ref(e->getAttributeNS(NULL, _ref));
83             if (ref.get() && *ref.get()) {
84                 multimap<string,MatchFunctor*>::const_iterator rule = p.first->getMatchFunctors().find(ref.get());
85                 m_functor = (rule!=p.first->getMatchFunctors().end()) ? rule->second : NULL;
86             }
87         }
88     }
89
90     if (!m_functor)
91         throw ConfigurationException("No child Rule installed into NotMatchFunctor.");
92 }
93
94 MatchFunctor* NotMatchFunctor::buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap)
95 {
96     // We'll track and map IDs just for consistency, but don't require them or worry about dups.
97     auto_ptr_char temp(e->getAttributeNS(NULL,_id));
98     const char* id = (temp.get() && *temp.get()) ? temp.get() : "";
99     if (*id && functorMap->getMatchFunctors().count(id))
100         id = "";
101
102     auto_ptr<QName> type(XMLHelper::getXSIType(e));
103     if (!type.get())
104         throw ConfigurationException("Child Rule found with no xsi:type.");
105
106     MatchFunctor* func = SPConfig::getConfig().MatchFunctorManager.newPlugin(*type.get(), make_pair(functorMap,e));
107     functorMap->getMatchFunctors().insert(multimap<string,MatchFunctor*>::value_type(id, func));
108     return func;
109 }