2 * Copyright 2001-2007 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * NumberOfAttributeValuesFunctor.cpp
20 * A match function that evaluates to true if the given attribute has as a number
\r
21 * of values that falls between the minimum and maximum.
25 #include "exceptions.h"
26 #include "attribute/Attribute.h"
27 #include "attribute/filtering/FilteringContext.h"
28 #include "attribute/filtering/FilterPolicyContext.h"
30 using namespace shibsp;
35 static const XMLCh attributeID[] = UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
36 static const XMLCh maximum[] = UNICODE_LITERAL_7(m,a,x,i,m,u,m);
37 static const XMLCh minimum[] = UNICODE_LITERAL_7(m,i,n,i,m,u,m);
40 * A match function that matches the scope of an attribute value against the specified value.
42 class SHIBSP_DLLLOCAL NumberOfAttributeValuesFunctor : public MatchFunctor
44 unsigned int m_min,m_max;
45 xmltooling::auto_ptr_char m_attributeID;
47 size_t count(const FilteringContext& filterContext) const;
50 NumberOfAttributeValuesFunctor(const DOMElement* e)
51 : m_min(0), m_max(INT_MAX), m_attributeID(e ? e->getAttributeNS(NULL,attributeID) : NULL) {
52 if (!m_attributeID.get() || !*m_attributeID.get())
53 throw ConfigurationException("No attributeID specified.");
54 const XMLCh* num = e->getAttributeNS(NULL, minimum);
56 m_min = XMLString::parseInt(num);
57 num = e->getAttributeNS(NULL, maximum);
59 m_max = XMLString::parseInt(num);
62 bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
63 size_t c = count(filterContext);
64 return (m_min <= c && c <= m_max);
67 bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
68 size_t c = count(filterContext);
69 return (m_min <= c && c <= m_max);
73 MatchFunctor* SHIBSP_DLLLOCAL NumberOfAttributeValuesFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
75 return new NumberOfAttributeValuesFunctor(p.second);
80 size_t NumberOfAttributeValuesFunctor::count(const FilteringContext& filterContext) const
83 pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
84 filterContext.getAttributes().equal_range(m_attributeID.get());
85 for (; attrs.first != attrs.second; ++attrs.first)
86 count += attrs.first->second->valueCount();