Leosac  0.8.0
Open Source Access Control
PolymorphicCredentialSerializer.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 
21 #include "PinCodeSerializer.hpp"
22 #include "RFIDCardSerializer.hpp"
23 #include "core/SecurityContext.hpp"
25 #include "tools/JSONUtils.hpp"
26 #include "tools/log.hpp"
27 
28 using namespace Leosac;
29 using namespace Leosac::Cred;
30 
31 // JSON
32 
34  const SecurityContext &sc)
35 {
36  HelperSerialize h(sc);
37  in.accept(h);
38  return h.result_;
39 }
40 
42  const json &in,
43  const SecurityContext &sc)
44 {
45  HelperUnserialize h(sc, in);
46  out.accept(h);
47 }
48 
49 std::string
51 {
53  in.accept(h);
54  ASSERT_LOG(h.result_.find("type") != h.result_.end(),
55  "The serializer didn't set a type.");
56  ASSERT_LOG(h.result_.at("type").is_string(), "Type is not a string.");
57  return h.result_.at("type");
58 }
59 
60 
61 // HELPERS
62 
64  const SecurityContext &sc, const json &payload)
65  : security_context_(sc)
66  , payload_(payload)
67 {
68 }
69 
71  Cred::IRFIDCard &t)
72 {
73  RFIDCardJSONSerializer::unserialize(t, payload_, security_context_);
74 }
75 
77 {
78  PinCodeJSONSerializer::unserialize(t, payload_, security_context_);
79 }
80 
82  const SecurityContext &sc)
83  : security_context_(sc)
84 {
85 }
86 
88 {
89  result_ = RFIDCardJSONSerializer::serialize(t, security_context_);
90 }
91 
93 {
94  result_ = PinCodeJSONSerializer::serialize(t, security_context_);
95 }
96 
97 
98 // JSONString
99 
100 
101 std::string
103  const SecurityContext &sc)
104 {
105  return PolymorphicCredentialJSONSerializer::serialize(in, sc).dump(4);
106 }
Leosac::PinCodeJSONSerializer::serialize
static json serialize(const Cred::IPinCode &in, const SecurityContext &sc)
Definition: PinCodeSerializer.cpp:30
Leosac::SystemSecurityContext::instance
static SecurityContext & instance()
Definition: SecurityContext.cpp:64
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::RFIDCardJSONSerializer::unserialize
static void unserialize(Cred::IRFIDCard &out, const json &in, const SecurityContext &sc)
Definition: RFIDCardSerializer.cpp:45
PolymorphicCredentialSerializer.hpp
Leosac::PolymorphicCredentialJSONSerializer::serialize
static json serialize(const Cred::ICredential &in, const SecurityContext &sc)
Definition: PolymorphicCredentialSerializer.cpp:33
Leosac::PolymorphicCredentialJSONStringSerializer::serialize
static std::string serialize(const Cred::ICredential &in, const SecurityContext &sc)
Definition: PolymorphicCredentialSerializer.cpp:102
Leosac::PolymorphicCredentialJSONSerializer::unserialize
static void unserialize(Cred::ICredential &out, const json &in, const SecurityContext &sc)
Definition: PolymorphicCredentialSerializer.cpp:41
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
ICredential.hpp
Leosac::Tools::IVisitable::accept
virtual void accept(::Leosac::Tools::BaseVisitor &)=0
Accept a visitor that may mutate this.
Leosac::PolymorphicCredentialJSONSerializer::HelperSerialize::visit
void visit(const Cred::IRFIDCard &t) override
Definition: PolymorphicCredentialSerializer.cpp:87
Leosac::PolymorphicCredentialJSONSerializer::HelperSerialize::result_
json result_
Store the result here because we can't return from the visit() method.
Definition: PolymorphicCredentialSerializer.hpp:71
Leosac::PolymorphicCredentialJSONSerializer::HelperSerialize
Non static helper that can visit credential object.
Definition: PolymorphicCredentialSerializer.hpp:59
Leosac::PolymorphicCredentialJSONSerializer::HelperUnserialize::HelperUnserialize
HelperUnserialize(const SecurityContext &sc, const json &payload)
Definition: PolymorphicCredentialSerializer.cpp:63
SecurityContext.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
PinCodeSerializer.hpp
Leosac::Cred::ICredential
Base interface for credential objects.
Definition: ICredential.hpp:35
JSONUtils.hpp
RFIDCardSerializer.hpp
Leosac::PolymorphicCredentialJSONSerializer::type_name
static std::string type_name(const Cred::ICredential &in)
Returns the "type-name" of the credential.
Definition: PolymorphicCredentialSerializer.cpp:50
Leosac::RFIDCardJSONSerializer::serialize
static json serialize(const Cred::IRFIDCard &in, const SecurityContext &sc)
Definition: RFIDCardSerializer.cpp:30
Leosac::PolymorphicCredentialJSONSerializer::HelperUnserialize::visit
void visit(Cred::IRFIDCard &t) override
Definition: PolymorphicCredentialSerializer.cpp:70
Leosac::Cred
Definition: Credential.hpp:31
log.hpp
Leosac::PolymorphicCredentialJSONSerializer::HelperUnserialize
Definition: PolymorphicCredentialSerializer.hpp:79
Leosac::PolymorphicCredentialJSONSerializer::HelperSerialize::HelperSerialize
HelperSerialize(const SecurityContext &sc)
Definition: PolymorphicCredentialSerializer.cpp:81
Leosac::Cred::IPinCode
Interface for PinCode credentials.
Definition: IPinCode.hpp:32
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::Cred::IRFIDCard
Interface for RFIDCard credential.
Definition: IRFIDCard.hpp:32
Leosac::PinCodeJSONSerializer::unserialize
static void unserialize(Cred::IPinCode &out, const json &in, const SecurityContext &sc)
Definition: PinCodeSerializer.cpp:43