Leosac  0.8.0
Open Source Access Control
Zone.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 "core/auth/Zone_odb.h"
22 #include "tools/log.hpp"
23 
24 namespace Leosac
25 {
26 namespace Auth
27 {
29  : id_(0)
30  , type_(Type::LOGICAL)
31  , version_(0)
32 {
33 }
34 
36 {
37  return id_;
38 }
39 
40 std::string Zone::alias() const
41 {
42  return alias_;
43 }
44 
45 std::string Zone::description() const
46 {
47  return description_;
48 }
49 
50 void Zone::alias(const std::string &alias)
51 {
52  alias_ = alias;
53 }
54 
55 void Zone::description(const std::string &desc)
56 {
57  description_ = desc;
58 }
59 
61 {
62  return type_;
63 }
64 
65 void Zone::type(IZone::Type t)
66 {
67  type_ = t;
68 }
69 
70 std::vector<ZoneLPtr> Zone::children() const
71 {
72  return children_;
73 }
74 
75 std::vector<DoorLPtr> Zone::doors() const
76 {
77  return doors_;
78 }
79 
81 {
82  children_.clear();
83 }
84 
86 {
87  doors_.clear();
88 }
89 
91 {
92  doors_.push_back(door);
93 }
94 
96 {
97  children_.push_back(zone);
98 }
99 
100 void Zone::validation_callback(odb::callback_event e, odb::database &) const
101 {
102  if (e == odb::callback_event::post_update ||
103  e == odb::callback_event::post_persist)
104  {
105  std::set<ZoneId> ids;
106  ZoneValidator::validate(*this, ids);
107  }
108 }
109 
110 static void insert_enforce_unique(std::set<ZoneId> &zone_ids, ZoneId id)
111 {
112  auto inserted = zone_ids.insert(id);
113  if (!inserted.second)
114  {
115  // Already was inserted. We have a cycle.
116  throw ModelException("",
117  "There cannot be cycle in parent/child relationship.");
118  }
119 }
120 
121 void ZoneValidator::validate(const Zone &z, std::set<ZoneId> &zone_ids)
122 {
123  unsigned int physical_parent_count = 0;
124 
125  // Add current zone id to iterated zones. This will throw if zone
126  // was already inserted.
127  insert_enforce_unique(zone_ids, z.id());
128  validate_type(z.type());
129 
130  for (auto &lazy_parent : z.parents_)
131  {
132  auto parent(lazy_parent.load());
133  ASSERT_LOG(parent, "Failed to load object.");
134  if (parent->type() == IZone::Type::PHYSICAL)
135  {
136  ++physical_parent_count;
137  }
138  }
139 
140  // Physical zone can have at most one physical parent.
141  if (z.type() == IZone::Type::PHYSICAL && physical_parent_count > 1)
142  {
143  throw ModelException(
144  "", "A physical zone cannot have more than one physical parent.");
145  }
146  // Logical zone cannot have physical parent.
147  else if (z.type() == IZone::Type::LOGICAL && physical_parent_count != 0)
148  {
149  throw ModelException("",
150  "A logical zone cannot have physical zone as a parent");
151  }
152 
153  // Recursively validate children.
154  for (auto &lazy_children : z.children_)
155  {
156  auto child(lazy_children.load());
157  ASSERT_LOG(child, "Failed to load object");
158  ZoneValidator::validate(*child, zone_ids);
159  }
160 }
161 
163 {
164  switch (value)
165  {
168  return;
169 
170  default:
171  throw ModelException("data/attributes/type", "Invalid zone type.");
172  }
173 }
174 }
175 }
Leosac::Auth::Zone::id_
ZoneId id_
Definition: Zone.hpp:101
Leosac::Auth::Zone::type
virtual Type type() const override
Definition: Zone.cpp:60
Leosac::Auth::Zone::add_door
virtual void add_door(DoorLPtr door) override
Definition: Zone.cpp:90
Leosac::Auth::Zone::children
virtual std::vector< ZoneLPtr > children() const override
Retrieve the children zones.
Definition: Zone.cpp:70
Leosac::Auth::ZoneId
unsigned long ZoneId
Definition: AuthFwd.hpp:119
Leosac::Auth::Zone::alias
virtual std::string alias() const override
Definition: Zone.cpp:40
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Auth::Zone::clear_doors
virtual void clear_doors() override
Definition: Zone.cpp:85
Leosac::Auth::Zone::doors
virtual std::vector< DoorLPtr > doors() const override
Retrieve the doors associated with the zones.
Definition: Zone.cpp:75
Leosac::Auth::Zone::alias_
std::string alias_
Definition: Zone.hpp:103
Leosac::Auth::Zone::children_
std::vector< ZoneLPtr > children_
Definition: Zone.hpp:108
Leosac::Auth::IZone::Type::LOGICAL
@ LOGICAL
Leosac::Auth::Zone::description_
std::string description_
Definition: Zone.hpp:104
Leosac::Auth::Zone::parents_
std::vector< ZoneLWPtr > parents_
Definition: Zone.hpp:114
Leosac::Auth::Zone::description
virtual std::string description() const override
Definition: Zone.cpp:45
Leosac::Auth::Zone::Zone
Zone()
Definition: Zone.cpp:28
Leosac::Auth::Zone
A Zone is a container for doors and other zone.
Definition: Zone.hpp:60
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Auth::ZoneValidator::validate_type
static void validate_type(IZone::Type value)
Validate that the integer value of the type is a correct type for a zone.
Definition: Zone.cpp:162
Leosac::Auth::Zone::clear_children
virtual void clear_children() override
Definition: Zone.cpp:80
Leosac::Auth::Zone::type_
Type type_
Definition: Zone.hpp:105
ModelException.hpp
Leosac::Auth::Zone::id
virtual ZoneId id() const override
Definition: Zone.cpp:35
Leosac::Auth::Zone::add_child
virtual void add_child(ZoneLPtr zone) override
Definition: Zone.cpp:95
Leosac::Auth::Zone::validation_callback
void validation_callback(odb::callback_event e, odb::database &) const
Callback function called by ODB before/after database operation against a Zone object.
Definition: Zone.cpp:100
ModelException
An exception class for general API error.
Definition: ModelException.hpp:33
Leosac::Auth::IZone::Type::PHYSICAL
@ PHYSICAL
Leosac::Auth::ZoneLPtr
odb::lazy_shared_ptr< Zone > ZoneLPtr
Definition: AuthFwd.hpp:123
log.hpp
Leosac::Auth::IZone::Type
Type
Zone's type.
Definition: IZone.hpp:53
Leosac::Auth::ZoneValidator::validate
static void validate(const Zone &z, std::set< ZoneId > &zone_ids)
Perform validation of the zone.
Definition: Zone.cpp:121
Leosac::Auth::DoorLPtr
odb::lazy_shared_ptr< Door > DoorLPtr
Definition: AuthFwd.hpp:111
Leosac::Auth::Zone::doors_
std::vector< DoorLPtr > doors_
Definition: Zone.hpp:111