Leosac  0.8.0
Open Source Access Control
ZoneEvent.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 "core/audit/ZoneEvent.hpp"
21 #include "LeosacFwd.hpp"
22 #include "core/audit/ZoneEvent_odb.h"
23 #include "core/auth/Zone.hpp"
24 #include "core/auth/Zone_odb.h"
25 #include "tools/JSONUtils.hpp"
27 #include "tools/log.hpp"
28 
29 using namespace Leosac;
30 using namespace Leosac::Audit;
31 
33  : target_zone_id_(0)
34 {
35 }
36 
37 std::shared_ptr<ZoneEvent> ZoneEvent::create(const DBPtr &database,
38  Auth::IZonePtr target_zone,
39  AuditEntryPtr parent)
40 {
41  ASSERT_LOG(database, "Database cannot be null.");
42  ASSERT_LOG(target_zone, "Target zone must be non null.");
43  ASSERT_LOG(target_zone->id(), "Target zone must be already persisted.");
44  ASSERT_LOG(parent, "Parent must be non null.");
45  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
46 
48 
49  Audit::ZoneEventPtr audit =
50  std::shared_ptr<Audit::ZoneEvent>(new Audit::ZoneEvent());
51  audit->database_ = database;
52  audit->target(target_zone);
53  database->persist(audit);
54 
55  audit->set_parent(parent);
56  database->update(audit);
57 
58  t.commit();
59  return audit;
60 }
61 
63 {
64  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
65  if (zone)
66  ASSERT_LOG(zone->id(), "Zone has no id.");
67  auto zone_odb = std::dynamic_pointer_cast<Auth::Zone>(zone);
68  ASSERT_LOG(zone_odb, "IZone is not of type Zone.");
69 
70  target_ = zone_odb;
71  target_zone_id_ = zone->id();
72 }
73 
74 void ZoneEvent::before(const std::string &repr)
75 {
76  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
77  before_ = repr;
78 }
79 
80 void ZoneEvent::after(const std::string &repr)
81 {
82  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
83  after_ = repr;
84 }
85 
87 {
88  if (target_.lock())
89  return target_.object_id();
90  return target_zone_id_;
91 }
92 
93 const std::string &ZoneEvent::before() const
94 {
95  return before_;
96 }
97 
98 const std::string &ZoneEvent::after() const
99 {
100  return after_;
101 }
102 
104 {
105  std::stringstream ss;
106 
108  ss << "Zone " << generate_target_description() << " has been created.";
110  ss << "Zone " << generate_target_description() << " has been edited.";
112  ss << "Zone " << generate_target_description() << " has been deleted.";
113 
114  return ss.str();
115 }
116 
118 {
119  Leosac::json desc;
120 
121  desc["id"] = target_id();
122  auto t = target_.load();
123  if (t)
124  desc["alias"] = t->alias();
125 
126  return desc.dump();
127 }
128 
129 std::shared_ptr<ZoneEvent> ZoneEvent::create_empty()
130 {
131  return std::shared_ptr<ZoneEvent>(new ZoneEvent());
132 }
Leosac::db::OptionalTransaction
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
Definition: OptionalTransaction.hpp:43
Leosac::db::OptionalTransaction::commit
void commit()
Commit the transaction, if there was no currently active transaction at the time of this object's cre...
Definition: OptionalTransaction.cpp:38
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::Auth::ZoneId
unsigned long ZoneId
Definition: AuthFwd.hpp:119
Leosac::Audit::ZoneEvent::ZoneEvent
ZoneEvent()
Definition: ZoneEvent.cpp:32
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
Leosac::Audit::EventType::DOOR_UPDATED
@ DOOR_UPDATED
ZoneEvent.hpp
Leosac::Audit::ZoneEvent::target_zone_id_
Auth::ZoneId target_zone_id_
Definition: ZoneEvent.hpp:71
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
LeosacFwd.hpp
Leosac::Audit::AuditEntry::database
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:144
Leosac::Audit::ZoneEvent::before
virtual const std::string & before() const override
Definition: ZoneEvent.cpp:93
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::ZoneEvent
Provides an implementation of IZoneEvent.
Definition: ZoneEvent.hpp:33
Leosac::Audit::ZoneEvent::target
virtual void target(Auth::IZonePtr zone) override
Set the zone that is targeted by the event.
Definition: ZoneEvent.cpp:62
Leosac::Audit::ZoneEvent::generate_description
virtual std::string generate_description() const override
Generate a description for this event.
Definition: ZoneEvent.cpp:103
Leosac::Audit
The Audit namespace provides classes and facilities to keep track of what's happening on the Leosac d...
Definition: AccessPointEvent.hpp:27
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
OptionalTransaction.hpp
Zone.hpp
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::ZoneEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: ZoneEvent.hpp:81
Leosac::Audit::ZoneEvent::generate_target_description
std::string generate_target_description() const
Generate a short description for the targeted zone.
Definition: ZoneEvent.cpp:117
Leosac::Audit::ZoneEvent::target_id
virtual Auth::ZoneId target_id() const override
Definition: ZoneEvent.cpp:86
JSONUtils.hpp
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::EventType::DOOR_DELETED
@ DOOR_DELETED
Leosac::Audit::ZoneEventPtr
std::shared_ptr< ZoneEvent > ZoneEventPtr
Definition: AuditFwd.hpp:117
log.hpp
Leosac::Audit::EventType::DOOR_CREATED
@ DOOR_CREATED
Leosac::Auth::IZonePtr
std::shared_ptr< IZone > IZonePtr
Definition: AuthFwd.hpp:116
Leosac::Audit::ZoneEvent::create_empty
static std::shared_ptr< ZoneEvent > create_empty()
Definition: ZoneEvent.cpp:129
Leosac::Audit::ZoneEvent::create
static std::shared_ptr< ZoneEvent > create(const DBPtr &database, Auth::IZonePtr target_zone, AuditEntryPtr parent)
Definition: ZoneEvent.cpp:37
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::ZoneEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: ZoneEvent.hpp:76
Leosac::Audit::ZoneEvent::after
virtual const std::string & after() const override
Definition: ZoneEvent.cpp:98
Leosac::Audit::ZoneEvent::target_
Auth::ZoneLWPtr target_
Definition: ZoneEvent.hpp:69