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