Leosac  0.8.0
Open Source Access Control
DoorEvent.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 "DoorEvent.hpp"
21 #include "LeosacFwd.hpp"
22 #include "core/audit/DoorEvent_odb.h"
23 #include "core/auth/Door.hpp"
24 #include "tools/JSONUtils.hpp"
26 #include "tools/log.hpp"
27 
28 using namespace Leosac;
29 using namespace Leosac::Audit;
30 
32  : target_door_id_(0)
33  , access_point_id_before_(0)
34  , access_point_id_after_(0)
35 {
36 }
37 
38 std::shared_ptr<DoorEvent> DoorEvent::create(const DBPtr &database,
39  Auth::IDoorPtr target_door,
40  AuditEntryPtr parent)
41 {
42  ASSERT_LOG(database, "Database cannot be null.");
43  ASSERT_LOG(target_door, "Target door must be non null.");
44  ASSERT_LOG(target_door->id(), "Target door must be already persisted.");
45  ASSERT_LOG(parent, "Parent must be non null.");
46  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
47 
49 
50  Audit::DoorEventPtr audit =
51  std::shared_ptr<Audit::DoorEvent>(new Audit::DoorEvent());
52  audit->database_ = database;
53  audit->target(target_door);
54  database->persist(audit);
55 
56  audit->set_parent(parent);
57  database->update(audit);
58 
59  t.commit();
60  return audit;
61 }
62 
64 {
65  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
66  if (door)
67  ASSERT_LOG(door->id(), "Door has no id.");
68  auto door_odb = std::dynamic_pointer_cast<Auth::Door>(door);
69  ASSERT_LOG(door_odb, "IDoor is not of type Door.");
70 
71  target_ = door_odb;
72  target_door_id_ = door->id();
73 }
74 
75 void DoorEvent::before(const std::string &repr)
76 {
77  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
78  before_ = repr;
79 }
80 
81 void DoorEvent::after(const std::string &repr)
82 {
83  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
84  after_ = repr;
85 }
86 
88 {
89  if (target_.lock())
90  return target_.object_id();
91  return target_door_id_;
92 }
93 
94 const std::string &DoorEvent::before() const
95 {
96  return before_;
97 }
98 
99 const std::string &DoorEvent::after() const
100 {
101  return after_;
102 }
103 
105 {
106  std::stringstream ss;
107 
109  ss << "Door " << generate_target_description() << " has been created.";
111  ss << "Door " << generate_target_description() << " has been edited.";
113  ss << "Door " << generate_target_description() << " has been deleted.";
114 
115  return ss.str();
116 }
117 
119 {
120  Leosac::json desc;
121 
122  desc["id"] = target_id();
123  auto t = target_.load();
124  if (t)
125  desc["alias"] = t->alias();
126 
127  return desc.dump();
128 }
129 
130 std::shared_ptr<DoorEvent> DoorEvent::create_empty()
131 {
132  return std::shared_ptr<DoorEvent>(new DoorEvent());
133 }
134 
136 {
138 }
139 
141 {
143 }
144 
146 {
147  return access_point_id_after_;
148 }
149 
151 {
153 }
Leosac::Audit::DoorEvent::target_door_id_
Auth::DoorId target_door_id_
Definition: DoorEvent.hpp:79
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::Audit::DoorEvent
Provides an implementation of IDoorEvent.
Definition: DoorEvent.hpp:33
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
Leosac::Audit::EventType::DOOR_UPDATED
@ DOOR_UPDATED
Leosac::Audit::DoorEvent::create_empty
static std::shared_ptr< DoorEvent > create_empty()
Definition: DoorEvent.cpp:130
Leosac::Audit::DoorEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: DoorEvent.hpp:84
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::DoorEvent::access_point_id_before
virtual Auth::AccessPointId access_point_id_before() const override
Return the id of the associated access_point before the event took place.
Definition: DoorEvent.cpp:135
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::DoorEvent::after
virtual const std::string & after() const override
Definition: DoorEvent.cpp:99
Leosac::Audit::DoorEvent::target_id
virtual Auth::DoorId target_id() const override
Definition: DoorEvent.cpp:87
Leosac::Audit::DoorEvent::access_point_id_after_
Auth::AccessPointId access_point_id_after_
The id of associated AP after the event.
Definition: DoorEvent.hpp:105
Leosac::Audit::DoorEvent::generate_target_description
std::string generate_target_description() const
Generate a short description for the targeted door.
Definition: DoorEvent.cpp:118
Leosac::Audit::DoorEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: DoorEvent.hpp:89
Leosac::Audit::DoorEvent::target
virtual void target(Auth::IDoorPtr door) override
Set the door that is targeted by the event.
Definition: DoorEvent.cpp:63
Door.hpp
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
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::DoorEvent::access_point_id_before_
Auth::AccessPointId access_point_id_before_
The id of the associated AP before the event.
Definition: DoorEvent.hpp:98
Leosac::Auth::DoorId
unsigned long DoorId
Definition: AuthFwd.hpp:107
DoorEvent.hpp
Leosac::Audit::DoorEvent::target_
Auth::DoorLWPtr target_
Definition: DoorEvent.hpp:77
Leosac::Audit::DoorEvent::DoorEvent
DoorEvent()
Definition: DoorEvent.cpp:31
Leosac::Audit::DoorEvent::create
static std::shared_ptr< DoorEvent > create(const DBPtr &database, Auth::IDoorPtr target_door, AuditEntryPtr parent)
Definition: DoorEvent.cpp:38
Leosac::Auth::IDoorPtr
std::shared_ptr< IDoor > IDoorPtr
Definition: AuthFwd.hpp:104
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::DoorEvent::access_point_id_after
virtual Auth::AccessPointId access_point_id_after() const override
Return the id of the associated access_point after the event took place.
Definition: DoorEvent.cpp:145
log.hpp
Leosac::Audit::EventType::DOOR_CREATED
@ DOOR_CREATED
Leosac::Audit::AuditEntry::id
virtual AuditEntryId id() const override
Retrieve the identifier of the entry.
Definition: AuditEntry.cpp:39
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::DoorEvent::before
virtual const std::string & before() const override
Definition: DoorEvent.cpp:94
Leosac::Audit::DoorEvent::generate_description
virtual std::string generate_description() const override
Generate a description for this event.
Definition: DoorEvent.cpp:104
Leosac::Audit::DoorEventPtr
std::shared_ptr< DoorEvent > DoorEventPtr
Definition: AuditFwd.hpp:108
Leosac::Auth::AccessPointId
unsigned long AccessPointId
Definition: AuthFwd.hpp:128