Imported Upstream version 2.0
[shibboleth/sp.git] / shibsp / attribute / filtering / impl / AttributeScopeStringFunctor.cpp
1 /*
2  *  Copyright 2001-2007 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  * AttributeScopeStringFunctor.cpp
19  * 
20  * A match function that matches the scope of an attribute value against the specified value.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "attribute/Attribute.h"
26 #include "attribute/filtering/FilteringContext.h"
27 #include "attribute/filtering/FilterPolicyContext.h"
28
29 using namespace shibsp;
30 using namespace std;
31
32 namespace shibsp {
33
34     static const XMLCh attributeID[] =  UNICODE_LITERAL_11(a,t,t,r,i,b,u,t,e,I,D);
35     static const XMLCh value[] =        UNICODE_LITERAL_5(v,a,l,u,e);
36
37     /**
38      * A match function that matches the scope of an attribute value against the specified value.
39      */
40     class SHIBSP_DLLLOCAL AttributeScopeStringFunctor : public MatchFunctor
41     {
42         xmltooling::auto_ptr_char m_value;
43         xmltooling::auto_ptr_char m_attributeID;
44
45         bool hasScope(const FilteringContext& filterContext) const;
46
47     public:
48         AttributeScopeStringFunctor(const DOMElement* e)
49             : m_value(e ? e->getAttributeNS(NULL,value) : NULL), m_attributeID(e ? e->getAttributeNS(NULL,attributeID) : NULL) {
50             if (!m_value.get() || !*m_value.get())
51                 throw ConfigurationException("AttributeScopeString MatchFunctor requires non-empty value attribute.");
52         }
53
54         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
55             if (!m_attributeID.get() || !*m_attributeID.get())
56                 throw AttributeFilteringException("No attributeID specified.");
57             return hasScope(filterContext);
58         }
59
60         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
61             if (!m_attributeID.get() || !*m_attributeID.get() || XMLString::equals(m_attributeID.get(), attribute.getId()))
62                 return XMLString::equals(attribute.getScope(index), m_value.get());
63             return hasScope(filterContext);
64         }
65     };
66
67     MatchFunctor* SHIBSP_DLLLOCAL AttributeScopeStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
68     {
69         return new AttributeScopeStringFunctor(p.second);
70     }
71
72 };
73
74 bool AttributeScopeStringFunctor::hasScope(const FilteringContext& filterContext) const
75 {
76     size_t count;
77     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
78         filterContext.getAttributes().equal_range(m_attributeID.get());
79     for (; attrs.first != attrs.second; ++attrs.first) {
80         count = attrs.first->second->valueCount();
81         for (size_t index = 0; index < count; ++index) {
82             if (XMLString::equals(attrs.first->second->getScope(index), m_value.get()))
83                 return true;
84         }
85     }
86     return false;
87 }