Leosac  0.8.0
Open Source Access Control
PolymorphicAuditSerializer.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 
22 #include "DoorEventSerializer.hpp"
23 #include "GroupEventSerializer.hpp"
24 #include "JSONService.hpp"
27 #include "WSAPICallSerializer.hpp"
28 #include "ZoneEventSerializer.hpp"
30 #include "core/SecurityContext.hpp"
33 #include "tools/AssertCast.hpp"
34 #include "tools/JSONUtils.hpp"
36 
37 namespace Leosac
38 {
39 namespace Audit
40 {
41 namespace Serializer
42 {
43 
45  const SecurityContext &sc)
46 {
47  HelperSerialize h(sc);
48  in.accept(h);
49  return h.result_;
50 }
51 
53 {
55  in.accept(h);
56  ASSERT_LOG(h.result_.find("type") != h.result_.end(),
57  "The serializer didn't set a type.");
58  ASSERT_LOG(h.result_.at("type").is_string(), "Type is not a string.");
59  return h.result_.at("type");
60 }
61 
63  : security_context_(sc)
64 {
65 }
66 
68 {
69  result_ = UserEventJSON::serialize(t, security_context_);
70 }
71 
73 {
74  result_ = WSAPICallJSON::serialize(t, security_context_);
75 }
76 
78 {
79  result_ = ScheduleEventJSON::serialize(t, security_context_);
80 }
81 
83 {
84  result_ = GroupEventJSON::serialize(t, security_context_);
85 }
86 
88 {
89  result_ = CredentialEventJSON::serialize(t, security_context_);
90 }
91 
93 {
94  result_ = DoorEventJSON::serialize(t, security_context_);
95 }
96 
99 {
100  result_ = UserGroupMembershipEventJSON::serialize(t, security_context_);
101 }
102 
104 {
105  result_ = UpdateEventJSON::serialize(t, security_context_);
106 }
107 
109 {
110  result_ = ZoneEventJSON::serialize(t, security_context_);
111 }
112 
114  const Tools::IVisitable &visitable)
115 {
116  // If we cannot visit a visitable it may mean 2 things:
117  // + It is an Audit object defined in a module. In that case we'll rely
118  // on dynamically registered serializer.
119  // + Its an other object, and in that case, we shouldn't be here (we will
120  // assert)
121  const auto &audit_entry = assert_cast<const Audit::IAuditEntry &>(visitable);
122  auto service =
124  ASSERT_LOG(service,
125  "Cannot retrieve Audit::Serializer::JSONService. Since this a "
126  "core service, something must be very wrong.");
127 
128  result_ = service->serialize(audit_entry, security_context_);
129 }
130 }
131 }
132 }
Leosac::SystemSecurityContext::instance
static SecurityContext & instance()
Definition: SecurityContext.cpp:64
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
GlobalRegistry.hpp
UserGroupMembershipEventSerializer.hpp
Leosac::Audit::Serializer::GroupEventJSON::serialize
static json serialize(const Audit::IGroupEvent &in, const SecurityContext &sc)
Definition: GroupEventSerializer.cpp:32
Leosac::Audit::IGroupEvent
Interface that describes an Audit object for group related event.
Definition: IGroupEvent.hpp:36
Leosac::Audit::IDoorEvent
Interface that describes an Audit object for door related event.
Definition: IDoorEvent.hpp:32
Leosac::Audit::Serializer::UpdateEventJSON::serialize
static json serialize(const Audit::IUpdateEvent &in, const SecurityContext &sc)
Definition: UpdateEventSerializer.cpp:33
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::Audit::ICredentialEvent
Audit interface to Credential related events.
Definition: ICredentialEvent.hpp:32
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Audit::Serializer::CredentialEventJSON::serialize
static json serialize(const Audit::ICredentialEvent &in, const SecurityContext &sc)
Definition: CredentialEventSerializer.cpp:32
Leosac::Audit::Serializer::ZoneEventJSON::serialize
static json serialize(const Audit::IZoneEvent &in, const SecurityContext &sc)
Definition: ZoneEventSerializer.cpp:32
Leosac::Audit::Serializer::PolymorphicAuditJSON::HelperSerialize::result_
json result_
Store the result here because we can't return from the visit() method.
Definition: PolymorphicAuditSerializer.hpp:100
Leosac::Audit::IUserEvent
Interface that describes an Audit object when a user-related event happens.
Definition: IUserEvent.hpp:37
Leosac::Tools::IVisitable::accept
virtual void accept(::Leosac::Tools::BaseVisitor &)=0
Accept a visitor that may mutate this.
Leosac::Audit::Serializer::UserEventJSON::serialize
static json serialize(const Audit::IUserEvent &in, const SecurityContext &sc)
Definition: UserEventSerializer.cpp:33
ZoneEventSerializer.hpp
PolymorphicAuditSerializer.hpp
ScheduleEventSerializer.hpp
SecurityContext.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Audit::Serializer::WSAPICallJSON::serialize
static json serialize(const Audit::IWSAPICall &in, const SecurityContext &sc)
Definition: WSAPICallSerializer.cpp:31
GetServiceRegistry.hpp
DoorEventSerializer.hpp
Leosac::Audit::Serializer::PolymorphicAuditJSON::HelperSerialize
Non static helper that can visit a core audit object.
Definition: PolymorphicAuditSerializer.hpp:67
Leosac::Audit::Serializer::PolymorphicAuditJSON::HelperSerialize::visit
virtual void visit(const Audit::IUserEvent &t) override
Definition: PolymorphicAuditSerializer.cpp:67
Leosac::Audit::Serializer::PolymorphicAuditJSON::HelperSerialize::HelperSerialize
HelperSerialize(const SecurityContext &sc)
Definition: PolymorphicAuditSerializer.cpp:62
JSONService.hpp
CredentialEventSerializer.hpp
JSONUtils.hpp
Leosac::Tools::IVisitable
Base class to make an object visitable.
Definition: IVisitable.hpp:43
Leosac::Audit::IScheduleEvent
Audit interface to Schedule related events.
Definition: IScheduleEvent.hpp:32
Leosac::Audit::Serializer::PolymorphicAuditJSON::type_name
static std::string type_name(const Audit::IAuditEntry &in)
Returns the "type-name" of the audit entry.
Definition: PolymorphicAuditSerializer.cpp:52
Leosac::Audit::IAuditEntry
Base interface to Audit object.
Definition: IAuditEntry.hpp:47
Leosac::Audit::Serializer::UserGroupMembershipEventJSON::serialize
static json serialize(const Audit::IUserGroupMembershipEvent &in, const SecurityContext &sc)
Definition: UserGroupMembershipEventSerializer.cpp:32
Leosac::Audit::Serializer::PolymorphicAuditJSON::HelperSerialize::cannot_visit
virtual void cannot_visit(const Tools::IVisitable &visitable) override
Called when no "hardcoded" audit type match, this method will delegate to runtime-registered serializ...
Definition: PolymorphicAuditSerializer.cpp:113
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::Audit::IZoneEvent
Interface that describes an Audit object for zone related event.
Definition: IZoneEvent.hpp:32
GroupEventSerializer.hpp
Leosac::Audit::IUpdateEvent
Interface for update-related event.
Definition: IUpdateEvent.hpp:32
Leosac::Audit::Serializer::PolymorphicAuditJSON::serialize
static json serialize(const Audit::IAuditEntry &in, const SecurityContext &sc)
Perform deep serialization of the AuditEntry in.
Definition: PolymorphicAuditSerializer.cpp:44
Leosac::Audit::IUserGroupMembershipEvent
Interface to audit object that take care of tracking user/group membership change.
Definition: IUserGroupMembershipEvent.hpp:33
UpdateEventSerializer.hpp
Leosac::Audit::Serializer::ScheduleEventJSON::serialize
static json serialize(const Audit::IScheduleEvent &in, const SecurityContext &sc)
Definition: ScheduleEventSerializer.cpp:32
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
UserEventSerializer.hpp
AssertCast.hpp
WSAPICallSerializer.hpp
Leosac::Audit::Serializer::JSONService
This service manages runtime registered serializer that target AuditEntry object.
Definition: JSONService.hpp:42
Leosac::Audit::IWSAPICall
An audit entry dedicated to tracing API call.
Definition: IWSAPICall.hpp:39
Leosac::Audit::Serializer::DoorEventJSON::serialize
static json serialize(const Audit::IDoorEvent &in, const SecurityContext &sc)
Definition: DoorEventSerializer.cpp:32