Leosac  0.7.0
OpenSourceAccessControl
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 }
std::vector< ZoneLPtr > children_
Definition: Zone.hpp:108
virtual std::string alias() const override
Definition: Zone.cpp:40
An exception class for general API error.
This is the header file for a generated source file, GitSHA1.cpp.
odb::lazy_shared_ptr< Door > DoorLPtr
Definition: AuthFwd.hpp:111
virtual void clear_doors() override
Definition: Zone.cpp:85
virtual void add_child(ZoneLPtr zone) override
Definition: Zone.cpp:95
virtual ZoneId id() const override
Definition: Zone.cpp:35
virtual std::vector< ZoneLPtr > children() const override
Retrieve the children zones.
Definition: Zone.cpp:70
std::vector< DoorLPtr > doors_
Definition: Zone.hpp:111
static void validate(const Zone &z, std::set< ZoneId > &zone_ids)
Perform validation of the zone.
Definition: Zone.cpp:121
std::string description_
Definition: Zone.hpp:104
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
A Zone is a container for doors and other zone.
Definition: Zone.hpp:60
virtual void clear_children() override
Definition: Zone.cpp:80
std::vector< ZoneLWPtr > parents_
Definition: Zone.hpp:114
odb::lazy_shared_ptr< Zone > ZoneLPtr
Definition: AuthFwd.hpp:123
virtual std::vector< DoorLPtr > doors() const override
Retrieve the doors associated with the zones.
Definition: Zone.cpp:75
std::string alias_
Definition: Zone.hpp:103
virtual std::string description() const override
Definition: Zone.cpp:45
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
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
Type
Zone&#39;s type.
Definition: IZone.hpp:53
virtual void add_door(DoorLPtr door) override
Definition: Zone.cpp:90
unsigned long ZoneId
Definition: AuthFwd.hpp:119
virtual Type type() const override
Definition: Zone.cpp:60