daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * AccessRulesControler.cpp
  3.  *
  4.  *  Created on: Mar 17, 2014
  5.  *      Author: tshw
  6.  */
  7. #include "AccessRulesControler.h"
  8. #include "SimpleAccessRules.h"
  9. #include "TwoMenRuleAccessRules.h"
  10. #include "../ezlogger/ezlogger_headers.hpp"
  11. #include "../WebsocketComponents/UIWebSocketConnection.h"
  12. #include "HamiltonAccessRules.h"
  13. #include <boost/algorithm/string/join.hpp>
  14. #include <boost/algorithm/string.hpp>
  15. #include <vector>
  16.  
  17.  
  18. bool ZoneWing::AccessRulesControler::GetAccessRulesDefinition(int Channel, ZoneWing::AccessRulesDefinition & accessRulesDefinition)
  19. {
  20.     DBMutexLockGuard db_mutex_lock_guard;
  21.     bool retVal = false;
  22.     sqlite3_stmt * sql_stmt;
  23.     std::stringstream query_stream;
  24.     std::string query;
  25.     query_stream << std::string("select * from accessrules where ChannelID =") << Channel;
  26.     query = query_stream.str();
  27.     if(sqlite3_prepare_v2(mpSqlDBConnectionPtr->SqLitePtr(), query.c_str(), -1, &sql_stmt, NULL) != SQLITE_OK)
  28.     {
  29.         sqlite3_finalize(sql_stmt);
  30.         throw std::runtime_error( std::string( "SQL error at loading schedules" ) );
  31.     }
  32.  
  33.     unsigned evaluation_result = sqlite3_step(sql_stmt);
  34.     if(evaluation_result == SQLITE_ROW)
  35.     {
  36.         accessRulesDefinition.SetAccessResultType( static_cast< AccessRulesType >(sqlite3_column_int(sql_stmt, 1)) );
  37.         accessRulesDefinition.SetParameters( reinterpret_cast< const char *>(sqlite3_column_text(sql_stmt, 2)));
  38.         accessRulesDefinition.SetKeypad( static_cast< bool >(sqlite3_column_int(sql_stmt, 3)));
  39.         retVal = true;
  40.     }
  41.  
  42.  
  43.     if( sqlite3_finalize(sql_stmt) != SQLITE_OK )
  44.     {
  45.         throw std::runtime_error( std::string( "SQL error at loading schedules 2" ));
  46.     }
  47.  
  48.     return retVal;
  49. }
  50.  
  51.  
  52. void ZoneWing::AccessRulesControler::SetAccessRulesDefinition(int Channel, const ZoneWing::AccessRulesDefinition & accessRulesDefinition)
  53. {
  54.  
  55.     DBMutexLockGuard db_mutex_lock_guard;
  56.  
  57.     sqlite3_stmt * sql_stmt;
  58.     std::stringstream sql_stream;
  59.     std::string query;
  60.     unsigned evaluation_result;
  61.  
  62.  
  63.  
  64. //  sql_stream << std::string("insert or replace INTO accessrules (ChannelID, AccessRuleType, Parameters, ReportingParameter, Keypad) VALUES (?,?,?)");
  65.  
  66.     sql_stream << std::string("insert or replace INTO accessrules (ChannelID, AccessRuleType, Parameters, Keypad) VALUES (?,?,?,?)");
  67.  
  68.     query = sql_stream.str();
  69.  
  70.     if((sqlite3_prepare_v2(mpSqlDBConnectionPtr->SqLitePtr(), query.c_str(), -1, &sql_stmt, NULL) != SQLITE_OK) ||
  71.         (sqlite3_bind_int(sql_stmt, 1, Channel) != SQLITE_OK) ||
  72.         (sqlite3_bind_int(sql_stmt, 2, accessRulesDefinition.GetAccessRulesType()) != SQLITE_OK) ||
  73.         (sqlite3_bind_text(sql_stmt, 3, accessRulesDefinition.GetParameters().c_str(), accessRulesDefinition.GetParameters().size(), NULL) != SQLITE_OK) ||
  74.         (sqlite3_bind_int(sql_stmt, 4, accessRulesDefinition.GetKeypad()) != SQLITE_OK))
  75.     {
  76.         sqlite3_finalize(sql_stmt);
  77.         throw std::runtime_error( std::string( "SQL error adding access rules definitions! " ));
  78.     }
  79.  
  80.     evaluation_result = sqlite3_step(sql_stmt);
  81.     if(evaluation_result != SQLITE_DONE)
  82.     {
  83.         sqlite3_finalize(sql_stmt);
  84.         throw std::runtime_error( std::string( "SQL error adding access rules definitions! " ));
  85.     }
  86.  
  87.     if( sqlite3_finalize(sql_stmt) != SQLITE_OK )
  88.     {
  89.         throw std::runtime_error( std::string( "SQL error adding access rules definitions! " ));
  90.     }
  91.  
  92. }
  93.  
  94.  
  95. ZoneWing::AccessRulesControler::TCommResp ZoneWing::AccessRulesControler::DefineChannelProperties(ZoneWing::NetMessagePtr pNetMessage)
  96. {
  97.     /*Name: XS_ChannelProp
  98.     Sender:     HOST
  99.     Response: GenAck
  100.     Format:     C_Dchannel_TaccessRulesType_PaccessRulesParams
  101.     */
  102.  
  103.     int Channel = -1;
  104.     bool accessRuleDefined = false;
  105.     std::string params = "";
  106.  
  107.  
  108.     AccessRulesDefinition access_rules_definition;
  109.     access_rules_definition.SetAccessResultType( AR_SIMPLE );
  110.     access_rules_definition.SetKeypad(false);
  111.  
  112.     if(pNetMessage->DoesFieldExist(string(1,'D')))
  113.     {
  114.         Channel = pNetMessage->GetIntegerField(std::string(1,'D'));
  115.     }
  116.  
  117.     if(pNetMessage->DoesFieldExist(std::string(1,'T')))
  118.     {
  119.         access_rules_definition.SetAccessResultType( static_cast< AccessRulesType >(pNetMessage->GetIntegerField(std::string(1,'T'))));
  120.         accessRuleDefined = true;
  121.     }
  122.  
  123.     if(pNetMessage->DoesFieldExist(std::string(1,'K')))
  124.     {
  125.         if(pNetMessage->GetIntegerField(std::string(1,'K'))>0)
  126.         {
  127.             access_rules_definition.SetKeypad(true);
  128.         }
  129.     }
  130.  
  131.     if(pNetMessage->DoesFieldExist(std::string(1,'P')))
  132.     {
  133.         params = pNetMessage->rGetField(std::string(1,'P'));
  134.         //We need to make sure there are no newline and carriage return chars in the string... Lexical cast later on fails if this is left in the string.
  135.         if(!params.empty() && params[params.size() -1] == '\n')
  136.         {
  137.             params.erase(params.size() - 1);
  138.         }
  139.         if(!params.empty() && params[params.size() -1] == '\r')
  140.         {
  141.             params.erase(params.size() - 1);
  142.         }
  143.         access_rules_definition.SetParameters(params);
  144.     }
  145.  
  146.     if(pNetMessage->DoesFieldExist(std::string(1,'S')))
  147.     {
  148.  
  149.         if(Channel == 0) //this monstrocity is result of the fact, this information is sent in every channel despite being a device wide setting......... Software issue.
  150.         {
  151.             bool enterSettings = false;
  152.             if(pNetMessage->GetIntegerField(std::string(1,'S'))>0)
  153.             {
  154.                 enterSettings = true;
  155.             }
  156.             else
  157.             {
  158.                 enterSettings = false;
  159.             }
  160.  
  161.             HamiltonAccessRules *hamrules = dynamic_cast<HamiltonAccessRules*>(mAccessRules[0].get());
  162.  
  163.             if(hamrules)
  164.             {
  165.                 hamrules->setEnterSettings(enterSettings);
  166.             }
  167.         }
  168.     }
  169.  
  170.     if(pNetMessage->DoesFieldExist(std::string(1,'H')))
  171.     {
  172.  
  173.         if(Channel == 0) //see monstrosity comment above
  174.         {
  175.             bool touchscreen = true;
  176.             if(pNetMessage->GetIntegerField(std::string(1,'H'))>0)
  177.             {
  178.                 touchscreen = true;
  179.             }
  180.             else
  181.             {
  182.                 touchscreen = false;
  183.             }
  184.  
  185.             HamiltonAccessRules *hamrules = dynamic_cast<HamiltonAccessRules*>(mAccessRules[0].get());
  186.  
  187.             if(hamrules)
  188.             {
  189.                 hamrules->setTouchscreenEnabled(touchscreen);
  190.             }
  191.         }
  192.     }
  193.  
  194.     if(Channel >= 0 && accessRuleDefined)
  195.     {
  196.         SetAccessRulesDefinition(Channel, access_rules_definition);
  197.         //InitAccessRule(Channel, false);
  198.     }
  199.  
  200.     return crAck;
  201. }
  202.  
  203. ZoneWing::AccessRulesControler::TCommResp ZoneWing::AccessRulesControler::DefineReportingParametersParser(ZoneWing::NetMessagePtr pNetMessage)
  204. /*
  205.  This packet is used to define alternate reporting functionality.
  206. Name:   XS_DefSch
  207. Sender:     HOST
  208. Response: GenAck
  209. Format:     C_Dchannel_EreportParameter
  210. D-channel number
  211. E-reporting parameter - 0 - Stops reporting of registrations which are not "granted"
  212. */
  213. {
  214.     TCommResp retVal = crAck;
  215.     int channel = -1;
  216.     string reporting_parameter = "";
  217.  
  218.     if(pNetMessage->DoesFieldExist(std::string(1,'D')))
  219.     {
  220.         channel = pNetMessage->GetIntegerField(std::string(1,'D'));
  221.     }
  222.  
  223.     if(pNetMessage->DoesFieldExist(std::string(1,'E')))
  224.     {
  225.         reporting_parameter = pNetMessage->rGetField(std::string(1,'E'));
  226.     }
  227.  
  228.     if(channel != -1 && reporting_parameter != "")
  229.     {
  230.         ReportingParameterPtr parameter = boost::make_shared< ReportingParameterObj >();
  231.         parameter->Channel = channel;
  232.         parameter->ReportingParameter = reporting_parameter;
  233.         mpReportingParameters.push_back(parameter);
  234.     }
  235.     else
  236.     {
  237.         EZLOGGERSTREAM << "Unknown ExtendedConfigFile command (reporting parameter). "  << std::endl;
  238.     }
  239.  
  240.     return retVal;
  241. }
  242.  
  243. ZoneWing::AccessRulesControler::TCommResp ZoneWing::AccessRulesControler::DefineDapZ1TTimeout(ZoneWing::NetMessagePtr pNetMessage)
  244. {
  245.     TCommResp retVal = crAck;
  246.     AccessRulesDefinition access_rules_definition;
  247.     std::string timeout;
  248.  
  249.     if(pNetMessage->DoesFieldExist(std::string(1,'T')))
  250.     {
  251.         timeout = pNetMessage->rGetField(std::string(1,'T'));
  252.         std::cout << "Got the time and am setting it to: " << timeout << std::endl;
  253.     }
  254.  
  255.     for(int channelID = 0;channelID<64;channelID++)
  256.     {
  257.         if(mAccessRules[channelID].get() != NULL)
  258.         {
  259.             if(GetAccessRulesDefinition(channelID, access_rules_definition))
  260.             {
  261.                 if(access_rules_definition.GetAccessRulesType() == AR_HAMILTON)
  262.                 {
  263.                     std::string resource_file_name = "";
  264.                     std::vector<std::string> tmpVector;
  265.  
  266.                     boost::split(tmpVector, access_rules_definition.GetParameters(), boost::is_any_of(";"));
  267.  
  268.                     if(tmpVector.size() >= 2)
  269.                     {
  270.                         resource_file_name = tmpVector[2];
  271.                     }
  272.  
  273.                     ptree pt;
  274.                     if(resource_file_name != "")
  275.                     {
  276.                         std::ifstream infile;
  277.                         ptree child;
  278.                         ptree resourcePacket;
  279.  
  280.                         infile.open(resource_file_name.c_str());
  281.                         std::stringstream strStream;
  282.                         strStream << infile.rdbuf();
  283.  
  284.                         boost::property_tree::json_parser::read_json(strStream, child);
  285.                         child.put("Settings.dapResponseTimeout", timeout);
  286.                         boost::property_tree::write_json(resource_file_name,child);
  287.                     }
  288.                 }
  289.             }
  290.         }
  291.     }
  292.  
  293.     return retVal;
  294. }
  295.  
  296.  
  297. ZoneWing::AccessRulesControler::TCommResp ZoneWing::AccessRulesControler::DefineRegexFilter(ZoneWing::NetMessagePtr pNetMessage)
  298. /*
  299.  This packet is used to define single schedule.
  300. Name:   XS_DefSch
  301. Sender:     HOST
  302. Response: GenAck
  303. Format:     b_Cchannel_Eexclusivity_RregexExpression
  304. C       - channel
  305. E       - exclusivity
  306. R       - regex expression
  307. */
  308. {
  309.     TCommResp retVal = crAck;
  310.     int channel = -1;
  311.     bool isExclusivitySet = false;
  312.     bool exclusivity = false;
  313.     string regex_expression = "";
  314.  
  315.     if(pNetMessage->DoesFieldExist(std::string(1,'C')))
  316.     {
  317.         channel = pNetMessage->GetIntegerField(string(1,'C'));
  318.     }
  319.  
  320.     if(pNetMessage->DoesFieldExist(std::string(1,'E')))
  321.     {
  322.         isExclusivitySet  = true;
  323.         exclusivity = !!(pNetMessage->GetIntegerField(string(1,'E')));
  324.     }
  325.  
  326.     if(pNetMessage->DoesFieldExist(std::string(1,'R')))
  327.     {
  328.         regex_expression = pNetMessage->rGetField(string(1,'R'));
  329.         if(!regex_expression.empty() && regex_expression[regex_expression.size() -1] == '\n')
  330.         {
  331.             regex_expression.erase(regex_expression.size() -
  332.                     1);
  333.         }
  334.         if(!regex_expression.empty() && regex_expression[regex_expression.size() -1] == '\r')
  335.         {
  336.             regex_expression.erase(regex_expression.size() - 1);
  337.         }
  338.     }
  339.  
  340.     if(channel != -1 && regex_expression != "" && isExclusivitySet)
  341.     {
  342.         RegexFilterPtr filters = boost::make_shared< RegexFilter >();
  343.         filters->Channel = channel;
  344.         filters->Exclusivity = exclusivity;
  345.         filters->RegexExpression = regex_expression;
  346.         mpRegexFilters.push_back(filters);
  347.     }
  348.     else
  349.     {
  350.         EZLOGGERSTREAM << "Unknown ExtendedConfigFile command (badge filters). "  << std::endl;
  351.     }
  352.  
  353.     return retVal;
  354. }
  355.  
  356.  
  357. ZoneWing::AccessRulesControler::TCommResp ZoneWing::AccessRulesControler::DefineUiConfigFile(ZoneWing::NetMessagePtr pNetMessage)
  358. {
  359.     AccessRulesDefinition access_rules_definition;
  360.     TCommResp retVal = crAck;
  361.     int channelID = 0;
  362.     std::string resource_file_name = "";
  363.     int first_index = 0;
  364.     int second_index = 0;
  365.     std::vector<std::string> translations_vector;
  366.     std::vector<std::string> ptree_translations_vector = {
  367.                                                             "Translations.Dates.Day.Sun",
  368.                                                             "Translations.Dates.Day.Mon",
  369.                                                             "Translations.Dates.Day.Tue",
  370.                                                             "Translations.Dates.Day.Wed",
  371.                                                             "Translations.Dates.Day.Thu",
  372.                                                             "Translations.Dates.Day.Fri",
  373.                                                             "Translations.Dates.Day.Sat",
  374.                                                             "Translations.Dates.Month.Jan",
  375.                                                             "Translations.Dates.Month.Feb",
  376.                                                             "Translations.Dates.Month.Mar",
  377.                                                             "Translations.Dates.Month.Apr",
  378.                                                             "Translations.Dates.Month.May",
  379.                                                             "Translations.Dates.Month.Jun",
  380.                                                             "Translations.Dates.Month.Jul",
  381.                                                             "Translations.Dates.Month.Aug",
  382.                                                             "Translations.Dates.Month.Sep",
  383.                                                             "Translations.Dates.Month.Oct",
  384.                                                             "Translations.Dates.Month.Nov",
  385.                                                             "Translations.Dates.Month.Dec",
  386.                                                             "Translations.UI.PlaceBadge",
  387.                                                             "Translations.UI.SelectEvent",
  388.                                                             "Translations.UI.PlaceCard",
  389.                                                             "Translations.UI.PlaceFinger",
  390.                                                             "Translations.UI.EnterPIN",
  391.                                                             "Translations.UI.EnterBadge",
  392.                                                             "Translations.UI.EnterNumberic",
  393.                                                             "Translations.UI.RegistrationSuccessful",
  394.                                                             "Translations.UI.RegistrationUnsuccessful"
  395.                                                          };
  396.  
  397.  
  398.     if(pNetMessage->DoesFieldExist(std::string(1,'S')))
  399.     {
  400.         first_index = pNetMessage->GetIntegerField(std::string(1,'S'))-1;
  401.     }
  402.  
  403.     if(pNetMessage->DoesFieldExist(std::string(1,'E')))
  404.     {
  405.         second_index = pNetMessage->GetIntegerField(std::string(1,'E'))-1;
  406.     }
  407.  
  408.  
  409.     if(pNetMessage->DoesFieldExist(std::string(1,'L')))
  410.     {
  411.         boost::split(translations_vector,pNetMessage->rGetField(std::string(1,'L')),boost::is_any_of(";"));
  412.     }
  413.  
  414.  
  415.     for(;channelID<64;channelID++)
  416.     {
  417.         if(mAccessRules[channelID].get() != NULL)
  418.         {
  419.             if(GetAccessRulesDefinition(channelID, access_rules_definition))
  420.             {
  421.                 if(access_rules_definition.GetAccessRulesType() == AR_HAMILTON)
  422.                 {
  423.                     std::string resource_file_name = "";
  424.                     std::vector<std::string> tmpVector;
  425.  
  426.                     boost::split(tmpVector, access_rules_definition.GetParameters(), boost::is_any_of(";"));
  427.  
  428.                     if(tmpVector.size() >= 2)
  429.                     {
  430.                         resource_file_name = tmpVector[2];
  431.                     }
  432.  
  433.                     ptree pt;
  434.                     if(resource_file_name != "")
  435.                     {
  436.                         std::ifstream infile;
  437.                         ptree child;
  438.                         ptree resourcePacket;
  439.  
  440.                         infile.open(resource_file_name.c_str());
  441.                         std::stringstream strStream;
  442.                         strStream << infile.rdbuf();
  443.  
  444.                         boost::property_tree::json_parser::read_json(strStream, child);
  445.  
  446.                         for(int x = 0;first_index<second_index;first_index++)
  447.                         {
  448.                             child.put(ptree_translations_vector[first_index], translations_vector[x]);
  449.                             x++;
  450.                         }
  451.  
  452.                         boost::property_tree::write_json(resource_file_name,child);
  453.                     }
  454.                 }
  455.             }
  456.         }
  457.     }
  458.     return retVal;
  459. }
  460.  
  461. void ZoneWing::AccessRulesControler::InitAccessRule(int channelID, bool create)
  462. {
  463.     AccessRulesDefinition access_rules_definition;
  464.  
  465.     if(mAccessRules[channelID].get() == NULL && create == false)
  466.     {
  467.         return;
  468.     }
  469.  
  470.  
  471.  
  472.     if(GetAccessRulesDefinition(channelID, access_rules_definition))
  473.     {
  474.         switch(access_rules_definition.GetAccessRulesType())
  475.         {
  476.             case AR_SIMPLE:
  477.             {
  478.                 mAccessRules[channelID] = boost::make_shared< SimpleAccessRules > (mChannels[channelID], mpUserProfileLoader, mpSchedules ,mpEventDefLoader, mpAPB, mpRegexFilters, mpReportingParameters,access_rules_definition.GetKeypad());
  479.                 break;
  480.             }
  481.             case AR_TMR:
  482.             {
  483.                 int timeout;
  484.                 timeout = boost::lexical_cast<int>(access_rules_definition.GetParameters());
  485.  
  486.                 EZLOGGERSTREAM << "Creating two men rule on channel: " << channelID << " with parameters: " << access_rules_definition.GetParameters()  << std::endl;
  487.                 mAccessRules[channelID] = boost::make_shared< TwoMenRuleAccessRules >(mChannels[channelID], mpUserProfileLoader, mpSchedules ,mpEventDefLoader, mpRegexFilters, mpReportingParameters, access_rules_definition.GetKeypad(), timeout );
  488.  
  489.  
  490.                 break;
  491.             }
  492.             case AR_HAMILTON:
  493.             {
  494.                 int tcp_port = 2200;
  495.                 int morpho_tcp_port = 2300;
  496.                 std::string resource_file_name = "hamilton.json";
  497.                 std::string factory_file_name = "factory.json";
  498.                 std::vector<std::string> lVector;
  499.  
  500.                 boost::split(lVector, access_rules_definition.GetParameters(), boost::is_any_of(";"));
  501.  
  502.                 std::cout << "Hamilton AC: " << lVector[0] << " " << lVector[1] << std::endl;
  503.  
  504.                 if(lVector.size() >= 1)
  505.                     tcp_port = boost::lexical_cast<int>(lVector[0]);
  506.                 if(lVector.size() >= 2)
  507.                     morpho_tcp_port = boost::lexical_cast<int>(lVector[1]);
  508.                 if(lVector.size() >= 3)
  509.                     resource_file_name = lVector[2];
  510.                 if(lVector.size() >= 4)
  511.                     factory_file_name = lVector[3];
  512.  
  513.                 EZLOGGERSTREAM << "Creating hamilton rules: " << channelID << " with parameters: " << access_rules_definition.GetParameters()  << " and resource file: " << resource_file_name << std::endl;
  514.  
  515.                 UIWebSocketConnectionPtr gui_connection = boost::make_shared< UIWebSocketConnection >(tcp_port );
  516.  
  517.                 UIWebSocketConnectionPtr morpho_connection = boost::make_shared< UIWebSocketConnection >(morpho_tcp_port);
  518.                 EZLOGGERSTREAM << "Websocket created" << std::endl;
  519.                 mAccessRules[channelID] = boost::make_shared< HamiltonAccessRules >(mChannels[channelID], mpUserProfileLoader, mpSchedules ,mpEventDefLoader, mpAPB, gui_connection, morpho_connection, resource_file_name, factory_file_name, mpSqlDBConnectionPtr);
  520.                 EZLOGGERSTREAM << "Rules created" << std::endl;
  521.                 break;
  522.             }
  523.         }
  524.     }
  525.     else  // if access rules are not defined, use default
  526.     {
  527.         mAccessRules[channelID] = boost::make_shared< SimpleAccessRules > (mChannels[channelID], mpUserProfileLoader, mpSchedules ,mpEventDefLoader, mpAPB, mpRegexFilters , mpReportingParameters, false );
  528.     }
  529.     if(mAccessRules[channelID].get() != NULL)
  530.     {
  531.         mAccessRules[channelID]->InitAccessRules();
  532.     }
  533.     else
  534.     {
  535.         EZLOGGERSTREAM << "Access rules not defined on channel: " << channelID  << std::endl;
  536.     }
  537. }
  538.  
  539.  
  540. void ZoneWing::AccessRulesControler::InitAccessRules()
  541. {
  542.     EZLOGGERSTREAM << "Initializing access rules... " << std::endl;
  543.  
  544.     for(int i = 0; i < MAX_CHECKPOINT; i++)
  545.     {
  546.         InitAccessRule(i);
  547.     }
  548. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top