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