Leosac  0.8.0
Open Source Access Control
AuditEntry.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 "AuditEntry.hpp"
21 #include "core/audit/AuditEntry_odb.h"
22 #include "core/auth/User.hpp"
23 #include "core/auth/User_odb.h"
25 #include "tools/log.hpp"
26 #include <odb/query.hxx>
27 
28 using namespace Leosac;
29 using namespace Leosac::Audit;
30 
32  : duration_(0)
33  , finalized_(false)
34  , version_(0)
35 {
36  timestamp_ = boost::posix_time::second_clock::local_time();
37 }
38 
40 {
41  return id_;
42 }
43 
44 void AuditEntry::odb_callback(odb::callback_event e, odb::database &db) const
45 {
46  if (e == odb::callback_event::post_update ||
47  e == odb::callback_event::post_persist)
48  {
49  if (auto parent = parent_.lock())
50  {
51  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
52  db.update(parent);
53  }
54  }
55 }
56 
58 {
59  ASSERT_LOG(odb::transaction::has_current(),
60  "Not currently in a database transaction.");
61  if (finalized_)
62  {
63  INFO("Trying to finalizing already finalized entry. Doing nothing instead.");
64  return;
65  }
66  finalized_ = true;
67  ASSERT_LOG(database_, "Null database pointer for AuditEntry.");
68  duration_ += etc_.elapsed();
69  database_->update(*this);
70 }
71 
73 {
74  return finalized_;
75 }
76 
78 {
79  ASSERT_LOG(!finalized_, "Audit entry is already finalized.");
80  event_mask_ = mask;
81 }
82 
84 {
85  return event_mask_;
86 }
87 
89 {
90  if (user)
91  ASSERT_LOG(user->id(), "Author is not already persisted.");
92  author_ = user;
93 }
94 
96 {
97  ASSERT_LOG(id_, "Current audit entry must be already persisted.");
98  ASSERT_LOG(parent, "Parent must not be null");
99  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
100  ASSERT_LOG(!parent_.lock(), "Entry must have no parent.");
101  auto parent_odb = std::dynamic_pointer_cast<AuditEntry>(parent);
102  ASSERT_LOG(parent_odb, "Parent is not of type AuditEntry");
103 
104  parent_odb->children_.push_back(shared_from_this());
105  parent_ = parent_odb;
106 
107  if (!author_)
108  author_ = parent_odb->author_;
109 }
110 
112 {
113  auto parent = parent_.lock();
114  if (!parent)
115  return;
116 
117  auto &children = parent->children_;
118  children.erase(std::remove(children.begin(), children.end(), shared_from_this()),
119  children.end());
120  parent_.reset();
121 }
122 
124 {
125  return children_.size();
126 }
127 
128 size_t AuditEntry::version() const
129 {
130  return version_;
131 }
132 
134 {
135  ASSERT_LOG(odb::transaction::has_current(), "Not currently in transaction.");
136  database_->reload(this);
137 }
138 
140 {
141  return parent_.lock();
142 }
143 
145 {
146  database_ = db;
147 }
148 
150 {
151  if (author_)
152  return author_.object_id();
153  return 0;
154 }
155 
156 boost::posix_time::ptime AuditEntry::timestamp() const
157 {
158  return timestamp_;
159 }
160 
162 {
163  db::OptionalTransaction t(db->begin());
164  using Query = odb::query<AuditEntry>;
165  Query q("ORDER BY id DESC LIMIT 1");
166 
167  return db->query_one<AuditEntry>(q);
168 }
Leosac::Audit::AuditEntry::author
virtual void author(Auth::UserPtr user) override
Set the author of the entry.
Definition: AuditEntry.cpp:88
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::Audit::AuditEntry::version_
const size_t version_
Definition: AuditEntry.hpp:153
Leosac::Audit::AuditEntry::parent_
AuditEntryWPtr parent_
Definition: AuditEntry.hpp:107
Leosac::Audit::AuditEntry::children_count
virtual size_t children_count() const override
Returns the number of children that this entry has.
Definition: AuditEntry.cpp:123
Leosac::Tools::ElapsedTimeCounter::elapsed
size_t elapsed() const
Returns the elapsed time, in milliseconds, since the creation of the ElapsedTimeCounter object.
Definition: ElapsedTimeCounter.cpp:30
Leosac::Audit::AuditEntryPtr
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
AuditEntry.hpp
Leosac::Audit::AuditEntry::odb_callback
void odb_callback(odb::callback_event e, odb::database &) const
Implementation of an ODB callback.
Definition: AuditEntry.cpp:44
Leosac::Audit::AuditEntry::id_
AuditEntryId id_
Definition: AuditEntry.hpp:97
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Audit::AuditEntry::author_id
virtual Auth::UserId author_id() const override
Retrieve the user id of the author of this entry.
Definition: AuditEntry.cpp:149
Leosac::Audit::AuditEntry::database
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:144
Leosac::Auth::UserPtr
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
Leosac::Audit::AuditEntry::children_
std::vector< AuditEntryPtr > children_
Definition: AuditEntry.hpp:103
User.hpp
Leosac::Audit::AuditEntryId
unsigned long AuditEntryId
Definition: AuditFwd.hpp:31
INFO
@ INFO
Definition: log.hpp:34
Leosac::DBPtr
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
Leosac::Audit::AuditEntry::event_mask
virtual const EventMask & event_mask() const override
Retrieve the current event mask.
Definition: AuditEntry.cpp:83
Leosac::Audit::IAuditEntryPtr
std::shared_ptr< IAuditEntry > IAuditEntryPtr
Definition: AuditFwd.hpp:40
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::Audit::AuditEntry::remove_parent
virtual void remove_parent() override
Remove the parent-child relationship between this entry and its parent.
Definition: AuditEntry.cpp:111
Leosac::Audit::AuditEntry::timestamp_
boost::posix_time::ptime timestamp_
Definition: AuditEntry.hpp:100
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::duration_
size_t duration_
How long did it take for the Audit object to be finalized.
Definition: AuditEntry.hpp:124
Leosac::Audit::AuditEntry::finalized
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:72
Leosac::Audit::AuditEntry::finalize
virtual void finalize() override
Mark the entry as finalized, and update it wrt the database.
Definition: AuditEntry.cpp:57
Leosac::Audit::AuditEntry::get_last_audit
static AuditEntryPtr get_last_audit(DBPtr db)
Retrieve the last (finalized) audit entry.
Definition: AuditEntry.cpp:161
Leosac::Audit::AuditEntry::etc_
Tools::ElapsedTimeCounter etc_
Keep track of how long the object has been alive.
Definition: AuditEntry.hpp:149
Leosac::Audit::EventMask
FlagSet< EventType > EventMask
Definition: AuditFwd.hpp:179
Leosac::Audit::AuditEntry::parent
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:139
Leosac::Audit::AuditEntry::set_parent
virtual void set_parent(IAuditEntryPtr parent) override
Set parent as the parent audit entry for this entry.
Definition: AuditEntry.cpp:95
Leosac::Audit::AuditEntry::timestamp
virtual boost::posix_time::ptime timestamp() const override
Retrieve unix timestamp.
Definition: AuditEntry.cpp:156
Leosac::Audit::AuditEntry::database_
DBPtr database_
Pointer to the database.
Definition: AuditEntry.hpp:142
log.hpp
Leosac::Audit::AuditEntry::finalized_
bool finalized_
Audit entry are sometime persisted multiple time before reaching their final state.
Definition: AuditEntry.hpp:133
Leosac::Audit::AuditEntry::id
virtual AuditEntryId id() const override
Retrieve the identifier of the entry.
Definition: AuditEntry.cpp:39
Leosac::Auth::UserId
unsigned long UserId
Definition: AuthFwd.hpp:34
Leosac::Audit::AuditEntry
Implementation of IAuditEntry, backed by ODB.
Definition: AuditEntry.hpp:45
Leosac::Audit::AuditEntry::event_mask_
EventMask event_mask_
Definition: AuditEntry.hpp:119
Leosac::Audit::AuditEntry::reload
virtual void reload() override
Reload the object from the database.
Definition: AuditEntry.cpp:133
Leosac::Audit::AuditEntry::version
virtual size_t version() const override
Returns the ODB version of the object.
Definition: AuditEntry.cpp:128
Leosac::Audit::AuditEntry::AuditEntry
AuditEntry()
Definition: AuditEntry.cpp:31
Leosac::Audit::AuditEntry::author_
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116