Leosac  0.8.0
Open Source Access Control
ZoneSerializer.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 "ZoneSerializer.hpp"
22 #include "core/auth/Door_odb.h"
23 #include "core/auth/Zone_odb.h"
24 #include "tools/JSONUtils.hpp"
26 
27 using namespace Leosac;
28 using namespace Leosac::Auth;
29 
31 {
32  json serialized = {{"id", zone.id()},
33  {"type", "zone"},
34  {"attributes",
35  {{"alias", zone.alias()},
36  {"description", zone.description()},
37  {"type", static_cast<int>(zone.type())}}}};
38 
39  json children = json::array();
40  for (const Auth::ZoneLPtr &lazy_zone : zone.children())
41  {
42  json child_zone_info = {{"id", lazy_zone.object_id()}, {"type", "zone"}};
43  children.push_back(child_zone_info);
44  }
45 
46  json doors = json::array();
47  for (const Auth::DoorLPtr &lazy_door : zone.doors())
48  {
49  json door_info = {{"id", lazy_door.object_id()}, {"type", "door"}};
50  doors.push_back(door_info);
51  }
52 
53  serialized["relationships"] = {{"children", {{"data", children}}},
54  {"doors", {{"data", doors}}}};
55  return serialized;
56 }
57 
59  const SecurityContext &)
60 {
61  using namespace Leosac::JSONUtil;
62  auto db = get_service_registry().get_service<DBService>()->db();
63 
64  out.alias(extract_with_default(in, "alias", out.alias()));
65  out.description(extract_with_default(in, "description", out.description()));
67 
68  auto doors_ids = in.at("doors");
69  out.clear_doors();
70  for (const auto &door_id : doors_ids)
71  {
72  auto door = Auth::DoorLPtr(*db, door_id.get<Auth::DoorId>());
73  out.add_door(door);
74  }
75 
76  auto children_ids = in.at("children");
77  out.clear_children();
78  for (const auto &child_id : children_ids)
79  {
80  auto child = Auth::ZoneLPtr(*db, child_id.get<Auth::ZoneId>());
81  out.add_child(child);
82  }
83 }
84 
86  const SecurityContext &sc)
87 {
88  return ZoneJSONSerializer::serialize(in, sc).dump(4);
89 }
90 
91 void ZoneJSONStringSerializer::unserialize(Auth::IZone &out, const std::string &in,
92  const SecurityContext &sc)
93 {
94  auto tmp = json::parse(in);
95  ZoneJSONSerializer::unserialize(out, tmp, sc);
96 }
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::JSONUtil
Add a few useful extraction functions.
Definition: JSONUtils.cpp:27
Leosac::Auth::ZoneId
unsigned long ZoneId
Definition: AuthFwd.hpp:119
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
Leosac::Auth::IZone::clear_children
virtual void clear_children()=0
Leosac::ZoneJSONSerializer::serialize
static json serialize(const Auth::IZone &Zone, const SecurityContext &sc)
Definition: ZoneSerializer.cpp:30
Leosac::Auth::IZone::children
virtual std::vector< ZoneLPtr > children() const =0
Retrieve the children zones.
Leosac::Auth::IZone::description
virtual std::string description() const =0
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
ServiceRegistry.hpp
Leosac::Auth::IZone
The interface for zones.
Definition: IZone.hpp:47
Leosac::Auth::IZone::doors
virtual std::vector< DoorLPtr > doors() const =0
Retrieve the doors associated with the zones.
Leosac::Auth::IZone::Type::LOGICAL
@ LOGICAL
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::ZoneJSONStringSerializer::serialize
static std::string serialize(const Auth::IZone &in, const SecurityContext &sc)
Definition: ZoneSerializer.cpp:85
GetServiceRegistry.hpp
Leosac::Auth::DoorId
unsigned long DoorId
Definition: AuthFwd.hpp:107
JSONUtils.hpp
Leosac::ZoneJSONStringSerializer::unserialize
static void unserialize(Auth::IZone &out, const std::string &in, const SecurityContext &sc)
Definition: ZoneSerializer.cpp:91
Leosac::Auth::IZone::alias
virtual std::string alias() const =0
Leosac::Auth::IZone::clear_doors
virtual void clear_doors()=0
Leosac::ZoneJSONSerializer::unserialize
static void unserialize(Auth::IZone &out, const json &in, const SecurityContext &sc)
Definition: ZoneSerializer.cpp:58
Leosac::Auth::ZoneLPtr
odb::lazy_shared_ptr< Zone > ZoneLPtr
Definition: AuthFwd.hpp:123
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
Leosac::Auth::IZone::type
virtual Type type() const =0
Leosac::Auth::IZone::add_child
virtual void add_child(ZoneLPtr zone)=0
Leosac::Auth::IZone::id
virtual ZoneId id() const =0
Leosac::Auth::DoorLPtr
odb::lazy_shared_ptr< Door > DoorLPtr
Definition: AuthFwd.hpp:111
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
ZoneSerializer.hpp
Leosac::Auth::IZone::add_door
virtual void add_door(DoorLPtr door)=0