Change license header.
[shibboleth/cpp-sp.git] / shibsp / attribute / filtering / impl / NotMatchFunctor.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * NotMatchFunctor.cpp
23  * 
24  * A MatchFunctor that negates the result of a contained functor.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "attribute/filtering/FilterPolicyContext.h"
30 #include "attribute/filtering/MatchFunctor.h"
31 #include "util/SPConstants.h"
32
33 #include <xercesc/util/XMLUniDefs.hpp>
34 #include <xmltooling/util/XMLHelper.h>
35
36 using namespace shibsp;
37 using namespace xmltooling;
38 using namespace std;
39
40 namespace shibsp {
41
42     /**
43      * A MatchFunctor that negates the result of a contained functor.
44      */
45     class SHIBSP_DLLLOCAL NotMatchFunctor : public MatchFunctor
46     {
47     public:
48         NotMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p);
49
50         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
51             if (m_functor)
52                 return !(m_functor->evaluatePolicyRequirement(filterContext));
53             return false;
54         }
55
56         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
57             if (m_functor)
58                 return !(m_functor->evaluatePermitValue(filterContext, attribute, index));
59             return false;
60         }
61
62     private:
63         MatchFunctor* buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap);
64
65         const MatchFunctor* m_functor;
66     };
67
68     MatchFunctor* SHIBSP_DLLLOCAL NotMatchFunctorFactory(const pair<const FilterPolicyContext*,const DOMElement*>& p)
69     {
70         return new NotMatchFunctor(p);
71     }
72
73     static XMLCh _id[] =            UNICODE_LITERAL_2(i,d);
74     static XMLCh _ref[] =           UNICODE_LITERAL_3(r,e,f);
75     static XMLCh Rule[] =           UNICODE_LITERAL_4(R,u,l,e);
76     static XMLCh RuleReference[] =  UNICODE_LITERAL_13(R,u,l,e,R,e,f,e,r,e,n,c,e);
77 };
78
79 NotMatchFunctor::NotMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p) : m_functor(nullptr)
80 {
81     const DOMElement* e = XMLHelper::getFirstChildElement(p.second);
82     if (e) {
83         if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, Rule)) {
84             m_functor = buildFunctor(e, p.first);
85         }
86         else if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, RuleReference)) {
87             string ref = XMLHelper::getAttrString(e, nullptr, _ref);
88             if (!ref.empty()) {
89                 multimap<string,MatchFunctor*>::const_iterator rule = p.first->getMatchFunctors().find(ref);
90                 m_functor = (rule!=p.first->getMatchFunctors().end()) ? rule->second : nullptr;
91             }
92         }
93     }
94
95     if (!m_functor)
96         throw ConfigurationException("No child Rule installed into NotMatchFunctor.");
97 }
98
99 MatchFunctor* NotMatchFunctor::buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap)
100 {
101     // We'll track and map IDs just for consistency, but don't require them or worry about dups.
102     string id = XMLHelper::getAttrString(e, nullptr, _id);
103     if (!id.empty() && functorMap->getMatchFunctors().count(id))
104         id.clear();
105
106     auto_ptr<xmltooling::QName> type(XMLHelper::getXSIType(e));
107     if (!type.get())
108         throw ConfigurationException("Child Rule found with no xsi:type.");
109
110     MatchFunctor* func = SPConfig::getConfig().MatchFunctorManager.newPlugin(*type.get(), make_pair(functorMap,e));
111     functorMap->getMatchFunctors().insert(multimap<string,MatchFunctor*>::value_type(id, func));
112     return func;
113 }