Xerces 3 revisions.
[shibboleth/cpp-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 ignoreCase[] =   UNICODE_LITERAL_10(i,g,n,o,r,e,C,a,s,e);
36     static const XMLCh value[] =        UNICODE_LITERAL_5(v,a,l,u,e);
37
38     /**
39      * A match function that matches the scope of an attribute value against the specified value.
40      */
41     class SHIBSP_DLLLOCAL AttributeScopeStringFunctor : public MatchFunctor
42     {
43         xmltooling::auto_ptr_char m_attributeID;
44         char* m_value;
45         bool m_ignoreCase;
46
47         bool hasScope(const FilteringContext& filterContext) const;
48
49     public:
50         AttributeScopeStringFunctor(const DOMElement* e)
51             : m_value(e ? xmltooling::toUTF8(e->getAttributeNS(NULL,value)) : NULL), m_attributeID(e ? e->getAttributeNS(NULL,attributeID) : NULL) {
52             if (!m_value || !*m_value) {
53                 delete[] m_value;
54                 throw ConfigurationException("AttributeScopeString MatchFunctor requires non-empty value attribute.");
55             }
56             const XMLCh* flag = e ? e->getAttributeNS(NULL,ignoreCase) : NULL;
57             m_ignoreCase = (flag && (*flag == chLatin_t || *flag == chDigit_1)); 
58         }
59
60         virtual ~AttributeScopeStringFunctor() {
61             delete[] m_value;
62         }
63
64         bool evaluatePolicyRequirement(const FilteringContext& filterContext) const {
65             if (!m_attributeID.get() || !*m_attributeID.get())
66                 throw AttributeFilteringException("No attributeID specified.");
67             return hasScope(filterContext);
68         }
69
70         bool evaluatePermitValue(const FilteringContext& filterContext, const Attribute& attribute, size_t index) const {
71             if (!m_attributeID.get() || !*m_attributeID.get() || XMLString::equals(m_attributeID.get(), attribute.getId())) {
72                 if (m_ignoreCase) {
73 #ifdef HAVE_STRCASECMP
74                     return !strcasecmp(attribute.getScope(index), m_value);
75 #else
76                     return !stricmp(attribute.getScope(index), m_value);
77 #endif
78                 }
79                 else
80                     return !strcmp(attribute.getScope(index), m_value);
81             }
82             return hasScope(filterContext);
83         }
84     };
85
86     MatchFunctor* SHIBSP_DLLLOCAL AttributeScopeStringFactory(const std::pair<const FilterPolicyContext*,const DOMElement*>& p)
87     {
88         return new AttributeScopeStringFunctor(p.second);
89     }
90
91 };
92
93 bool AttributeScopeStringFunctor::hasScope(const FilteringContext& filterContext) const
94 {
95     size_t count;
96     pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
97         filterContext.getAttributes().equal_range(m_attributeID.get());
98     for (; attrs.first != attrs.second; ++attrs.first) {
99         count = attrs.first->second->valueCount();
100         for (size_t index = 0; index < count; ++index) {
101             if (m_ignoreCase) {
102 #ifdef HAVE_STRCASECMP
103                 if (!strcasecmp(attrs.first->second->getScope(index), m_value))
104                     return true;
105 #else
106                 if (!stricmp(attrs.first->second->getScope(index), m_value))
107                     return true;
108 #endif
109             }
110             else {
111                 if (!strcmp(attrs.first->second->getScope(index), m_value))
112                     return true;
113             }
114         }
115     }
116     return false;
117 }