Leosac  0.8.0
Open Source Access Control
DoorSerializer.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 "DoorSerializer.hpp"
22 #include "core/auth/AccessPoint_odb.h"
23 #include "core/auth/Door.hpp"
25 #include "tools/JSONUtils.hpp"
27 
28 using namespace Leosac;
29 using namespace Leosac::Auth;
30 
32 {
33  json serialized = {
34  {"id", door.id()},
35  {"type", "door"},
36  {"attributes",
37  {
38  {"alias", door.alias()}, {"description", door.description()},
39  }}};
40 
41  if (door.access_point())
42  {
43  serialized["relationships"]["access-point"] = {
44  {"data", {{"id", door.access_point()->id()}, {"type", "access-point"}}}};
45  }
46  return serialized;
47 }
48 
50  const SecurityContext &)
51 {
52  using namespace Leosac::JSONUtil;
53 
54  out.alias(extract_with_default(in, "alias", out.alias()));
55  out.description(extract_with_default(in, "description", out.description()));
56 
57  // Update the linked access point.
58  Auth::AccessPointId current_ap_id = 0;
59  if (out.access_point())
60  current_ap_id = out.access_point()->id();
61 
62  Auth::AccessPointId new_ap_id =
63  extract_with_default(in, "access_point_id", current_ap_id);
64  if (new_ap_id != current_ap_id)
65  {
66  if (new_ap_id)
67  {
69  Auth::AccessPointLPtr new_access_point(*dbptr, new_ap_id);
70  out.access_point(new_access_point.load());
71  }
72  else
73  {
74  out.access_point(nullptr);
75  }
76  }
77 }
78 
80  const SecurityContext &sc)
81 {
82  return DoorJSONSerializer::serialize(in, sc).dump(4);
83 }
84 
85 void DoorJSONStringSerializer::unserialize(Auth::IDoor &out, const std::string &in,
86  const SecurityContext &sc)
87 {
88  auto tmp = json::parse(in);
89  DoorJSONSerializer::unserialize(out, tmp, sc);
90 }
Leosac::Auth
Holds classes relevant to the Authentication and Authorization subsystem.
Definition: AccessPoint.hpp:27
DoorSerializer.hpp
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::JSONUtil
Add a few useful extraction functions.
Definition: JSONUtils.cpp:27
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
IAccessPoint.hpp
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::DoorJSONSerializer::serialize
static json serialize(const Auth::IDoor &door, const SecurityContext &sc)
Definition: DoorSerializer.cpp:31
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::DoorJSONSerializer::unserialize
static void unserialize(Auth::IDoor &out, const json &in, const SecurityContext &sc)
Definition: DoorSerializer.cpp:49
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
Leosac::Auth::IDoor::description
virtual std::string description() const =0
Door.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
GetServiceRegistry.hpp
Leosac::DoorJSONStringSerializer::serialize
static std::string serialize(const Auth::IDoor &in, const SecurityContext &sc)
Definition: DoorSerializer.cpp:79
JSONUtils.hpp
ThreadLocalRegistry.hpp
Leosac::Auth::IDoor::alias
virtual std::string alias() const =0
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::IDoor::id
virtual DoorId id() const =0
Leosac::Auth::IDoor::access_point
virtual IAccessPointPtr access_point() const =0
Leosac::Auth::IDoor
A door.
Definition: IDoor.hpp:38
Leosac::Auth::AccessPointLPtr
odb::lazy_shared_ptr< AccessPoint > AccessPointLPtr
Definition: AuthFwd.hpp:132
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::DoorJSONStringSerializer::unserialize
static void unserialize(Auth::IDoor &out, const std::string &in, const SecurityContext &sc)
Definition: DoorSerializer.cpp:85
Leosac::Auth::AccessPointId
unsigned long AccessPointId
Definition: AuthFwd.hpp:128