Leosac  0.7.0
OpenSourceAccessControl
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  NOTICE(
64  "Trying to finalizing already finalized entry. Doing nothing instead.");
65  return;
66  }
67  finalized_ = true;
68  ASSERT_LOG(database_, "Null database pointer for AuditEntry.");
69  duration_ += etc_.elapsed();
70  database_->update(*this);
71 }
72 
74 {
75  return finalized_;
76 }
77 
79 {
80  ASSERT_LOG(!finalized_, "Audit entry is already finalized.");
81  event_mask_ = mask;
82 }
83 
85 {
86  return event_mask_;
87 }
88 
90 {
91  if (user)
92  ASSERT_LOG(user->id(), "Author is not already persisted.");
93  author_ = user;
94 }
95 
97 {
98  ASSERT_LOG(id_, "Current audit entry must be already persisted.");
99  ASSERT_LOG(parent, "Parent must not be null");
100  ASSERT_LOG(parent->id(), "Parent must be already persisted.");
101  ASSERT_LOG(!parent_.lock(), "Entry must have no parent.");
102  auto parent_odb = std::dynamic_pointer_cast<AuditEntry>(parent);
103  ASSERT_LOG(parent_odb, "Parent is not of type AuditEntry");
104 
105  parent_odb->children_.push_back(shared_from_this());
106  parent_ = parent_odb;
107 
108  if (!author_)
109  author_ = parent_odb->author_;
110 }
111 
113 {
114  auto parent = parent_.lock();
115  if (!parent)
116  return;
117 
118  auto &children = parent->children_;
119  children.erase(std::remove(children.begin(), children.end(), shared_from_this()),
120  children.end());
121  parent_.reset();
122 }
123 
125 {
126  return children_.size();
127 }
128 
129 size_t AuditEntry::version() const
130 {
131  return version_;
132 }
133 
135 {
136  ASSERT_LOG(odb::transaction::has_current(), "Not currently in transaction.");
137  database_->reload(this);
138 }
139 
141 {
142  return parent_.lock();
143 }
144 
146 {
147  database_ = db;
148 }
149 
151 {
152  if (author_)
153  return author_.object_id();
154  return 0;
155 }
156 
157 boost::posix_time::ptime AuditEntry::timestamp() const
158 {
159  return timestamp_;
160 }
161 
163 {
164  db::OptionalTransaction t(db->begin());
165  using Query = odb::query<AuditEntry>;
166  Query q("ORDER BY id DESC LIMIT 1");
167 
168  return db->query_one<AuditEntry>(q);
169 }
bool finalized_
Audit entry are sometime persisted multiple time before reaching their final state.
Definition: AuditEntry.hpp:133
virtual void remove_parent() override
Remove the parent-child relationship between this entry and its parent.
Definition: AuditEntry.cpp:112
Implementation of IAuditEntry, backed by ODB.
Definition: AuditEntry.hpp:45
This is the header file for a generated source file, GitSHA1.cpp.
Definition: log.hpp:36
virtual size_t children_count() const override
Returns the number of children that this entry has.
Definition: AuditEntry.cpp:124
virtual void author(Auth::UserPtr user) override
Set the author of the entry.
Definition: AuditEntry.cpp:89
unsigned long AuditEntryId
Definition: AuditFwd.hpp:31
size_t duration_
How long did it take for the Audit object to be finalized.
Definition: AuditEntry.hpp:124
static AuditEntryPtr get_last_audit(DBPtr db)
Retrieve the last (finalized) audit entry.
Definition: AuditEntry.cpp:162
virtual bool finalized() const override
Is this entry finalized.
Definition: AuditEntry.cpp:73
Tools::ElapsedTimeCounter etc_
Keep track of how long the object has been alive.
Definition: AuditEntry.hpp:149
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
std::shared_ptr< IAuditEntry > IAuditEntryPtr
Definition: AuditFwd.hpp:40
virtual IAuditEntryPtr parent() const override
Retrieve the parent of this entry.
Definition: AuditEntry.cpp:140
virtual void finalize() override
Mark the entry as finalized, and update it wrt the database.
Definition: AuditEntry.cpp:57
unsigned long UserId
Definition: AuthFwd.hpp:34
void odb_callback(odb::callback_event e, odb::database &) const
Implementation of an ODB callback.
Definition: AuditEntry.cpp:44
std::vector< AuditEntryPtr > children_
Definition: AuditEntry.hpp:103
virtual Auth::UserId author_id() const override
Retrieve the user id of the author of this entry.
Definition: AuditEntry.cpp:150
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
virtual boost::posix_time::ptime timestamp() const override
Retrieve unix timestamp.
Definition: AuditEntry.cpp:157
std::shared_ptr< AuditEntry > AuditEntryPtr
Definition: AuditFwd.hpp:81
virtual const EventMask & event_mask() const override
Retrieve the current event mask.
Definition: AuditEntry.cpp:84
DBPtr database_
Pointer to the database.
Definition: AuditEntry.hpp:142
std::shared_ptr< User > UserPtr
Definition: AuthFwd.hpp:31
boost::posix_time::ptime timestamp_
Definition: AuditEntry.hpp:100
FlagSet< EventType > EventMask
Definition: AuditFwd.hpp:179
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
virtual void reload() override
Reload the object from the database.
Definition: AuditEntry.cpp:134
virtual size_t version() const override
Returns the ODB version of the object.
Definition: AuditEntry.cpp:129
size_t elapsed() const
Returns the elapsed time, in milliseconds, since the creation of the ElapsedTimeCounter object...
virtual void set_parent(IAuditEntryPtr parent) override
Set parent as the parent audit entry for this entry.
Definition: AuditEntry.cpp:96
Auth::UserLPtr author_
The user at the source of the entry.
Definition: AuditEntry.hpp:116
std::shared_ptr< odb::database > DBPtr
Definition: db_fwd.hpp:31
void database(DBPtr db)
Set the database pointer.
Definition: AuditEntry.cpp:145
virtual AuditEntryId id() const override
Retrieve the identifier of the entry.
Definition: AuditEntry.cpp:39
The Audit namespace provides classes and facilities to keep track of what&#39;s happening on the Leosac d...