Leosac  0.8.0
Open Source Access Control
ExternalMessageSerializer.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2022 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 
23 #include "tools/JSONUtils.hpp"
24 #include "tools/log.hpp"
25 
26 namespace Leosac
27 {
28 namespace Hardware
29 {
31  const SecurityContext &sc)
32 {
33  json serialized = DeviceSerializer::serialize(in, sc);
34  ASSERT_LOG(serialized.at("type").is_string(),
35  "Base device serialization did something unexpected.");
36 
37  // Override object type
38  serialized["type"] = "external-message";
39 
40  // Add External Message specific attributes
41  serialized["attributes"]["subject"] = in.subject();
42  serialized["attributes"]["direction"] = in.direction();
43  serialized["attributes"]["virtualtype"] = in.virtualtype();
44  serialized["attributes"]["payload"] = in.payload();
45 
46  return serialized;
47 }
48 
50  const SecurityContext &sc)
51 {
52  using namespace JSONUtil;
53  DeviceSerializer::unserialize(out, in, sc);
54 
55  out.subject(extract_with_default(in, "subject", out.subject()));
56  out.direction(extract_with_default(in, "direction", out.direction()));
57  out.virtualtype(extract_with_default(in, "virtualtype", out.virtualtype()));
58  out.payload(extract_with_default(in, "payload", out.payload()));
59 }
60 }
61 }
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
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
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Hardware::ExternalMessage::payload
std::string payload() const
Definition: ExternalMessage.cpp:65
Leosac::Hardware::ExternalMessage::subject
std::string subject() const
Definition: ExternalMessage.cpp:35
Leosac::Hardware::ExternalMessage::direction
Direction direction() const
Definition: ExternalMessage.cpp:45
Leosac::Hardware::ExternalMessageSerializer::serialize
static json serialize(const Hardware::ExternalMessage &in, const SecurityContext &sc)
Definition: ExternalMessageSerializer.cpp:30
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
ExternalMessageSerializer.hpp
Leosac::Hardware::ExternalMessageSerializer::unserialize
static void unserialize(Hardware::ExternalMessage &out, const json &in, const SecurityContext &sc)
Definition: ExternalMessageSerializer.cpp:49
JSONUtils.hpp
Leosac::Hardware::ExternalMessage
Abstraction of an External Messaging device.
Definition: ExternalMessage.hpp:41
log.hpp
Leosac::Hardware::ExternalMessage::virtualtype
DeviceClass virtualtype() const
Definition: ExternalMessage.cpp:55
Leosac::Hardware::DeviceSerializer::serialize
static json serialize(const Hardware::Device &in, const SecurityContext &sc)
Definition: DeviceSerializer.cpp:28
Leosac::Hardware::DeviceSerializer::unserialize
static void unserialize(Hardware::Device &out, const json &in, const SecurityContext &sc)
Definition: DeviceSerializer.cpp:40
DeviceSerializer.hpp
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40