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.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 * A MatchFunctor that logical ORs the results of contained functors.
28 #include "exceptions.h"
29 #include "attribute/filtering/FilterPolicyContext.h"
30 #include "attribute/filtering/MatchFunctor.h"
31 #include "util/SPConstants.h"
33 #include <boost/bind.hpp>
34 #include <xercesc/util/XMLUniDefs.hpp>
35 #include <xmltooling/util/XMLHelper.h>
37 using namespace shibsp;
38 using namespace xmltooling;
39 using namespace boost;
45 * A MatchFunctor that logical ORs the results of contained functors.
47 class SHIBSP_DLLLOCAL OrMatchFunctor : public MatchFunctor
50 OrMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p);
52 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
53 vector<const MatchFunctor*>::const_iterator i = find_if(
54 m_functors.begin(), m_functors.end(),
55 boost::bind(&MatchFunctor::evaluatePolicyRequirement, _1, boost::cref(filterContext)) == true
57 return (i != m_functors.end());
60 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
61 vector<const MatchFunctor*>::const_iterator i = find_if(
62 m_functors.begin(), m_functors.end(),
63 boost::bind(&MatchFunctor::evaluatePermitValue, _1, boost::cref(filterContext), boost::cref(attribute), index) == true
65 return (i != m_functors.end());
69 MatchFunctor* buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap);
71 vector<const MatchFunctor*> m_functors;
74 MatchFunctor* SHIBSP_DLLLOCAL OrMatchFunctorFactory(const pair<const FilterPolicyContext*,const DOMElement*>& p)
76 return new OrMatchFunctor(p);
79 static XMLCh _id[] = UNICODE_LITERAL_2(i,d);
80 static XMLCh _ref[] = UNICODE_LITERAL_3(r,e,f);
81 static XMLCh Rule[] = UNICODE_LITERAL_4(R,u,l,e);
82 static XMLCh RuleReference[] = UNICODE_LITERAL_13(R,u,l,e,R,e,f,e,r,e,n,c,e);
85 OrMatchFunctor::OrMatchFunctor(const pair<const FilterPolicyContext*,const DOMElement*>& p)
88 const DOMElement* e = XMLHelper::getFirstChildElement(p.second);
91 if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, Rule)) {
92 func = buildFunctor(e, p.first);
94 else if (XMLHelper::isNodeNamed(e, shibspconstants::SHIB2ATTRIBUTEFILTER_MF_BASIC_NS, RuleReference)) {
95 string ref = XMLHelper::getAttrString(e, nullptr, _ref);
97 multimap<string,MatchFunctor*>::const_iterator rule = p.first->getMatchFunctors().find(ref);
98 func = (rule!=p.first->getMatchFunctors().end()) ? rule->second : nullptr;
103 m_functors.push_back(func);
105 e = XMLHelper::getNextSiblingElement(e);
109 MatchFunctor* OrMatchFunctor::buildFunctor(const DOMElement* e, const FilterPolicyContext* functorMap)
111 // We'll track and map IDs just for consistency, but don't require them or worry about dups.
112 string id = XMLHelper::getAttrString(e, nullptr, _id);
113 if (!id.empty() && functorMap->getMatchFunctors().count(id))
116 scoped_ptr<xmltooling::QName> type(XMLHelper::getXSIType(e));
118 throw ConfigurationException("Child Rule found with no xsi:type.");
120 auto_ptr<MatchFunctor> func(SPConfig::getConfig().MatchFunctorManager.newPlugin(*type, make_pair(functorMap,e)));
121 functorMap->getMatchFunctors().insert(multimap<string,MatchFunctor*>::value_type(id, func.get()));
122 return func.release();