Leosac  0.8.0
Open Source Access Control
AccessPointEvent.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 "AccessPointEvent.hpp"
21 #include "LeosacFwd.hpp"
22 #include "core/audit/AccessPointEvent_odb.h"
24 #include "tools/JSONUtils.hpp"
26 #include "tools/log.hpp"
27 
28 using namespace Leosac;
29 using namespace Leosac::Audit;
30 
32  : target_ap_id_(0)
33 {
34 }
35 
36 std::shared_ptr<AccessPointEvent>
38  AuditEntryPtr parent)
39 {
40  ASSERT_LOG(database, "Database cannot be null.");
41  ASSERT_LOG(target_ap, "Target AccessPoint must be non null.");
42  ASSERT_LOG(target_ap->id(), "Target AccessPoint must be already persisted.");
43  ASSERT_LOG(parent, "Parent must be non null.");
44  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
45 
47 
49  std::shared_ptr<Audit::AccessPointEvent>(new Audit::AccessPointEvent());
50  audit->database_ = database;
51  audit->target(target_ap);
52  database->persist(audit);
53 
54  audit->set_parent(parent);
55  database->update(audit);
56 
57  t.commit();
58  return audit;
59 }
60 
62 {
63  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
64  if (ap)
65  ASSERT_LOG(ap->id(), "AccessPoint has no id.");
66  auto ap_odb = std::dynamic_pointer_cast<Auth::AccessPoint>(ap);
67  ASSERT_LOG(ap_odb, "IAccessPoint is not of type AccessPoint.");
68 
69  target_ = ap_odb;
70  target_ap_id_ = ap->id();
71 }
72 
73 void AccessPointEvent::before(const std::string &repr)
74 {
75  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
76  before_ = repr;
77 }
78 
79 void AccessPointEvent::after(const std::string &repr)
80 {
81  ASSERT_LOG(!finalized(), "Audit entry is already finalized.");
82  after_ = repr;
83 }
84 
86 {
87  if (target_.lock())
88  return target_.object_id();
89  return target_ap_id_;
90 }
91 
92 const std::string &AccessPointEvent::before() const
93 {
94  return before_;
95 }
96 
97 const std::string &AccessPointEvent::after() const
98 {
99  return after_;
100 }
101 
103 {
104  std::stringstream ss;
105 
107  ss << "AccessPoint " << generate_target_description()
108  << " has been created.";
110  ss << "AccessPoint " << generate_target_description() << " has been edited.";
112  ss << "AccessPoint " << generate_target_description()
113  << " 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 
131 {
133 }
Leosac::Audit::EventType::ACCESS_POINT_CREATED
@ ACCESS_POINT_CREATED
Leosac::Audit::AccessPointEvent::target_ap_id_
Auth::AccessPointId target_ap_id_
Definition: AccessPointEvent.hpp:72
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
AccessPoint.hpp
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::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
Leosac::Audit::AccessPointEvent::target_id
Auth::AccessPointId target_id() const override
Definition: AccessPointEvent.cpp:85
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::AccessPointEvent::AccessPointEvent
AccessPointEvent()
Definition: AccessPointEvent.cpp:31
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::AccessPointEvent
Provides an implementation of IAccessPointEvent.
Definition: AccessPointEvent.hpp:33
Leosac::Audit::AccessPointEvent::after_
std::string after_
Optional JSON dump of the object after the event took place.
Definition: AccessPointEvent.hpp:82
Leosac::Auth::IAccessPointPtr
std::shared_ptr< IAccessPoint > IAccessPointPtr
Definition: AuthFwd.hpp:127
Leosac::Audit::EventType::ACCESS_POINT_DELETED
@ ACCESS_POINT_DELETED
Leosac::Audit::AccessPointEvent::create_empty
static std::shared_ptr< AccessPointEvent > create_empty()
Definition: AccessPointEvent.cpp:130
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::EventType::ACCESS_POINT_UPDATED
@ ACCESS_POINT_UPDATED
Leosac::Audit::AccessPointEvent::before_
std::string before_
Optional JSON dump of the object before the event took place.
Definition: AccessPointEvent.hpp:77
AccessPointEvent.hpp
Leosac::Audit::AccessPointEvent::after
const std::string & after() const override
Definition: AccessPointEvent.cpp:97
JSONUtils.hpp
Leosac::Audit::AccessPointEvent::generate_target_description
std::string generate_target_description() const
Generate a short description for the targeted AP.
Definition: AccessPointEvent.cpp:118
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::AccessPointEvent::before
const std::string & before() const override
Definition: AccessPointEvent.cpp:92
Leosac::Audit::AccessPointEvent::create
static std::shared_ptr< AccessPointEvent > create(const DBPtr &database, Auth::IAccessPointPtr target_ap, AuditEntryPtr parent)
Definition: AccessPointEvent.cpp:37
Leosac::Audit::AccessPointEvent::target
virtual void target(Auth::IAccessPointPtr door) override
Set the AccessPoint that is targeted by the event.
Definition: AccessPointEvent.cpp:61
Leosac::Audit::AccessPointEvent::generate_description
std::string generate_description() const override
Generate a description for this event.
Definition: AccessPointEvent.cpp:102
log.hpp
Leosac::Audit::AccessPointEventPtr
std::shared_ptr< AccessPointEvent > AccessPointEventPtr
Definition: AuditFwd.hpp:111
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::AccessPointEvent::target_
Auth::AccessPointLWPtr target_
Definition: AccessPointEvent.hpp:70
Leosac::Auth::AccessPointId
unsigned long AccessPointId
Definition: AuthFwd.hpp:128