Leosac  0.8.0
Open Source Access Control
XmlScheduleLoader.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
22 #include "tools/Schedule.hpp"
23 #include "tools/log.hpp"
24 #include <boost/algorithm/string.hpp>
25 #include <boost/property_tree/ptree.hpp>
26 
27 using namespace Leosac::Tools;
28 
29 int XmlScheduleLoader::week_day_to_int(const std::string &day)
30 {
31  if (day == "sunday")
32  return 0;
33  if (day == "monday")
34  return 1;
35  if (day == "tuesday")
36  return 2;
37  if (day == "wednesday")
38  return 3;
39  if (day == "thursday")
40  return 4;
41  if (day == "friday")
42  return 5;
43  if (day == "saturday")
44  return 6;
45  ASSERT_LOG(0, "{" << day << "} is not a valid day of the week.");
46  return -1;
47 }
48 
49 bool XmlScheduleLoader::load(const boost::property_tree::ptree &schedules_tree)
50 {
51  // make sure the map is clear.
52  assert(schedules_.begin() == schedules_.end());
53  for (const auto &sched : schedules_tree)
54  {
55  const std::string &node_name = sched.first;
56  const boost::property_tree::ptree &node = sched.second;
57 
58  if (node_name != "schedule")
59  {
60  std::string err = "Invalid configuration file content. Expected a XML "
61  "node named 'schedule' but found " +
62  node_name + " instead.";
63  ERROR(err);
64  return false;
65  }
66  extract_one(node);
67  }
68  return true;
69 }
70 
71 bool XmlScheduleLoader::extract_one(const boost::property_tree::ptree &node)
72 {
73  std::string schedule_name = node.get<std::string>("name");
74  Schedule sched(schedule_name);
75 
76  // loop on all properties of the schedule.
77  // those will be weekday and the <name> tag too.
78  for (const auto &sched_data : node)
79  {
80  if (sched_data.first == "name") // we already got the name.
81  continue;
82  std::string start = sched_data.second.get<std::string>("start");
83  std::string end = sched_data.second.get<std::string>("end");
84  std::vector<std::string> temp;
85 
86  boost::split(temp, start, boost::is_any_of(":"));
87  if (temp.size() != 2)
88  throw ModuleException("AuthFail schedule building error.");
89  int start_hour = std::stoi(temp[0]);
90  int start_min = std::stoi(temp[1]);
91 
92  temp.clear();
93  boost::split(temp, end, boost::is_any_of(":"));
94  if (temp.size() != 2)
95  throw ModuleException("AuthFail schedule building error.");
96  int end_hour = std::stoi(temp[0]);
97  int end_min = std::stoi(temp[1]);
98 
99  Tools::SingleTimeFrame tf(week_day_to_int(sched_data.first), start_hour,
100  start_min, end_hour, end_min);
101  sched.add_timeframe(tf);
102  }
103  if (schedules_.count(schedule_name))
104  INFO("A schedule with name " << schedule_name
105  << " already exists. It will be overridden.");
106 
107  schedules_[schedule_name] = std::make_shared<Schedule>(sched);
108  return true;
109 }
110 
111 const std::map<std::string, ISchedulePtr> &XmlScheduleLoader::schedules() const
112 {
113  return schedules_;
114 }
ERROR
@ ERROR
Definition: log.hpp:32
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
INFO
@ INFO
Definition: log.hpp:34
ModuleException
Definition: moduleexception.hpp:33
Leosac::Tools::XmlScheduleLoader::load
bool load(const boost::property_tree::ptree &t)
Load all schedules from a tree and stores them in the map.
Definition: XmlScheduleLoader.cpp:49
Leosac::Tools::XmlScheduleLoader::extract_one
bool extract_one(const boost::property_tree::ptree &node)
Adds a new schedule to the map.
Definition: XmlScheduleLoader.cpp:71
XmlScheduleLoader.hpp
Leosac::Tools::XmlScheduleLoader::schedules_
std::map< std::string, ISchedulePtr > schedules_
Definition: XmlScheduleLoader.hpp:60
configexception.hpp
Leosac::Tools::XmlScheduleLoader::schedules
const std::map< std::string, ISchedulePtr > & schedules() const
Access the map of stored schedules.
Definition: XmlScheduleLoader.cpp:111
log.hpp
Leosac::Tools::SingleTimeFrame
This struct abstracts what we call a single time frame.
Definition: SingleTimeFrame.hpp:38
Leosac::Tools::Schedule
A schedule is simply a list of time frame (SingleTimeFrame) with a name.
Definition: Schedule.hpp:41
Schedule.hpp
Leosac::Tools::XmlScheduleLoader::week_day_to_int
static int week_day_to_int(const std::string &day)
Helper function.
Definition: XmlScheduleLoader.cpp:29
Leosac::Tools
Definition: DatabaseLogSink.hpp:27