Leosac  0.8.0
Open Source Access Control
ProfileMerger.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 
20 #include "ProfileMerger.hpp"
21 #include "SimpleAccessProfile.hpp"
22 #include <cassert>
23 
24 using namespace Leosac::Auth;
25 
27 
28 IAccessProfilePtr ProfileMerger::merge(std::shared_ptr<const IAccessProfile> p1,
29  std::shared_ptr<const IAccessProfile> p2)
30 {
31  assert(p1 && p2);
32  const SimpleAccessProfile *t_p1 =
33  dynamic_cast<const SimpleAccessProfile *>(p1.get());
34  const SimpleAccessProfile *t_p2 =
35  dynamic_cast<const SimpleAccessProfile *>(p2.get());
36  assert(t_p1 && t_p2);
37 
39 
40  // merge strategy: simply add all schedule from both profile.
41 
42  for (const auto &schedule : t_p1->defaultSchedules())
43  {
44  result->addAccessSchedule(nullptr, schedule);
45  }
46  for (const auto &schedule : t_p2->defaultSchedules())
47  {
48  result->addAccessSchedule(nullptr, schedule);
49  }
50 
51  std::string door_name;
52  std::vector<Tools::IScheduleCPtr> schedules;
53  for (const auto &name_sched_pair : t_p1->schedules())
54  {
55  std::tie(door_name, schedules) = name_sched_pair;
56  // hack
57  AuthTargetPtr target(new AuthTarget(door_name));
58  for (const Tools::IScheduleCPtr &sched : schedules)
59  result->addAccessSchedule(target, sched);
60  }
61  for (const auto &name_sched_pair : t_p2->schedules())
62  {
63  std::tie(door_name, schedules) = name_sched_pair;
64  // hack
65  AuthTargetPtr target(new AuthTarget(door_name));
66  for (const Tools::IScheduleCPtr &sched : schedules)
67  result->addAccessSchedule(target, sched);
68  }
69 
70  return result;
71 }
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::Auth::AuthTarget
Represent an object that we are authorizing against (a door).
Definition: AuthTarget.hpp:37
SingleTimeFrame
Leosac::Tools::SingleTimeFrame SingleTimeFrame
Definition: ProfileMerger.cpp:26
SimpleAccessProfile.hpp
Leosac::Auth::IAccessProfilePtr
std::shared_ptr< IAccessProfile > IAccessProfilePtr
Definition: AuthFwd.hpp:88
Leosac::Auth::SimpleAccessProfile::defaultSchedules
virtual const std::vector< Tools::IScheduleCPtr > & defaultSchedules() const
Definition: SimpleAccessProfile.cpp:71
Leosac::Auth::AuthTargetPtr
std::shared_ptr< AuthTarget > AuthTargetPtr
Definition: AuthFwd.hpp:93
Leosac::Tools::IScheduleCPtr
std::shared_ptr< const ISchedule > IScheduleCPtr
Definition: ToolsFwd.hpp:38
Leosac::Auth::SimpleAccessProfile
Concrete implementation of a simple access control class.
Definition: SimpleAccessProfile.hpp:39
Leosac::Auth::SimpleAccessProfile::schedules
virtual const std::map< std::string, std::vector< Tools::IScheduleCPtr > > & schedules() const
Returns the map of schedule for each target (except the default target)
Definition: SimpleAccessProfile.cpp:65
Leosac::Auth::SimpleAccessProfilePtr
std::shared_ptr< SimpleAccessProfile > SimpleAccessProfilePtr
Definition: AuthFwd.hpp:44
Leosac::Tools::SingleTimeFrame
This struct abstracts what we call a single time frame.
Definition: SingleTimeFrame.hpp:38
ProfileMerger.hpp
Leosac::Auth::ProfileMerger::merge
virtual IAccessProfilePtr merge(std::shared_ptr< const IAccessProfile > p1, std::shared_ptr< const IAccessProfile > p2)
Build a new Profile object by merging two profiles.
Definition: ProfileMerger.cpp:28