Back to index

nordugrid-arc-nox  1.1.0~rc6
MatchFunction.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include "MatchFunction.h"
00006 #include "../attr/BooleanAttribute.h"
00007 #include "../attr/StringAttribute.h"
00008 #include "../attr/DateTimeAttribute.h"
00009 #include "../attr/X500NameAttribute.h"
00010 #include "../attr/AnyURIAttribute.h"
00011 
00012 namespace ArcSec {
00013 
00014 std::string MatchFunction::getFunctionName(std::string datatype){
00015   std::string ret;
00016   if (datatype ==  StringAttribute::getIdentifier()) ret = NAME_REGEXP_STRING_MATCH;
00017   else if(datatype == AnyURIAttribute::getIdentifier()) ret = NAME_ANYURI_REGEXP_MATCH;
00018   else if(datatype == X500NameAttribute::getIdentifier()) ret = NAME_X500NAME_REGEXP_MATCH;
00019   return ret;
00020 }
00021 
00022 MatchFunction::MatchFunction(std::string functionName, std::string argumentType) : Function(functionName, argumentType) {
00023   fnName = functionName;
00024   argType = argumentType;
00025 }
00026 
00027 AttributeValue* MatchFunction::evaluate(AttributeValue* arg0, AttributeValue* arg1, bool check_id){
00028   //TODO
00029   //arg0 is the attributevalue in policy
00030   //arg1 is the attributevalue in request
00031   if(check_id) { if(arg0->getId() != arg1->getId()) return new BooleanAttribute(false); }
00032   std::string label = arg0->encode();
00033   std::string value = arg1->encode();
00034   Arc::RegularExpression regex(label);
00035   if(regex.isOk()){
00036     std::list<std::string> unmatched, matched;
00037     if(regex.match(value, unmatched, matched))
00038       return new BooleanAttribute(true);
00039   }
00040   // std::cerr<<"Bad Regex"<<std::endl;
00041   return new BooleanAttribute(false);
00042 }
00043 
00044 std::list<AttributeValue*> MatchFunction::evaluate(std::list<AttributeValue*> args, bool check_id) {
00045   AttributeValue* arg0 = NULL;
00046   AttributeValue* arg1 = NULL;
00047   std::list<AttributeValue*>::iterator it = args.begin();
00048   arg0 = *it; it++;
00049   if(it!= args.end()) arg1 = *it;
00050   if(check_id) { 
00051     if(arg0->getId() != arg1->getId()) {
00052       std::list<AttributeValue*> ret;
00053       ret.push_back(new BooleanAttribute(false));
00054       return ret;
00055     }
00056   }
00057   AttributeValue* res = evaluate(arg0, arg1);
00058   std::list<AttributeValue*> ret;
00059   ret.push_back(res);
00060   return ret;
00061 }
00062 }