Change license header.
[shibboleth/cpp-sp.git] / shibsp / attribute / filtering / impl / NumberOfAttributeValuesFunctor.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  * NumberOfAttributeValuesFunctor.cpp
23  * 
24  * A match function that evaluates to true if the given attribute has as a number
25  * of values that falls between the minimum and maximum.
26  */
27
28 #include "internal.h"
29 #include "exceptions.h"
30 #include "attribute/Attribute.h"
31 #include "attribute/filtering/FilteringContext.h"
32 #include "attribute/filtering/FilterPolicyContext.h"
33 #include "attribute/filtering/MatchFunctor.h"
34
35 #include <xmltooling/util/XMLHelper.h>
36
37 using namespace shibsp;
38 using namespace std;
39 using xmltooling::XMLHelper;
40
41 namespace shibsp {
42
43     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
44     static const XMLCh maximum[] =      UNICODE_LITERAL_7(m,a,x,i,m,u,m);
45     static const XMLCh minimum[] =      UNICODE_LITERAL_7(m,i,n,i,m,u,m);
46
47     /**
48      * A match function that evaluates to true if the given attribute has as a number
49      * of values that falls between the minimum and maximum.
50      */
51     class SHIBSP_DLLLOCAL NumberOfAttributeValuesFunctor : public MatchFunctor
52     {
53         unsigned int m_min,m_max;
54         string m_attributeID;
55
56         size_t count(const FilteringContext& filterContext) const;
57
58     public:
59         NumberOfAttributeValuesFunctor(const DOMElement* e)
60             : m_min(XMLHelper::getAttrInt(e, 0, minimum)),
61                 m_max(XMLHelper::getAttrInt(e, INT_MAX, maximum)),
62                 m_attributeID(XMLHelper::getAttrString(e, nullptr, attributeID)) {
63             if (m_attributeID.empty())
64                 throw ConfigurationException("No attributeID specified.");
65         }
66
67         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
68             size_t c = count(filterContext);
69             return (m_min <= c && c <= m_max);
70         }
71
72         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
73             size_t c = count(filterContext);
74             return (m_min <= c && c <= m_max);
75         }
76     };
77
78     MatchFunctor* SHIBSP_DLLLOCAL NumberOfAttributeValuesFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
79     {
80         return new NumberOfAttributeValuesFunctor(p.second);
81     }
82
83 };
84
85 size_t NumberOfAttributeValuesFunctor::count(const FilteringContext& filterContext) const
86 {
87     size_t count = 0;
88     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
89         filterContext.getAttributes().equal_range(m_attributeID);
90     for (; attrs.first != attrs.second; ++attrs.first)
91         count += attrs.first->second->valueCount();
92     return count;
93 }